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

babel-plugin-jest-hoist: Ignore TS type annotations when looking for out-of-scope references #7641

Merged
merged 5 commits into from
Jan 16, 2019
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
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