Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: createMockFromModule not mocking class correctly #14022

Closed
dominiktopp opened this issue Mar 20, 2023 · 12 comments
Closed

[Bug]: createMockFromModule not mocking class correctly #14022

dominiktopp opened this issue Mar 20, 2023 · 12 comments

Comments

@dominiktopp
Copy link

Version

29.5.0

Steps to reproduce

  1. Clone my repo at https://github.com/dominiktopp/jestCreateMockFromModuleIssue
  2. npm install
  3. npm test
  4. tests will fail

Expected behavior

I expect the test cases to succeed.

Actual behavior

Tests are failing because jest.createMockFromModule only mocks constructor and functions for classes but no other properties (also no getters).
The example for jest.createMockFromModule does not mock an exported class but an instance of a class:
class: new (class Bar {...})()

Additional context

No response

Environment

System:
  OS: Windows 10 10.0.19045
  CPU: (12) x64 Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
  Memory: 10.67 GB / 31.76 GB
Binaries:
  Node: 16.15.0 - C:\Program Files\nodejs\node.EXE
  npm: 8.5.5 - C:\Program Files\nodejs\npm.CMD
@mattvot
Copy link

mattvot commented Mar 24, 2023

Just to add I've been seeing a similar issue. Using the package.json resolves and yarn I determined that our tests work fine when I set:

{
  ...
  "resolutions": {
    "jest-mock": "29.4.2"
  }
}

However with:

{
  ...
  "resolutions": {
    "jest-mock": "29.4.3"
  }
}

Our failing tests call a function of an instance that has been mocked with jest.mock(). They fail because the function returns undefined instead of the expected value.

While I'm not 100% familiar with how jest-mock works yet, I've been looking though the history and spotted a few things that may have caused this:

Other than that I can't see how the other commits would make a difference here: https://github.com/facebook/jest/commits/main/packages/jest-mock

@mrazauskas
Copy link
Contributor

That seems different and not related with this issue. See #13925 for details and possible solutions.

@mattvot
Copy link

mattvot commented Mar 28, 2023

Thanks @mrazauskas. I can confirm that when I made a new codebase to reproduce the issue, the fix suggested by @CreativeTechGuy in #13925 (comment) worked. Unfortunately for our main codebase this did not work, so something else is afoot. We will be looking further at this in time. For now we've pinned jest at 29.4.2.

@mrazauskas
Copy link
Contributor

In your place I would try looking for jest.restoreAllMocks() in test files. If these are, try replacing them with jest.clearAllMocks() or recreating mocks within beforeEach.

Unfortunately jest.restoreAllMocks() did not call .mockRestore() on each mock before #13867. Now it does, but that isn’t always what was intended.

@github-actions
Copy link

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 github-actions bot added the Stale label Apr 27, 2023
@dominiktopp
Copy link
Author

dominiktopp commented May 3, 2023

Since the previous comments seem to be related to another problem, can anyone help me with my problem mocking classes with createMockFromModule please?

@github-actions github-actions bot removed the Stale label May 3, 2023
@github-actions
Copy link

github-actions bot commented Jun 2, 2023

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 github-actions bot added the Stale label Jun 2, 2023
@dominiktopp
Copy link
Author

still active, not stale

@github-actions github-actions bot removed the Stale label Jun 2, 2023
@github-actions
Copy link

github-actions bot commented Jul 2, 2023

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 github-actions bot added the Stale label Jul 2, 2023
@dominiktopp
Copy link
Author

not stale!

@github-actions github-actions bot removed the Stale label Jul 3, 2023
@mrazauskas
Copy link
Contributor

Duplicate of #14178

@mrazauskas mrazauskas marked this as a duplicate of #14178 Jul 5, 2023
@github-actions
Copy link

github-actions bot commented Aug 5, 2023

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 github-actions bot locked as resolved and limited conversation to collaborators Aug 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants