From 6fa3b6ae49d9f3a0b1e65cce62f533bc7bd1821b Mon Sep 17 00:00:00 2001 From: DorianGrey Date: Wed, 5 Jul 2017 15:52:08 +0200 Subject: [PATCH] Use ts-jest instead of custom solution for transforming ts files. --- .../config/jest/typescriptTransform.js | 63 +------------------ packages/react-scripts/config/paths.js | 3 + packages/react-scripts/package.json | 1 + .../scripts/utils/createJestConfig.js | 5 ++ 4 files changed, 11 insertions(+), 61 deletions(-) diff --git a/packages/react-scripts/config/jest/typescriptTransform.js b/packages/react-scripts/config/jest/typescriptTransform.js index 18aa9351e..9b138ac8e 100644 --- a/packages/react-scripts/config/jest/typescriptTransform.js +++ b/packages/react-scripts/config/jest/typescriptTransform.js @@ -2,65 +2,6 @@ 'use strict'; -const fs = require('fs'); -const crypto = require('crypto'); -const tsc = require('typescript'); -const tsconfigPath = require('app-root-path').resolve('/tsconfig.json'); -const THIS_FILE = fs.readFileSync(__filename); +const tsJestPreprocessor = require('ts-jest/preprocessor'); -let compilerConfig = { - module: tsc.ModuleKind.CommonJS, - jsx: tsc.JsxEmit.React, -}; - -if (fs.existsSync(tsconfigPath)) { - try { - const tsconfig = tsc.readConfigFile(tsconfigPath).config; - - if (tsconfig && tsconfig.compilerOptions) { - compilerConfig = tsconfig.compilerOptions; - } - } catch (e) { - /* Do nothing - default is set */ - } -} - -module.exports = { - process(src, path, config, options) { - if (path.endsWith('.ts') || path.endsWith('.tsx')) { - let compilerOptions = compilerConfig; - if (options.instrument) { - // inline source with source map for remapping coverage - compilerOptions = Object.assign({}, compilerConfig); - delete compilerOptions.sourceMap; - compilerOptions.inlineSourceMap = true; - compilerOptions.inlineSources = true; - // fix broken paths in coverage report if `.outDir` is set - delete compilerOptions.outDir; - } - - const tsTranspiled = tsc.transpileModule(src, { - compilerOptions: compilerOptions, - fileName: path, - }); - return tsTranspiled.outputText; - } - return src; - }, - getCacheKey(fileData, filePath, configStr, options) { - return crypto - .createHash('md5') - .update(THIS_FILE) - .update('\0', 'utf8') - .update(fileData) - .update('\0', 'utf8') - .update(filePath) - .update('\0', 'utf8') - .update(configStr) - .update('\0', 'utf8') - .update(JSON.stringify(compilerConfig)) - .update('\0', 'utf8') - .update(options.instrument ? 'instrument' : '') - .digest('hex'); - }, -}; +module.exports = tsJestPreprocessor; diff --git a/packages/react-scripts/config/paths.js b/packages/react-scripts/config/paths.js index cb125d207..b52ea6b82 100644 --- a/packages/react-scripts/config/paths.js +++ b/packages/react-scripts/config/paths.js @@ -60,6 +60,7 @@ module.exports = { yarnLockFile: resolveApp('yarn.lock'), testsSetup: resolveApp('src/setupTests.ts'), appNodeModules: resolveApp('node_modules'), + appTsConfig: resolveApp('tsconfig.json'), publicUrl: getPublicUrl(resolveApp('package.json')), servedPath: getServedPath(resolveApp('package.json')), }; @@ -80,6 +81,7 @@ module.exports = { yarnLockFile: resolveApp('yarn.lock'), testsSetup: resolveApp('src/setupTests.ts'), appNodeModules: resolveApp('node_modules'), + appTsConfig: resolveApp('tsconfig.json'), publicUrl: getPublicUrl(resolveApp('package.json')), servedPath: getServedPath(resolveApp('package.json')), // These properties only exist before ejecting: @@ -109,6 +111,7 @@ if ( yarnLockFile: resolveOwn('template/yarn.lock'), testsSetup: resolveOwn('template/src/setupTests.ts'), appNodeModules: resolveOwn('node_modules'), + appTsConfig: resolveOwn('template/tsconfig.json'), publicUrl: getPublicUrl(resolveOwn('package.json')), servedPath: getServedPath(resolveOwn('package.json')), // These properties only exist before ejecting: diff --git a/packages/react-scripts/package.json b/packages/react-scripts/package.json index e21bbfb30..e0aa96121 100644 --- a/packages/react-scripts/package.json +++ b/packages/react-scripts/package.json @@ -41,6 +41,7 @@ "react-dev-utils": "^2.0.1", "react-error-overlay": "^1.0.6", "style-loader": "0.17.0", + "ts-jest": "^20.0.7", "ts-loader": "^2.2.1", "tslint": "^5.2.0", "tslint-loader": "^3.5.3", diff --git a/packages/react-scripts/scripts/utils/createJestConfig.js b/packages/react-scripts/scripts/utils/createJestConfig.js index 5625a3da7..9063be329 100644 --- a/packages/react-scripts/scripts/utils/createJestConfig.js +++ b/packages/react-scripts/scripts/utils/createJestConfig.js @@ -45,6 +45,11 @@ module.exports = (resolve, rootDir) => { moduleNameMapper: { '^react-native$': 'react-native-web', }, + globals: { + 'ts-jest': { + tsConfigFile: paths.appTsConfig, + }, + }, }; if (rootDir) { config.rootDir = rootDir;