Skip to content

Commit d1fd158

Browse files
committed
POC: ts-test integration
1 parent 67a0872 commit d1fd158

File tree

4 files changed

+9
-61
lines changed

4 files changed

+9
-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 tsjest = 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 = tsjest;

packages/react-scripts/config/paths.js

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ module.exports = {
5858
appIndexJs: resolveApp('src/index.tsx'),
5959
appPackageJson: resolveApp('package.json'),
6060
appSrc: resolveApp('src'),
61+
appTsConfig: resolveApp('tsconfig.json'),
6162
yarnLockFile: resolveApp('yarn.lock'),
6263
testsSetup: resolveApp('src/setupTests.ts'),
6364
appNodeModules: resolveApp('node_modules'),
@@ -78,6 +79,7 @@ module.exports = {
7879
appIndexJs: resolveApp('src/index.tsx'),
7980
appPackageJson: resolveApp('package.json'),
8081
appSrc: resolveApp('src'),
82+
appTsConfig: resolveApp('tsconfig.json'),
8183
yarnLockFile: resolveApp('yarn.lock'),
8284
testsSetup: resolveApp('src/setupTests.ts'),
8385
appNodeModules: resolveApp('node_modules'),
@@ -107,6 +109,7 @@ if (
107109
appIndexJs: resolveOwn('template/src/index.tsx'),
108110
appPackageJson: resolveOwn('package.json'),
109111
appSrc: resolveOwn('template/src'),
112+
appTsConfig: resolveOwn('template/tsconfig.json'),
110113
yarnLockFile: resolveOwn('template/yarn.lock'),
111114
testsSetup: resolveOwn('template/src/setupTests.ts'),
112115
appNodeModules: resolveOwn('node_modules'),

packages/react-scripts/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"sass-loader": "6.0.5",
4848
"style-loader": "0.17.0",
4949
"ts-checker-webpack-plugin": "^0.1.1",
50+
"ts-jest": "^20.0.6",
5051
"ts-loader": "^2.0.3",
5152
"tslint": "^5.2.0",
5253
"tslint-loader": "^3.5.3",

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

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ module.exports = (resolve, rootDir) => {
2323
// TODO: I don't know if it's safe or not to just use / as path separator
2424
// in Jest configs. We need help from somebody with Windows to determine this.
2525
const config = {
26+
globals: {
27+
__TS_CONFIG__: paths.appTsConfig,
28+
},
2629
mapCoverage: true,
2730
collectCoverageFrom: [
2831
'src/**/*.{ts,tsx}',

0 commit comments

Comments
 (0)