From e7c90d0e822dc748120b74d49ab378b404923fec Mon Sep 17 00:00:00 2001 From: lgandecki Date: Sat, 12 Jan 2019 15:44:06 +0400 Subject: [PATCH 1/2] ignore imports of types for typescript --- e2e/__tests__/findRelatedFiles.test.js | 37 ++++++++++++++++++++++++++ packages/jest-haste-map/package.json | 1 + packages/jest-haste-map/src/worker.js | 25 ++++++++++++++--- yarn.lock | 17 +++++++++++- 4 files changed, 76 insertions(+), 4 deletions(-) diff --git a/e2e/__tests__/findRelatedFiles.test.js b/e2e/__tests__/findRelatedFiles.test.js index 98fa91b5523f..0729586e88ba 100644 --- a/e2e/__tests__/findRelatedFiles.test.js +++ b/e2e/__tests__/findRelatedFiles.test.js @@ -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': '', diff --git a/packages/jest-haste-map/package.json b/packages/jest-haste-map/package.json index 2588f476c89c..cedb32df382b 100644 --- a/packages/jest-haste-map/package.json +++ b/packages/jest-haste-map/package.json @@ -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", diff --git a/packages/jest-haste-map/src/worker.js b/packages/jest-haste-map/src/worker.js index 1fb4562caacd..0ff14bccb354 100644 --- a/packages/jest-haste-map/src/worker.js +++ b/packages/jest-haste-map/src/worker.js @@ -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'; @@ -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; @@ -49,18 +51,35 @@ export async function worker(data: WorkerMessage): Promise { 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); diff --git a/yarn.lock b/yarn.lock index 74e169452fa9..c1ca9466f161 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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= -diff@3.5.0, diff@^3.2.0: +diff@3.5.0: version "3.5.0" resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12" integrity sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA== From a91324af355504ff2505d88d0bf4c5f207785b30 Mon Sep 17 00:00:00 2001 From: lgandecki Date: Sat, 12 Jan 2019 20:06:34 +0400 Subject: [PATCH 2/2] pass the tests on all environments --- e2e/__tests__/findRelatedFiles.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/__tests__/findRelatedFiles.test.js b/e2e/__tests__/findRelatedFiles.test.js index 0729586e88ba..76f9beca0e9a 100644 --- a/e2e/__tests__/findRelatedFiles.test.js +++ b/e2e/__tests__/findRelatedFiles.test.js @@ -117,7 +117,7 @@ describe('--findRelatedTests flag', () => { }); const {stderr} = runJest(DIR, ['--findRelatedTests', 'c.js']); - expect(stderr).toMatch('SyntaxError: Unexpected token {'); + expect(stderr).toMatch('SyntaxError: Unexpected token'); }); test('generates coverage report for filename', () => {