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]: Unable to use jest.mock #3206

Open
badsyntax opened this issue Jan 4, 2022 · 7 comments
Open

[Bug]: Unable to use jest.mock #3206

badsyntax opened this issue Jan 4, 2022 · 7 comments

Comments

@badsyntax
Copy link

badsyntax commented Jan 4, 2022

Version

27.1.2

Steps to reproduce

  1. Clone this repo
  2. cd examples/ts-only
  3. npm i
  4. Add jest.mock('example'); to src/welcome-message.spec.ts
  5. Run 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

System:
    OS: macOS 11.6
    CPU: (8) arm64 Apple M1
  Binaries:
    Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
    npm: 8.3.0 - ~/.nvm/versions/node/v16.13.1/bin/npm
  npmPackages:
    jest: ^27.3.1 => 27.4.3 
@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 4, 2022

When you run Jest in ESM mode, it is required to use @jest/globals because Jest ESM mode doesn't inject global jest object by default.

@badsyntax
Copy link
Author

Ok then, but I can't for the life of me get mocking to work when using @jest/globals.

In the same examples/ts-only directory:

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'
})

foo.ts:

export const foo = () => 'foo';

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 4, 2022

jest.mock isn't supported in ESM mode yet, see jestjs/jest#10976

Also you should take a look at jestjs/jest#9430 and search for keyword jest.(do|un)mock

I can conclude this is not an issue for ts-jest but for Jest itself.

@badsyntax
Copy link
Author

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.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 4, 2022

There are lots of things related to running Jest in ESM mode. We did have in documentation about
image
That link will redirect to Jest documentation, which contains the link to the jestjs/jest#9430

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.

@badsyntax
Copy link
Author

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.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 4, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants