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

Ignore imports of types with typescript #7614

Closed
Closed
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
37 changes: 37 additions & 0 deletions e2e/__tests__/findRelatedFiles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,43 @@ describe('--findRelatedTests flag', () => {
expect(stderr).toMatch(summaryMsg);
});

test('ignores imports of types for typescript', () => {
writeFiles(DIR, {
'.watchmanconfig': '',
'__tests__/test.test.ts': `
import {SomeClassType} from "../c";
test('a', () => {});
`,
'c.ts': `
class SomeClass {}
export type SomeClassType = SomeClass;
`,
'package.json': JSON.stringify({jest: {testEnvironment: 'node'}}),
});

const {stderr, stdout} = runJest(DIR, ['--findRelatedTests', 'c.ts']);
expect(stdout).toMatch('Pattern: c.ts - 0 matches');
expect(stderr).toEqual('');
});

test('skips typescript type stripping for .js files', () => {
writeFiles(DIR, {
'.watchmanconfig': '',
'__tests__/test.test.js': `
import {SomeClassType} from "../c";
test('a', () => {});
`,
'c.js': `
class SomeClass {}
export type SomeClassType = SomeClass;
`,
'package.json': JSON.stringify({jest: {testEnvironment: 'node'}}),
});

const {stderr} = runJest(DIR, ['--findRelatedTests', 'c.js']);
expect(stderr).toMatch('SyntaxError: Unexpected token');
});

test('generates coverage report for filename', () => {
writeFiles(DIR, {
'.watchmanconfig': '',
Expand Down
1 change: 1 addition & 0 deletions packages/jest-haste-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"license": "MIT",
"main": "build/index.js",
"dependencies": {
"@babel/plugin-transform-typescript": "^7.2.0",
"fb-watchman": "^2.0.0",
"graceful-fs": "^4.1.15",
"invariant": "^2.2.4",
Expand Down
25 changes: 22 additions & 3 deletions packages/jest-haste-map/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import type {HasteImpl, WorkerMessage, WorkerMetadata} from './types';

import * as babel from '@babel/core';
import crypto from 'crypto';
import path from 'path';
import fs from 'graceful-fs';
Expand All @@ -17,6 +18,7 @@ import H from './constants';
import * as dependencyExtractor from './lib/dependencyExtractor';

const PACKAGE_JSON = path.sep + 'package.json';
const TYPESCRIPT_EXTENSION = '.ts';

let hasteImpl: ?HasteImpl = null;
let hasteImplModulePath: ?string = null;
Expand Down Expand Up @@ -49,18 +51,35 @@ export async function worker(data: WorkerMessage): Promise<WorkerMetadata> {

const {computeDependencies, computeSha1, rootDir, filePath} = data;

const getContent = (): string => {
const getJSONContent = (): string => {
if (content === undefined) {
content = fs.readFileSync(filePath, 'utf8');
}

return content;
};
const getContent = (): string => {
if (content === undefined) {
if (filePath.endsWith(TYPESCRIPT_EXTENSION)) {
try {
const transformed = babel.transformFileSync(filePath, {
cwd: __dirname,
plugins: ['@babel/plugin-transform-typescript'],
});
content = transformed.code;
} catch (e) {
content = fs.readFileSync(filePath, 'utf8');
}
} else {
content = fs.readFileSync(filePath, 'utf8');
}
}
return content;
};

if (filePath.endsWith(PACKAGE_JSON)) {
// Process a package.json that is returned as a PACKAGE type with its name.
try {
const fileData = JSON.parse(getContent());
const fileData = JSON.parse(getJSONContent());

if (fileData.name) {
const relativeFilePath = path.relative(rootDir, filePath);
Expand Down
17 changes: 16 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-syntax-typescript@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-typescript/-/plugin-syntax-typescript-7.2.0.tgz#55d240536bd314dcbbec70fd949c5cabaed1de29"
integrity sha512-WhKr6yu6yGpGcNMVgIBuI9MkredpVc7Y3YR4UzEZmDztHoL6wV56YBHLhWnjO1EvId1B32HrD3DRFc+zSoKI1g==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-transform-arrow-functions@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-arrow-functions/-/plugin-transform-arrow-functions-7.0.0.tgz#a6c14875848c68a3b4b3163a486535ef25c7e749"
Expand Down Expand Up @@ -692,6 +699,14 @@
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-typescript" "^7.0.0"

"@babel/plugin-transform-typescript@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-typescript/-/plugin-transform-typescript-7.2.0.tgz#bce7c06300434de6a860ae8acf6a442ef74a99d1"
integrity sha512-EnI7i2/gJ7ZNr2MuyvN2Hu+BHJENlxWte5XygPvfj/MbvtOkWor9zcnHpMMQL2YYaaCcqtIvJUyJ7QVfoGs7ew==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-typescript" "^7.2.0"

"@babel/plugin-transform-unicode-regex@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-unicode-regex/-/plugin-transform-unicode-regex-7.0.0.tgz#c6780e5b1863a76fe792d90eded9fcd5b51d68fc"
Expand Down Expand Up @@ -4503,7 +4518,7 @@ diacritics-map@^0.1.0:
resolved "https://registry.yarnpkg.com/diacritics-map/-/diacritics-map-0.1.0.tgz#6dfc0ff9d01000a2edf2865371cac316e94977af"
integrity sha1-bfwP+dAQAKLt8oZTccrDFulJd68=

[email protected], diff@^3.2.0:
[email protected]:
version "3.5.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"
integrity sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==
Expand Down