Skip to content

Commit 358b319

Browse files
authored
Merge pull request facebook#100 from DorianGrey/master
Use ts-jest instead of custom solution for transforming ts files
2 parents 12989de + 6fa3b6a commit 358b319

File tree

4 files changed

+11
-61
lines changed

4 files changed

+11
-61
lines changed

packages/react-scripts/config/jest/typescriptTransform.js

+2-61
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,6 @@
22

33
'use strict';
44

5-
const fs = require('fs');
6-
const crypto = require('crypto');
7-
const tsc = require('typescript');
8-
const tsconfigPath = require('app-root-path').resolve('/tsconfig.json');
9-
const THIS_FILE = fs.readFileSync(__filename);
5+
const tsJestPreprocessor = require('ts-jest/preprocessor');
106

11-
let compilerConfig = {
12-
module: tsc.ModuleKind.CommonJS,
13-
jsx: tsc.JsxEmit.React,
14-
};
15-
16-
if (fs.existsSync(tsconfigPath)) {
17-
try {
18-
const tsconfig = tsc.readConfigFile(tsconfigPath).config;
19-
20-
if (tsconfig && tsconfig.compilerOptions) {
21-
compilerConfig = tsconfig.compilerOptions;
22-
}
23-
} catch (e) {
24-
/* Do nothing - default is set */
25-
}
26-
}
27-
28-
module.exports = {
29-
process(src, path, config, options) {
30-
if (path.endsWith('.ts') || path.endsWith('.tsx')) {
31-
let compilerOptions = compilerConfig;
32-
if (options.instrument) {
33-
// inline source with source map for remapping coverage
34-
compilerOptions = Object.assign({}, compilerConfig);
35-
delete compilerOptions.sourceMap;
36-
compilerOptions.inlineSourceMap = true;
37-
compilerOptions.inlineSources = true;
38-
// fix broken paths in coverage report if `.outDir` is set
39-
delete compilerOptions.outDir;
40-
}
41-
42-
const tsTranspiled = tsc.transpileModule(src, {
43-
compilerOptions: compilerOptions,
44-
fileName: path,
45-
});
46-
return tsTranspiled.outputText;
47-
}
48-
return src;
49-
},
50-
getCacheKey(fileData, filePath, configStr, options) {
51-
return crypto
52-
.createHash('md5')
53-
.update(THIS_FILE)
54-
.update('\0', 'utf8')
55-
.update(fileData)
56-
.update('\0', 'utf8')
57-
.update(filePath)
58-
.update('\0', 'utf8')
59-
.update(configStr)
60-
.update('\0', 'utf8')
61-
.update(JSON.stringify(compilerConfig))
62-
.update('\0', 'utf8')
63-
.update(options.instrument ? 'instrument' : '')
64-
.digest('hex');
65-
},
66-
};
7+
module.exports = tsJestPreprocessor;

packages/react-scripts/config/paths.js

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ module.exports = {
6060
yarnLockFile: resolveApp('yarn.lock'),
6161
testsSetup: resolveApp('src/setupTests.ts'),
6262
appNodeModules: resolveApp('node_modules'),
63+
appTsConfig: resolveApp('tsconfig.json'),
6364
publicUrl: getPublicUrl(resolveApp('package.json')),
6465
servedPath: getServedPath(resolveApp('package.json')),
6566
};
@@ -80,6 +81,7 @@ module.exports = {
8081
yarnLockFile: resolveApp('yarn.lock'),
8182
testsSetup: resolveApp('src/setupTests.ts'),
8283
appNodeModules: resolveApp('node_modules'),
84+
appTsConfig: resolveApp('tsconfig.json'),
8385
publicUrl: getPublicUrl(resolveApp('package.json')),
8486
servedPath: getServedPath(resolveApp('package.json')),
8587
// These properties only exist before ejecting:
@@ -109,6 +111,7 @@ if (
109111
yarnLockFile: resolveOwn('template/yarn.lock'),
110112
testsSetup: resolveOwn('template/src/setupTests.ts'),
111113
appNodeModules: resolveOwn('node_modules'),
114+
appTsConfig: resolveOwn('template/tsconfig.json'),
112115
publicUrl: getPublicUrl(resolveOwn('package.json')),
113116
servedPath: getServedPath(resolveOwn('package.json')),
114117
// These properties only exist before ejecting:

packages/react-scripts/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"react-dev-utils": "^2.0.1",
4242
"react-error-overlay": "^1.0.6",
4343
"style-loader": "0.17.0",
44+
"ts-jest": "^20.0.7",
4445
"ts-loader": "^2.2.1",
4546
"tslint": "^5.2.0",
4647
"tslint-loader": "^3.5.3",

packages/react-scripts/scripts/utils/createJestConfig.js

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ module.exports = (resolve, rootDir) => {
4545
moduleNameMapper: {
4646
'^react-native$': 'react-native-web',
4747
},
48+
globals: {
49+
'ts-jest': {
50+
tsConfigFile: paths.appTsConfig,
51+
},
52+
},
4853
};
4954
if (rootDir) {
5055
config.rootDir = rootDir;

0 commit comments

Comments
 (0)