|
6 | 6 | * of patent rights can be found in the PATENTS file in the same directory.
|
7 | 7 | */
|
8 | 8 |
|
| 9 | +// This file is a merger between the original transform.js and ts-jest/dist/preprocessor.js (https://github.com/kulshekhar/ts-jest/blob/e1f95e524ed62091736f70abf63530f1f107ec03/src/preprocessor.ts) |
| 10 | +// The preprocessor from ts-jest could not be used directly, |
| 11 | +// because it did not use babel and |
| 12 | +// could not get configuration from the right place (../utils/createJestConfig.js) |
| 13 | +// instead it was retrieved from of argv which was incomplete |
| 14 | + |
9 | 15 | const babelJest = require('babel-jest');
|
10 | 16 | const tsc = require('typescript');
|
| 17 | +const glob = require('glob-all'); |
| 18 | +const nodepath = require('path'); |
| 19 | +const tsJestUtils = require('ts-jest/dist/utils'); |
| 20 | +const getPackageRoot = require('jest-util').getPackageRoot; |
| 21 | +const root = getPackageRoot(); |
11 | 22 |
|
12 | 23 | const babelTransformer = babelJest.createTransformer({
|
13 | 24 | presets: [require.resolve('babel-preset-react-app')]
|
14 | 25 | });
|
15 | 26 |
|
16 |
| -// TODO load tsconfig.json in created app instead of duplicating tsconfig.compilerOptions here |
17 |
| -const compilerOptions = { |
18 |
| - // Overwrite module |
19 |
| - // Jest gives `SyntaxError: Unexpected token import` error when ES6 module are emitted |
20 |
| - // module: tsc.ModuleKind.ES6, |
21 |
| - module: tsc.ModuleKind.CommonJS, |
22 |
| - // Overwrite jsx |
23 |
| - // Expected Babel transformer to convert jsx to js |
24 |
| - // but Jest gives `SyntaxError: Unexpected token <` error when set to Preserve |
25 |
| - // jsx: tsc.JsxEmit.Preserve, |
26 |
| - jsx: tsc.JsxEmit.React, |
27 |
| - target: tsc.ScriptTarget.ES6, |
28 |
| - moduleResolution: tsc.ModuleResolutionKind.NodeJs, |
29 |
| -}; |
| 27 | +function initializeCache(config) { |
| 28 | + const collectCoverage = config.collectCoverage; |
| 29 | + const coverageDirectory = config.coverageDirectory; |
| 30 | + const coverageReporters = config.coverageReporters; |
| 31 | + const collectCoverageFrom = config.collectCoverageFrom; |
| 32 | + const testResultsProcessor = config.testResultsProcessor; |
| 33 | + global.__ts_coverage__cache__ = {}; |
| 34 | + global.__ts_coverage__cache__.sourceCache = {}; |
| 35 | + global.__ts_coverage__cache__.coverageConfig = { collectCoverage: collectCoverage, coverageDirectory: coverageDirectory, coverageReporters: coverageReporters }; |
| 36 | + global.__ts_coverage__cache__.coverageCollectFiles = |
| 37 | + collectCoverage && |
| 38 | + testResultsProcessor && |
| 39 | + collectCoverageFrom && |
| 40 | + collectCoverageFrom.length ? |
| 41 | + glob.sync(collectCoverageFrom).map(function (x) { return nodepath.resolve(root, x); }) : []; |
| 42 | +} |
| 43 | + |
| 44 | +function tsProcess(src, path, config) { |
| 45 | + if (path.endsWith('.ts') || path.endsWith('.tsx')) { |
| 46 | + if (config.testResultsProcessor && !global.__ts_coverage__cache__) { |
| 47 | + // initialize only once |
| 48 | + initializeCache(config); |
| 49 | + } |
| 50 | + var transpiled = tsc.transpileModule(src, { |
| 51 | + compilerOptions: tsJestUtils.getTSConfig(config.globals, config.collectCoverage), |
| 52 | + fileName: path |
| 53 | + }); |
| 54 | + if (global.__ts_coverage__cache__) { |
| 55 | + if (!config.testRegex || !path.match(config.testRegex)) { |
| 56 | + global.__ts_coverage__cache__.sourceCache[path] = transpiled.outputText; |
| 57 | + } |
| 58 | + } |
| 59 | + var modified = "require('ts-jest').install();" + transpiled.outputText; |
| 60 | + return modified; |
| 61 | + } |
| 62 | + return src; |
| 63 | +} |
30 | 64 |
|
31 | 65 | // transpile the source with TypeScript, if needed, and then with Babel
|
32 | 66 | module.exports = {
|
33 |
| - process(src, path) { |
34 |
| - if (path.endsWith('.ts') || path.endsWith('.tsx')) { |
35 |
| - src = tsc.transpile( |
36 |
| - src, |
37 |
| - compilerOptions, |
38 |
| - path, |
39 |
| - [] |
40 |
| - ); |
41 |
| - } |
| 67 | + process(src, path, config) { |
| 68 | + src = tsProcess(src, path, config); |
42 | 69 | return babelTransformer.process(src, path);
|
43 | 70 | },
|
44 | 71 | };
|
0 commit comments