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]: mocking modules does not seem to work? #15351

Closed
patrick99e99 opened this issue Oct 20, 2024 · 4 comments
Closed

[Bug]: mocking modules does not seem to work? #15351

patrick99e99 opened this issue Oct 20, 2024 · 4 comments

Comments

@patrick99e99
Copy link

Version

29.7.0

Steps to reproduce

  1. clone repo: https://github.com/patrick99e99/jest-mock-test
  2. npm i
  3. npm run test

Expected behavior

3 tests passing

Actual behavior

Nothing works.. mocking the module for ./my-class.js

  1. an export foo is added, it is not there when imported
  2. MyClass is overwritten, expecting a mocked out instance with someMethod returning mocked-value but instead it is the original implementation.
  3. Attempted same thing but with a delayed import aka await import('./my-class'); .. same result.

Additional context

No response

Environment

System:
    OS: macOS 12.4
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node
    npm: 9.6.7 - ~/.nvm/versions/node/v18.17.1/bin/npm
  npmPackages:
    jest: ^29.7.0 => 29.7.0
@patrick99e99
Copy link
Author

patrick99e99 commented Oct 20, 2024

I also added the sound player example tests from https://jestjs.io/docs/es6-class-mocks

They also do not work...

@patrick99e99
Copy link
Author

patrick99e99 commented Oct 23, 2024

Also, am now trying to mock puppeteer..

test code does:

jest.mock('puppeteer', () => ({
  __esModule: true,
  launch: jest.fn().returnMockValue('fake-launch'),
}));

beforeEach(async () => {
  const imported = await import('./index.js')  // <-- delaying importing real code to make sure puppeteer is mocked...
  ...
})

index.js

import puppeteer from 'puppeteer';
console.log(puppeteer); <--- despite being mocked, it's still the REAL implementation, not the mock...

So I cannot get this to work either... Strangely, I can mock redis in this exact same way and it works just fine.. but not not local es6 files, and not puppeteer.. I just don't understand why I am having such trouble getting mocks to work.

@patrick99e99
Copy link
Author

It looks like this issue is a duplicate of #10025

Copy link

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 Nov 24, 2024
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

1 participant