[Bug]: Named mocks defined at the module level do not show descriptive name in the reporter for failed assertions
Version
27.5.1
Steps to reproduce
- Create a test with an intentionally failing assertion based on the call to a mock which is defined at the module level.
- Run the test using the default CLI reporter (ex.
npm testin CRA)
Example:
const fn = jest.fn().mockName("myMockedFunction");
describe("QWERTY", () => {
test("should show the name of the mock in the test", () => {
expect(fn).toBeCalled();
});
});
Expected behavior
The descriptive name of myMockedFunction which was specified using the mockName function should be reported in failed assertions.
FAIL src/Example.test.js
● QWERTY › should show the name of the mock in the test
expect(myMockedFunction).toBeCalled()
Expected number of calls: >= 1
Received number of calls: 0
350 | describe("QWERTY", () => {
351 | test("should show the name of the mock in the test", () => {
> 352 | expect(fn).toBeCalled();
| ^
353 | });
354 | });
355 |
at Object.<anonymous> (src/Example.test.js:352:20)
Actual behavior
The failure is reported but instead of the descriptive name for the mock (myMockedFunction), it only shows jest.fn() which is less helpful and inconsistent with
FAIL src/Example.test.js
● QWERTY › should show the name of the mock in the test
expect(jest.fn()).toBeCalled()
Expected number of calls: >= 1
Received number of calls: 0
350 | describe("QWERTY", () => {
351 | test("should show the name of the mock in the test", () => {
> 352 | expect(fn).toBeCalled();
| ^
353 | });
354 | });
355 |
at Object.<anonymous> (src/Example.test.js:352:20)
Additional context
For what it's worth, I'm also not seeing any of the human readable assertions described in https://github.com/jestjs/jest/issues/6192 which leads me to expect reporter output like the example below:
Expected mock function 'myMockedFunction' to have been called.
Perhaps this is a regression or an intentional change since when this was implemented in 2018 but either way I figured I'd point it out because it also could be related. If not I can open a separate issue for this.
Additionally, it's also worth mentioning that the mock name does seem to get reported correctly when it is defined within the test itself such as in the example below.
describe("QWERTY", () => {
test("should show the name of the mock in the test", () => {
const fn = jest.fn().mockName("myMockedFunction");
expect(fn).toBeCalled();
});
});
Other potentially related issues:
- https://github.com/jestjs/jest/issues/6192
- https://github.com/jestjs/jest/issues/8503
- https://github.com/jestjs/jest/issues/12850
Environment
System:
OS: Linux 5.15 Ubuntu 20.04.6 LTS (Focal Fossa)
CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
Binaries:
Node: 20.10.0 - ~/.asdf/installs/nodejs/20.10.0/bin/node
npm: 10.2.3 - ~/.asdf/plugins/nodejs/shims/npm
Note that moving the mock definition up to the describe block also seems to exhibit the same issue as when it is defined within the module scope.
describe("QWERTY", () => {
const fn = jest.fn().mockName("myMockedFunction");
test("should show the name of the mock in the test", () => {
expect(fn).toHaveBeenCalled();
});
});
Jest v27 is unsupported - does this still happen with v29? Running your test on main of this repo shows the correct behaviour
@SimenB, looking at our project it uses CRA which seems to have stagnated and doesn't have any updates past v5.0.1 which was released back in 2022 and controls the dependency to Jest. So at the moment it doesn't seem possible for us to upgrade to test this without ejecting. After we work through this issue with stale dependencies I'll retest the issue with a more recent version of Jest and see if it helps. Thanks for your attention on this one.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.