Skip to content

Commit

Permalink
babel-plugin-jest-hoist: Ignore TS type annotations when looking for …
Browse files Browse the repository at this point in the history
…out-of-scope references (jestjs#7641)
  • Loading branch information
jeysal authored and captain-yossarian committed Jul 18, 2019
1 parent 2a997ad commit 60549cc
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 203 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
- `[jest-snapshot]` Write and read snapshots from disk even if `fs` is mocked ([#7080](https://github.com/facebook/jest/pull/7080))
- `[jest-config]` Normalize `config.cwd` and `config.rootDir` using `realpath ([#7598](https://github.com/facebook/jest/pull/7598))
- `[jest-environment-node]` Fix buffer property is not ArrayBuffer issue. ([#7626](https://github.com/facebook/jest/pull/7626))
- `[babel-plugin-jest-hoist]` Ignore TS type annotations when looking for out-of-scope references ([#7641](https://github.com/facebook/jest/pull/7641))

### Chore & Maintenance

Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/babelPluginJestHoist.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ beforeEach(() => {
it('sucessfully runs the tests inside `babel-plugin-jest-hoist/`', () => {
const {json} = runWithJson(DIR, ['--no-cache', '--coverage']);
expect(json.success).toBe(true);
expect(json.numTotalTestSuites).toBe(2);
expect(json.numTotalTestSuites).toBe(3);
});
24 changes: 24 additions & 0 deletions e2e/babel-plugin-jest-hoist/__tests__/typescript.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+jsinfra
*/

/* eslint-disable import/no-unresolved */

import {Color} from '../types';
import {color} from '../entry';

jest.mock('../entry', () => {
const color: Color = 'blue';
return {color};
});

describe('babel-plugin-jest-hoist', () => {
it('works even with type imports', () => {
expect(color).toBe('blue');
});
});
12 changes: 11 additions & 1 deletion e2e/babel-plugin-jest-hoist/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.

module.exports = {
overrides: [
{
presets: ['@babel/preset-flow'],
test: '**/*.js',
},
{
presets: ['@babel/preset-typescript'],
test: '**/*.ts',
},
],
plugins: ['jest-hoist'],
presets: ['@babel/preset-env', '@babel/preset-flow'],
presets: ['@babel/preset-env'],
};
11 changes: 11 additions & 0 deletions e2e/babel-plugin-jest-hoist/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// eslint-disable-next-line import/no-unresolved
import {Color} from './types';

export const color: Color = 'red';
5 changes: 3 additions & 2 deletions e2e/babel-plugin-jest-hoist/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"dependencies": {
"@babel/preset-env": "7.0.0",
"@babel/preset-flow": "7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-typescript": "^7.0.0",
"react": "*"
},
"jest": {
Expand Down
8 changes: 8 additions & 0 deletions e2e/babel-plugin-jest-hoist/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

export type Color = 'red' | 'blue';
419 changes: 221 additions & 198 deletions e2e/babel-plugin-jest-hoist/yarn.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/babel-plugin-jest-hoist/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const IDVisitor = {
ReferencedIdentifier(path) {
this.ids.add(path);
},
blacklist: ['TypeAnnotation'],
blacklist: ['TypeAnnotation', 'TSTypeAnnotation'],
};

const FUNCTIONS: Object = Object.create(null);
Expand Down

0 comments on commit 60549cc

Please sign in to comment.