jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: Named mocks defined at the module level do not show descriptive name in the reporter for failed assertions

Open jpierson-at-riis opened this issue 1 year ago • 4 comments

Version

27.5.1

Steps to reproduce

  1. Create a test with an intentionally failing assertion based on the call to a mock which is defined at the module level.
  2. Run the test using the default CLI reporter (ex. npm test in 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

jpierson-at-riis avatar Dec 30 '23 14:12 jpierson-at-riis

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();
    });
});

jpierson-at-riis avatar Dec 30 '23 15:12 jpierson-at-riis

Jest v27 is unsupported - does this still happen with v29? Running your test on main of this repo shows the correct behaviour

image

SimenB avatar Dec 30 '23 18:12 SimenB

@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.

jpierson-at-riis avatar Jan 05 '24 13:01 jpierson-at-riis

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.

github-actions[bot] avatar Feb 04 '24 14:02 github-actions[bot]

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.

github-actions[bot] avatar Mar 05 '24 14:03 github-actions[bot]

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.

github-actions[bot] avatar Mar 05 '24 14:03 github-actions[bot]

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.

github-actions[bot] avatar Apr 07 '24 00:04 github-actions[bot]