Skip to content

Commit 544984e

Browse files
authored
feat: create new @jest/transform package (#7915)
1 parent e760ec4 commit 544984e

21 files changed

+140
-86
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
- `[jest-resolve]`: Migrate to TypeScript ([#7871](https://github.com/facebook/jest/pull/7871))
4141
- `[@jest/reporter]`: New package extracted from `jest-cli` ([#7902](https://github.com/facebook/jest/pull/7902))
4242
- `[jest-snapshot]`: Migrate to TypeScript ([#7899](https://github.com/facebook/jest/pull/7899))
43+
- `[@jest/transform]`: New package extracted from `jest-runtime` ([#7915](https://github.com/facebook/jest/pull/7915))
4344

4445
### Performance
4546

packages/jest-cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "build/jest.js",
66
"dependencies": {
77
"@jest/reporters": "^24.1.0",
8+
"@jest/transform": "^24.1.0",
89
"ansi-escapes": "^3.0.0",
910
"chalk": "^2.0.1",
1011
"exit": "^0.1.2",

packages/jest-cli/src/runGlobalHook.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {Test} from 'types/TestRunner';
1313
import {extname} from 'path';
1414
import pEachSeries from 'p-each-series';
1515
import {addHook} from 'pirates';
16-
import Runtime from 'jest-runtime';
16+
import {ScriptTransformer} from '@jest/transform';
1717

1818
// copied from https://github.com/babel/babel/blob/56044c7851d583d498f919e9546caddf8f80a72f/packages/babel-helpers/src/helpers.js#L558-L562
1919
function _interopRequireDefault(obj) {
@@ -52,7 +52,7 @@ export default ({
5252
: // Fallback to first config
5353
allTests[0].context.config;
5454

55-
const transformer = new Runtime.ScriptTransformer(projectConfig);
55+
const transformer = new ScriptTransformer(projectConfig);
5656

5757
// Load the transformer to avoid a cycle where we need to load a
5858
// transformer in order to transform it in the require hooks

packages/jest-reporters/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"version": "24.1.0",
55
"main": "build/index.js",
66
"dependencies": {
7+
"@jest/transform": "^24.1.0",
78
"chalk": "^2.0.1",
89
"exit": "^0.1.2",
910
"glob": "^7.1.2",
10-
"jest-runtime": "^24.1.0",
1111
"istanbul-api": "^2.1.1",
1212
"istanbul-lib-coverage": "^2.0.2",
1313
"istanbul-lib-instrument": "^3.0.1",

packages/jest-reporters/src/__tests__/generateEmptyCoverage.test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
*
77
* @flow
88
*/
9-
'use strict';
109

1110
import istanbulCoverage from 'istanbul-lib-coverage';
1211
import libSourceMaps from 'istanbul-lib-source-maps';
1312
import generateEmptyCoverage from '../generateEmptyCoverage';
14-
import Runtime from 'jest-runtime';
1513

16-
const path = require('path');
17-
const os = require('os');
18-
const {makeGlobalConfig, makeProjectConfig} = require('../../../../TestUtils');
14+
import path from 'path';
15+
import os from 'os';
16+
import {makeGlobalConfig, makeProjectConfig} from '../../../../TestUtils';
1917

20-
jest.spyOn(Runtime, 'shouldInstrument').mockImplementation(() => true);
18+
jest.mock('@jest/transform', () => ({
19+
...jest.requireActual('@jest/transform'),
20+
shouldInstrument: () => true,
21+
}));
2122

2223
const src = `
2324
throw new Error('this should not be thrown');

packages/jest-reporters/src/generateEmptyCoverage.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {GlobalConfig, ProjectConfig, Path} from 'types/Config';
1111

1212
import {readInitialCoverage} from 'istanbul-lib-instrument';
1313
import {classes} from 'istanbul-lib-coverage';
14-
import Runtime from 'jest-runtime';
14+
import {shouldInstrument, ScriptTransformer} from '@jest/transform';
1515

1616
export type CoverageWorkerResult = {|
1717
coverage: any,
@@ -33,9 +33,9 @@ export default function(
3333
collectCoverageFrom: globalConfig.collectCoverageFrom,
3434
collectCoverageOnlyFrom: globalConfig.collectCoverageOnlyFrom,
3535
};
36-
if (Runtime.shouldInstrument(filename, coverageOptions, config)) {
36+
if (shouldInstrument(filename, coverageOptions, config)) {
3737
// Transform file with instrumentation to make sure initial coverage data is well mapped to original code.
38-
const {code, mapCoverage, sourceMapPath} = new Runtime.ScriptTransformer(
38+
const {code, mapCoverage, sourceMapPath} = new ScriptTransformer(
3939
config,
4040
).transformSource(filename, source, true);
4141
const extracted = readInitialCoverage(code);

packages/jest-runtime/package.json

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@
99
"license": "MIT",
1010
"main": "build/index.js",
1111
"dependencies": {
12-
"@babel/core": "^7.1.0",
13-
"babel-plugin-istanbul": "^5.1.0",
12+
"@jest/transform": "^24.1.0",
1413
"chalk": "^2.0.1",
15-
"convert-source-map": "^1.4.0",
1614
"exit": "^0.1.2",
17-
"fast-json-stable-stringify": "^2.0.0",
1815
"glob": "^7.1.3",
1916
"graceful-fs": "^4.1.15",
2017
"jest-config": "^24.1.0",
@@ -25,24 +22,18 @@
2522
"jest-snapshot": "^24.1.0",
2623
"jest-util": "^24.0.0",
2724
"jest-validate": "^24.0.0",
28-
"micromatch": "^3.1.10",
2925
"realpath-native": "^1.1.0",
3026
"slash": "^2.0.0",
3127
"strip-bom": "^3.0.0",
32-
"write-file-atomic": "2.4.1",
3328
"yargs": "^12.0.2"
3429
},
3530
"devDependencies": {
36-
"@types/babel__core": "^7.0.4",
37-
"@types/convert-source-map": "^1.5.1",
3831
"@types/exit": "^0.1.30",
3932
"@types/glob": "^7.1.1",
4033
"@types/graceful-fs": "^4.1.2",
41-
"@types/micromatch": "^3.1.0",
42-
"@types/strip-bom": "3.0.0",
43-
"@types/write-file-atomic": "^2.1.1",
34+
"@types/slash": "^2.0.0",
35+
"@types/strip-bom": "^3.0.0",
4436
"@types/yargs": "^12.0.2",
45-
"jest-environment-jsdom": "^24.0.0",
4637
"jest-environment-node": "^24.0.0"
4738
},
4839
"bin": {

packages/jest-runtime/src/__tests__/instrumentation.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import vm from 'vm';
1212
import path from 'path';
1313
import os from 'os';
14-
import ScriptTransformer from '../ScriptTransformer';
14+
import {ScriptTransformer} from '@jest/transform';
1515

1616
jest.mock('vm');
1717

packages/jest-runtime/src/helpers.js

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,9 @@
55
import type {Path} from 'types/Config';
66

77
import path from 'path';
8-
import chalk from 'chalk';
98
import slash from 'slash';
109
import glob from 'glob';
1110

12-
const DOT = ' \u2022 ';
13-
14-
export const enhanceUnexpectedTokenMessage = (e: Error) => {
15-
e.stack =
16-
`${chalk.bold.red('Jest encountered an unexpected token')}
17-
18-
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
19-
20-
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
21-
22-
Here's what you can do:
23-
${DOT}To have some of your "node_modules" files transformed, you can specify a custom ${chalk.bold(
24-
'"transformIgnorePatterns"',
25-
)} in your config.
26-
${DOT}If you need a custom transformation specify a ${chalk.bold(
27-
'"transform"',
28-
)} option in your config.
29-
${DOT}If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the ${chalk.bold(
30-
'"moduleNameMapper"',
31-
)} config option.
32-
33-
You'll find more details and examples of these config options in the docs:
34-
${chalk.cyan('https://jestjs.io/docs/en/configuration.html')}
35-
36-
${chalk.bold.red('Details:')}
37-
38-
` + e.stack;
39-
40-
return e;
41-
};
42-
4311
export const findSiblingsWithFileExtension = (
4412
moduleFileExtensions: Array<string>,
4513
from: Path,

packages/jest-runtime/src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ import Resolver from 'jest-resolve';
2424
import {createDirectory, deepCyclicCopy} from 'jest-util';
2525
import {escapePathForRegex} from 'jest-regex-util';
2626
import Snapshot from 'jest-snapshot';
27+
import {ScriptTransformer, shouldInstrument} from '@jest/transform';
2728
import fs from 'graceful-fs';
2829
import stripBOM from 'strip-bom';
29-
import ScriptTransformer from './ScriptTransformer';
30-
import shouldInstrument from './shouldInstrument';
3130
import {run as cliRun} from './cli';
3231
import {options as cliOptions} from './cli/args';
3332
import {findSiblingsWithFileExtension} from './helpers';

0 commit comments

Comments
 (0)