Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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