ts-jest icon indicating copy to clipboard operation
ts-jest copied to clipboard

[Bug]: Unable to use jest.mock

Open badsyntax opened this issue 2 years ago • 7 comments

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 

badsyntax avatar Jan 04 '22 13:01 badsyntax

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.

ahnpnl avatar Jan 04 '22 13:01 ahnpnl

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';

badsyntax avatar Jan 04 '22 13:01 badsyntax

jest.mock isn't supported in ESM mode yet, see https://github.com/facebook/jest/pull/10976

Also you should take a look at https://github.com/facebook/jest/issues/9430 and search for keyword jest.(do|un)mock

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

ahnpnl avatar Jan 04 '22 13:01 ahnpnl

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.

badsyntax avatar Jan 04 '22 13:01 badsyntax

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 https://github.com/facebook/jest/issues/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.

ahnpnl avatar Jan 04 '22 13:01 ahnpnl

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.

badsyntax avatar Jan 04 '22 13:01 badsyntax

Feel free to submit a PR :) The location for docs will be:

  • https://github.com/kulshekhar/ts-jest/blob/main/website/docs/guides/esm-support.md
  • https://github.com/kulshekhar/ts-jest/blob/main/website/versioned_docs/version-27.0/guides/esm-support.md
  • https://github.com/kulshekhar/ts-jest/blob/main/website/versioned_docs/version-27.1/guides/esm-support.md

ahnpnl avatar Jan 04 '22 13:01 ahnpnl