Giter Club home page Giter Club logo

Comments (1)

KNU-K avatar KNU-K commented on July 23, 2024

@dipunm
The difference between the two test cases lies in how Jest manages mock functions' behavior regarding their results and instances.

In the first test case, jest.fn() is used to define a mock function, and mockImplementation is used to create a mock object when new customMock() is called:

const customMock = jest.fn().mockImplementation(() => ({ mock: 'mock' }));
const mock3 = new customMock();

Here, customMock.mock.instances is used to track instances created when the mock function is called. This array collects instances each time the mock function is invoked. However, accessing customMock.mock.instances[0] immediately after the function call might fail because the instance may not have been added to the array yet.

In the second test case, again jest.fn() is used to define a mock function, and mockImplementation creates a mock object when new customMock() is called:

const customMock = jest.fn().mockImplementation(() => ({ mock: 'mock' }));
const mock3 = new customMock();

Here, customMock.mock.results is used to track the results of each invocation of the mock function. Each element in customMock.mock.results contains information about a specific call, including the value property which holds the return value of that call.

console.log(mock3);
console.log(customMock.mock.results[0].value);
expect(customMock.mock.results[0].value).toStrictEqual(mock3);

This test case checks whether customMock.mock.results[0].value (the return value of the first call) is equal to mock3, the instance created by new customMock(). This approach is more reliable because it directly accesses the specific return value of the first call rather than relying on the instance being added to an array.

Therefore, the second test case is more likely to pass because it verifies the return value explicitly associated with the first function call using customMock.mock.results.

from jest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.