-
Notifications
You must be signed in to change notification settings - Fork 451
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]: Unable to use jest.mock #3206
Comments
When you run Jest in ESM mode, it is required to use |
Ok then, but I can't for the life of me get mocking to work when using In the same Example test file: import {jest} from '@jest/globals';
import {foo} from './foo'
jest.mock('./foo', () => ({
__esModule: true,
foo: jest.fn()
}));
test('mock', () => {
expect(foo()).toBe(''); // still prints 'foo'
})
export const foo = () => 'foo'; |
Also you should take a look at jestjs/jest#9430 and search for keyword I can conclude this is not an issue for |
If that's the case do you think the ts-jest documentation related to ESM support should call this out? I don't really see a point in using the esm transformers if you can't use the jest API. |
There are lots of things related to running Jest in ESM mode. We did have in documentation about I don't think mentioning everything specific a good idea. Perhaps a note about missing ESM features which leads users to Jest documentation is better. |
I personally think this would be helpful. If you read the docs page at a glance you get the impression ESM will just work. It'd be helpful if there was a note to explicitly mentioned this is not the case. |
Feel free to submit a PR :) The location for docs will be: |
Version
27.1.2
Steps to reproduce
cd examples/ts-only
npm i
jest.mock('example');
tosrc/welcome-message.spec.ts
npm run test-esm
Expected behavior
Jest to run without an error and for the jest mocking to work correctly.
Actual behavior
I get the following error:
ReferenceError: jest is not defined
Debug log
I can't post a debug log because GitHub Issues throws an error with
There was an error creating your Issue: body is too long (maximum is 65536 characters).
Additional context
I've tried importing jest from
@jest/globals
which resolved the error but then the mocking does not work.It would be useful if you could provide a working mocking example.
Environment
The text was updated successfully, but these errors were encountered: