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

fix(babel-hoist-plugin): ignore TS type queries #13367

Merged
merged 4 commits into from Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
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
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
@@ -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
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
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
Expand Up @@ -6162,6 +6162,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