Skip to content

Commit

Permalink
fix(babel-hoist-plugin): ignore TS type queries (#13367)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Oct 3, 2022
1 parent 42a466c commit 04dae6c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

### Fixes

- `[babel-plugin-jest-hoist]` Ignore `TSTypeQuery` when checking for hoisted references ([#13367](https://github.com/facebook/jest/pull/13367))

### Chore & Maintenance

### Performance
Expand Down
1 change: 1 addition & 0 deletions packages/babel-plugin-jest-hoist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"devDependencies": {
"@babel/core": "^7.11.6",
"@babel/preset-react": "^7.12.1",
"@babel/preset-typescript": "^7.0.0",
"@types/babel__template": "^7.0.2",
"@types/node": "*",
"@types/prettier": "^2.1.5",
Expand Down
Original file line number Diff line number Diff line change
@@ -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<typeof actual.method>();
});
↓ ↓ ↓ ↓ ↓ ↓
_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', () => () => <div>Hello world</div>);
Expand Down
17 changes: 17 additions & 0 deletions packages/babel-plugin-jest-hoist/src/__tests__/hoistPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof actual.method>();
});
`),
formatResult,
snapshot: true,
},
},
/* eslint-enable */
});
7 changes: 6 additions & 1 deletion packages/babel-plugin-jest-hoist/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ const IDVisitor = {
) {
ids.add(path);
},
blacklist: ['TypeAnnotation', 'TSTypeAnnotation', 'TSTypeReference'],
blacklist: [
'TypeAnnotation',
'TSTypeAnnotation',
'TSTypeQuery',
'TSTypeReference',
],
};

const FUNCTIONS: Record<
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6168,6 +6168,7 @@ __metadata:
dependencies:
"@babel/core": ^7.11.6
"@babel/preset-react": ^7.12.1
"@babel/preset-typescript": ^7.0.0
"@babel/template": ^7.3.3
"@babel/types": ^7.3.3
"@types/babel__core": ^7.1.14
Expand Down

0 comments on commit 04dae6c

Please sign in to comment.