-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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]: referencing jest
inside jest.mock
block throws, if jest
is imported from @jest/globals
#12422
Comments
@SimenB As I was mentioning, if an In this case it might be enough to add |
@nicolo-ribaudo any tips here? 🙂 The hoist plugin transforms the Failing unit test: diff --git i/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts w/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts
index c6ad87de81..b03ff10728 100644
--- i/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts
+++ w/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts
@@ -90,6 +90,17 @@ pluginTester({
formatResult,
snapshot: true,
},
+ 'imported jest within jest': {
+ code: formatResult(`
+ import {jest} from '@jest/globals';
+
+ jest.mock('some-module', () => {
+ jest.mock('some-module');
+ });
+ `),
+ formatResult,
+ snapshot: true,
+ },
},
/* eslint-enable */
}); If it's not an import, it's transformed into a function, maybe we should do the same for the import? |
When you are checking if a variable is safe to use, you could get
An alternative is to remove the |
Awesome, thanks @nicolo-ribaudo! |
I've found another issue while playing with this - we only replace calls to jest.mock('some-module', () => {
jest.requireActual('some-module');
}); is transformed into _getJestObj().mock('some-module', () => {
jest.requireActual('some-module');
});
function _getJestObj() {
const {jest} = require('@jest/globals');
_getJestObj = () => jest;
return jest;
} Notice how the inner Diff with both tests (first fails due to the check discussed here, and the other produces the wrong result) diff --git i/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts w/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts
index c6ad87de81..950ff71647 100644
--- i/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts
+++ w/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts
@@ -90,6 +90,26 @@ pluginTester({
formatResult,
snapshot: true,
},
+ 'imported jest within jest': {
+ code: formatResult(`
+ import {jest} from '@jest/globals';
+
+ jest.mock('some-module', () => {
+ jest.requireActual('some-module');
+ });
+ `),
+ formatResult,
+ snapshot: true,
+ },
+ '"global" jest within jest': {
+ code: formatResult(`
+ jest.mock('some-module', () => {
+ jest.requireActual('some-module');
+ });
+ `),
+ formatResult,
+ snapshot: true,
+ },
},
/* eslint-enable */
}); |
jest
inside jest.mock
block throws, if jest
is imported from @jest/globals
in a TS test filejest
inside jest.mock
block throws, if jest
is imported from @jest/globals
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. |
I've ran into this as well, any thoughts on how we can get around the issue? The workaround I currently use is this import { jest, test, expect } from '@jest/globals';
import b from './b'
import a from '../module';
jest.mock('../module');
test('some test', () => {
a.mockImplementationOnce(() => true);
expect(b()).toBe(false);
}) contrived example, but it does work. Its a little bit more work than just mocking the module out directly, it is more work overall. I haven't hit any gotchas with named imports though, so it does seem like a solid workaround |
@ScottAwesome I think something like this should work: import {jest, jest as mockJest} from '@jest/globals';
jest.mock('some-module', () => {
mockJest.mock('some-module');
}); |
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. |
Version
28.0.0-alpha.3
Steps to reproduce
Here is a repo with minimal reproduction: https://github.com/mrazauskas/x-jest-globals
I simply added explicit import to the example from docs (and used TS to trigger transpiration) – https://jestjs.io/docs/next/mock-functions#mocking-partials
Running
yarn jest
will throw an ReferenceError:Expected behavior
It should not throw ReferenceError.
Actual behavior
It throws ReferenceError, probably because of transpilation. I also tried transpiling using
ts-jest
and the error was gone. It seems like only users ofbabel-jest
are affected.Additional context
No response
Environment
The text was updated successfully, but these errors were encountered: