Skip to content

Commit

Permalink
Merge pull request facebook#100 from DorianGrey/master
Browse files Browse the repository at this point in the history
Use ts-jest instead of custom solution for transforming ts files
  • Loading branch information
wmonk authored Jul 20, 2017
2 parents 12989de + 6fa3b6a commit 358b319
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 61 deletions.
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

0 comments on commit 358b319

Please sign in to comment.