Comments (1)
@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)
- [Feature]: Jest should use the `debugger` statement on failure
- [Bug]: `instanceof Date` fails when using a date created from node-api (n-api)
- [Docs]: One of the examples for configuring Jest to work with Eslint no longer works with the flat config.
- [Bug]: inflight deprecated, glob deprecated HOT 1
- [Bug]: module is not mocked if there's signal input in spec file (angular)
- [Bug]: isEqual with large Float64Arrays is slow HOT 1
- [Feature]: Provide console logs per testcase to reporter
- [Bug]: require.main has invalid value null
- [Bug]: toThrow fails if error object contains a cause property
- [Feature]: add `expect(spy).toHaveResolvedWith` for spies on async functions
- [Feature]: Replace `chalk` dependency with a lighter alternative HOT 3
- [Feature]: make PRINT_LIMIT configurable
- [Feature]: Show which project failed when a `preset` fails to load HOT 1
- [Bug]: Calling ts.createSourceFile in a test file hangs with Typescript 5.x.x
- [Bug]: [jest-runner] loses error `stack` property for failed test when sends a message to jest
- [Bug]: No way to use inline snapshots without Babel
- [Docs]: .tocontainequalitem() explanation is ambiguous HOT 1
- [Feature]: Expect the assertion to eventually pass
- [Bug]: CR (`"\r"`) in the test name breaks snapshot testing
- [Bug]: Snapshot testing does not detect the difference between CR ("\r") and LF ("\n")
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jest.