Skip to content

Commit 125d06b

Browse files
authored
Merge pull request #652 from huafu/hotfix/babel-and-preprocess-bridges
[HOTFIX] babel and preprocess.js bridges
2 parents 6916e7b + 3068e73 commit 125d06b

15 files changed

Lines changed: 177 additions & 8115 deletions

File tree

package-lock.json

Lines changed: 0 additions & 8090 deletions
This file was deleted.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-jest",
3-
"version": "23.1.0",
3+
"version": "23.1.1",
44
"main": "dist/index.js",
55
"types": "./dist/index.d.ts",
66
"description": "A preprocessor with sourcemap support to help use Typescript with Jest",
@@ -81,16 +81,17 @@
8181
"@babel/core": "^7.0.0-beta.54",
8282
"@babel/preset-env": "^7.0.0-beta.54",
8383
"@types/babel-core": "^6.25.5",
84+
"@types/babel__core": "^7.0.1",
8485
"@types/es6-shim": "0.31.37",
8586
"@types/fs-extra": "5.0.4",
8687
"@types/jest": "^23.3.0",
8788
"@types/lodash": "^4.14.109",
8889
"@types/node": "10.5.5",
8990
"@types/react": "16.4.7",
9091
"@types/yargs": "^11.0.0",
92+
"babel-core": "^7.0.0-0",
9193
"babel-jest": "^23.4.0",
9294
"babel-preset-jest": "^23.2.0",
93-
"babel-core": "^7.0.0-0",
9495
"cpx": "^1.5.0",
9596
"cross-spawn": "latest",
9697
"cross-spawn-with-kill": "latest",
@@ -118,5 +119,8 @@
118119
"prettier --write --single-quote --trailing-comma all",
119120
"git add"
120121
]
122+
},
123+
"engines": {
124+
"node": ">= 6"
121125
}
122126
}

preprocessor.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
console.warn(
2+
`[ts-jest][DEPRECATED] - replace any occurrences of "ts-jest/dist/preprocessor.js" or ` +
3+
` "<rootDir>/node_modules/ts-jest/preprocessor.js"` +
4+
` in the 'transform' section of your Jest config with just "ts-jest".`
5+
);
6+
7+
module.exports = require('./dist');

scripts/tests.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ function createIntegrationMock() {
3939
path.resolve(testCaseModuleFolder, 'package.json')
4040
);
4141

