From 8ca92ea5510e118be243f4ac66756e8c075f1e3e Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 3 Oct 2022 11:13:17 +0200 Subject: [PATCH 1/4] fix(bable-hoist-plugin): ignore TS type queries --- packages/babel-plugin-jest-hoist/package.json | 1 + .../__snapshots__/hoistPlugin.test.ts.snap | 26 +++++++++++++++++++ .../src/__tests__/hoistPlugin.test.ts | 17 ++++++++++++ packages/babel-plugin-jest-hoist/src/index.ts | 7 ++++- yarn.lock | 1 + 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/packages/babel-plugin-jest-hoist/package.json b/packages/babel-plugin-jest-hoist/package.json index b242dc0b0998..e28a60a5ba5e 100644 --- a/packages/babel-plugin-jest-hoist/package.json +++ b/packages/babel-plugin-jest-hoist/package.json @@ -28,6 +28,7 @@ "devDependencies": { "@babel/core": "^7.11.6", "@babel/preset-react": "^7.12.1", + "@babel/preset-typescript": "^7.18.6", "@types/babel__template": "^7.0.2", "@types/node": "*", "@types/prettier": "^2.1.5", diff --git a/packages/babel-plugin-jest-hoist/src/__tests__/__snapshots__/hoistPlugin.test.ts.snap b/packages/babel-plugin-jest-hoist/src/__tests__/__snapshots__/hoistPlugin.test.ts.snap index 3a5a0817baed..a95af4e804b7 100644 --- a/packages/babel-plugin-jest-hoist/src/__tests__/__snapshots__/hoistPlugin.test.ts.snap +++ b/packages/babel-plugin-jest-hoist/src/__tests__/__snapshots__/hoistPlugin.test.ts.snap @@ -1,5 +1,31 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`babel-plugin-jest-hoist TS typeof usage in jest.mock: TS typeof usage in jest.mock 1`] = ` + +jest.mock('some-module', () => { + const actual = jest.requireActual('some-module'); + + return jest.fn(); +}); + + ↓ ↓ ↓ ↓ ↓ ↓ + +_getJestObj().mock('some-module', () => { + const actual = jest.requireActual('some-module'); + return jest.fn(); +}); + +function _getJestObj() { + const {jest} = require('@jest/globals'); + + _getJestObj = () => jest; + + return jest; +} + + +`; + exports[`babel-plugin-jest-hoist automatic react runtime: automatic react runtime 1`] = ` jest.mock('./App', () => () =>
Hello world
); diff --git a/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts b/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts index 685ab680aa75..7828b5095307 100644 --- a/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts +++ b/packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts @@ -134,6 +134,23 @@ pluginTester({ formatResult, snapshot: true, }, + 'TS typeof usage in jest.mock': { + babelOptions: { + babelrc: false, + configFile: false, + filename: path.resolve(__dirname, '../file.ts'), + presets: [[require.resolve('@babel/preset-typescript')]], + }, + code: formatResult(` + jest.mock('some-module', () => { + const actual = jest.requireActual('some-module'); + + return jest.fn(); + }); + `), + formatResult, + snapshot: true, + }, }, /* eslint-enable */ }); diff --git a/packages/babel-plugin-jest-hoist/src/index.ts b/packages/babel-plugin-jest-hoist/src/index.ts index 21af10f6c2a2..2841036ede50 100644 --- a/packages/babel-plugin-jest-hoist/src/index.ts +++ b/packages/babel-plugin-jest-hoist/src/index.ts @@ -106,7 +106,12 @@ const IDVisitor = { ) { ids.add(path); }, - blacklist: ['TypeAnnotation', 'TSTypeAnnotation', 'TSTypeReference'], + blacklist: [ + 'TypeAnnotation', + 'TSTypeAnnotation', + 'TSTypeQuery', + 'TSTypeReference', + ], }; const FUNCTIONS: Record< diff --git a/yarn.lock b/yarn.lock index 5e4330a38c9b..d6161a60e909 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6162,6 +6162,7 @@ __metadata: dependencies: "@babel/core": ^7.11.6 "@babel/preset-react": ^7.12.1 + "@babel/preset-typescript": ^7.18.6 "@babel/template": ^7.3.3 "@babel/types": ^7.3.3 "@types/babel__core": ^7.1.14 From ca8ae1f5adf9abfc85e454b5d13c577a2e8b142b Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 3 Oct 2022 11:16:26 +0200 Subject: [PATCH 2/4] changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1ef3104cc39..f69fbb9ebd25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ ### Fixes +- `[babel-plugin-jest-hoist]` Support imported `jest` in mock factory ([#13367](https://github.com/facebook/jest/pull/13367)) + ### Chore & Maintenance ### Performance From 3ae20dfdcb6ccc9229564a83507b57e433a4cfbf Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 3 Oct 2022 11:23:38 +0200 Subject: [PATCH 3/4] make constraints happy --- packages/babel-plugin-jest-hoist/package.json | 2 +- yarn.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-plugin-jest-hoist/package.json b/packages/babel-plugin-jest-hoist/package.json index e28a60a5ba5e..d691bdb1a79f 100644 --- a/packages/babel-plugin-jest-hoist/package.json +++ b/packages/babel-plugin-jest-hoist/package.json @@ -28,7 +28,7 @@ "devDependencies": { "@babel/core": "^7.11.6", "@babel/preset-react": "^7.12.1", - "@babel/preset-typescript": "^7.18.6", + "@babel/preset-typescript": "^7.0.0", "@types/babel__template": "^7.0.2", "@types/node": "*", "@types/prettier": "^2.1.5", diff --git a/yarn.lock b/yarn.lock index d6161a60e909..78099e535f34 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6162,7 +6162,7 @@ __metadata: dependencies: "@babel/core": ^7.11.6 "@babel/preset-react": ^7.12.1 - "@babel/preset-typescript": ^7.18.6 + "@babel/preset-typescript": ^7.0.0 "@babel/template": ^7.3.3 "@babel/types": ^7.3.3 "@types/babel__core": ^7.1.14 From fc90ede3ecede790bf900aec73866f02a6e12798 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 3 Oct 2022 11:34:56 +0200 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f69fbb9ebd25..f8a4d33ec1c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ ### Fixes -- `[babel-plugin-jest-hoist]` Support imported `jest` in mock factory ([#13367](https://github.com/facebook/jest/pull/13367)) +- `[babel-plugin-jest-hoist]` Ignore `TSTypeQuery` when checking for hoisted references ([#13367](https://github.com/facebook/jest/pull/13367)) ### Chore & Maintenance