Skip to content
This repository was archived by the owner on Jan 26, 2019. It is now read-only.

Use ts-jest instead of custom solution for transforming ts files #100

Merged
merged 1 commit into from
Jul 20, 2017
Merged
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
63 changes: 2 additions & 61 deletions packages/react-scripts/config/jest/typescriptTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 3 additions & 0 deletions packages/react-scripts/config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
};
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions packages/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions packages/react-scripts/scripts/utils/createJestConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down