42+
// TODO: remove this in next major version as well as the test, and the preprocessor.js file in root
43+
// Copy preprocessor.js
44+
fs.copySync(
45+
path.resolve(rootDir, 'preprocessor.js'),
46+
path.resolve(testCaseModuleFolder, 'preprocessor.js')
47+
);
48+
4249
// Copy dist folder
4350
fs.copySync(
4451
path.resolve(rootDir, 'dist'),

src/postprocess.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@
22
* Postprocess step. Based on babel-jest: https://github.com/facebook/jest/blob/master/packages/babel-jest/src/index.js
33
* https://github.com/facebook/jest/blob/9b157c3a7c325c3971b2aabbe4c8ab4ce0b0c56d/packages/babel-jest/src/index.js
44
*/
5-
import * as __types__babel from 'babel-core';
6-
import __types__istanbulPlugin from 'babel-plugin-istanbul';
7-
import * as __types__jestPreset from 'babel-preset-jest';
85
import {
96
BabelTransformOptions,
107
PostProcessHook,
11-
JestCacheKeyOptions,
8+
TBabel,
9+
TBabelPluginIstanbul,
10+
TBabelPresetJest,
1211
} from './types';
1312
import { logOnce } from './utils/logger';
1413
import getTSJestConfig from './utils/get-ts-jest-config';
14+
import {
15+
importBabelCore,
16+
importBabelPluginIstanbul,
17+
importBabelPresetJest,
18+
} from './utils/imports';
19+
import { TransformOptions } from '@babel/core';
1520

16-
let babel: typeof __types__babel;
17-
let istanbulPlugin: typeof __types__istanbulPlugin;
18-
let jestPreset: typeof __types__jestPreset;
21+
let babel: TBabel;
22+
let istanbulPlugin: TBabelPluginIstanbul;
23+
let jestPreset: TBabelPresetJest;
1924

2025
function importBabelDeps() {
2126
if (babel) return; // tslint:disable-line
2227
// we must use babel until we handle hoisting of jest.mock() internally
23-
babel = require('@babel/core');
24-
istanbulPlugin = require('babel-plugin-istanbul').default;
25-
jestPreset = require('babel-preset-jest');
28+
babel = importBabelCore();
29+
istanbulPlugin = importBabelPluginIstanbul();
30+
jestPreset = importBabelPresetJest();
2631
}
2732

2833
// Function that takes the transpiled typescript and runs it through babel/whatever.
@@ -62,13 +67,17 @@ function createBabelTransformer(
6267
codeSourcemapPair: jest.TransformedSource,
6368
filename: string,
6469
config: jest.ProjectConfig,
65-
transformOptions: JestCacheKeyOptions,
70+
transformOptions: jest.TransformOptions,
6671
): jest.TransformedSource => {
6772
const inputSourceMap =
6873
typeof codeSourcemapPair.map === 'string'
6974
? JSON.parse(codeSourcemapPair.map)
7075
: codeSourcemapPair.map;
71-
const theseOptions = { ...optionsBase, filename, inputSourceMap };
76+
const theseOptions = {
77+
...optionsBase,
78+
filename,
79+
inputSourceMap,
80+
} as TransformOptions;
7281
if (transformOptions && transformOptions.instrument) {
7382
theseOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
7483
// Copied from jest-runtime transform.js

src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { TransformOptions as BabelTransformOpts } from 'babel-core';
22

3+
import * as _babel from '@babel/core';
4+
import * as _babelEnv from '@babel/preset-env';
5+
import * as _babelJest from 'babel-preset-jest';
6+
import _babelIstanbul from 'babel-plugin-istanbul';
7+
8+
export type TBabel = typeof _babel;
9+
export type TBabelPluginIstanbul = typeof _babelIstanbul;
10+
export type TBabelPresetEnv = typeof _babelEnv;
11+
export type TBabelPresetJest = typeof _babelJest;
12+
313
export interface JestCacheKeyOptions {
414
rootDir: string;
515
instrument: boolean;

src/utils/get-ts-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function findTSConfigPath(
109109
`${jestConfig.rootDir}${sep}`,
110110
);
111111
// ensure the path is resolved
112-
if (!tsConfigFile.startsWith('/')) {
112+
if (!tsConfigFile.startsWith('/') && jestConfig.rootDir) {
113113
tsConfigFile = resolve(jestConfig.rootDir, tsConfigFile);
114114
} else {
115115
tsConfigFile = resolve(tsConfigFile);

src/utils/imports.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {
2+
TBabel,
3+
TBabelPresetEnv,
4+
TBabelPluginIstanbul,
5+
TBabelPresetJest,
6+
} from '../types';
7+
8+
export function importBabelCore(): TBabel {
9+
const mod = tryRequire('@babel/core') || tryRequire('babel-core');
10+
if (!mod) {
11+
throw new Error(
12+
`[ts-jest] You must install the '@babel/core' or 'babel-core'` +
13+
` package (depending on the version you want to use).`,
14+
);
15+
}
16+
return mod;
17+
}
18+
19+
export function importBabelPresetEnv(): TBabelPresetEnv {
20+
const mod = tryRequire('@babel/preset-env') || tryRequire('babel-preset-env');
21+
if (!mod) {
22+
throw new Error( // babel-jest has the env preset as a dep
23+
`[ts-jest] You must install the 'babel-jest' package if you're using babel.`,
24+
);
25+
}
26+
return mod;
27+
}
28+
29+
export function importBabelPresetJest(): TBabelPresetJest {
30+
const mod = tryRequire('babel-preset-jest');
31+
if (!mod) {
32+
throw new Error( // babel-jest has the jest preset as a dep
33+
`[ts-jest] You must install the 'babel-jest' package if you're using babel.`,
34+
);
35+
}
36+
return mod;
37+
}
38+
39+
export function importBabelPluginIstanbul(): TBabelPluginIstanbul {
40+
const mod = tryRequire('babel-plugin-istanbul');
41+
if (!mod) {
42+
throw new Error( // babel-jest has the istanbul plugin as a dep
43+
`[ts-jest] You must install the 'babel-jest' package if you're using babel.`,
44+
);
45+
}
46+
return mod.default;
47+
}
48+
49+
function tryRequire<T = any>(packageName: string): T | void {
50+
let mod: T;
51+
try {
52+
mod = require(packageName);
53+
} catch (err) {
54+
if (err.code !== 'ENOENT') throw err; // tslint:disable-line
55+
}
56+
return mod;
57+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Deprecated Jest transform value should log the depration message 1`] = `"[ts-jest][DEPRECATED] - replace any occurrences of \\"ts-jest/dist/preprocessor.js\\" or \\"<rootDir>/node_modules/ts-jest/preprocessor.js\\" in the 'transform' section of your Jest config with just \\"ts-jest\\"."`;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import runJest from '../__helpers__/runJest';
2+
3+
describe('Deprecated Jest transform value', () => {
4+
it('should log the depration message', () => {
5+
const result = runJest('../deprecated-transform', ['--no-cache']);
6+
7+
const output = result.stderr;
8+
9+
// get only the line with deprecation message (we dont want the snapshot to contain any timing or test name)
10+
const msg = output.split('\n').find(line => /deprecated/i.test(line));
11+
12+
expect(msg).toMatchSnapshot();
13+
});
14+
});

0 commit comments

Comments
 (0)