Skip to content

Commit 8fc5038

Browse files
committed
chore: migrate babel-jest to TypeScript
1 parent 780c838 commit 8fc5038

File tree

7 files changed

+122
-29
lines changed

7 files changed

+122
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- `[jest-watcher]`: Migrate to TypeScript ([#7843](https://github.com/facebook/jest/pull/7843))
2626
- `[jest-mock]`: Migrate to TypeScript ([#7847](https://github.com/facebook/jest/pull/7847), [#7850](https://github.com/facebook/jest/pull/7850))
2727
- `[jest-worker]`: Migrate to TypeScript ([#7853](https://github.com/facebook/jest/pull/7853))
28+
- `[babel-jest]`: Migrate to TypeScript
2829

2930
### Performance
3031

packages/babel-jest/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
},
1010
"license": "MIT",
1111
"main": "build/index.js",
12+
"types": "build/index.d.ts",
1213
"dependencies": {
14+
"@jest/types": "^24.1.0",
1315
"babel-plugin-istanbul": "^5.1.0",
1416
"babel-preset-jest": "^24.1.0",
1517
"chalk": "^2.4.2",

packages/babel-jest/src/__tests__/index.js renamed to packages/babel-jest/src/__tests__/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7-
const babelJest = require('../index');
7+
8+
import {Config, Transform} from '@jest/types';
9+
import babelJest from '../index';
810

911
//Mock data for all the tests
1012
const sourceString = `
@@ -24,12 +26,16 @@ const mockConfig = {
2426
};
2527

2628
test(`Returns source string with inline maps when no transformOptions is passed`, () => {
27-
const result = babelJest.process(sourceString, 'dummy_path.js', mockConfig);
29+
const result = babelJest.process(
30+
sourceString,
31+
'dummy_path.js',
32+
(mockConfig as unknown) as Config.ProjectConfig,
33+
) as Transform.TransformedSource;
2834
expect(typeof result).toBe('object');
2935
expect(result.code).toBeDefined();
3036
expect(result.map).toBeDefined();
3137
expect(result.code).toMatch('//# sourceMappingURL');
3238
expect(result.code).toMatch('customMultiply');
33-
expect(result.map.sources).toEqual(['dummy_path.js']);
34-
expect(JSON.stringify(result.map.sourcesContent)).toMatch('customMultiply');
39+
expect(result.map!.sources).toEqual(['dummy_path.js']);
40+
expect(JSON.stringify(result.map!.sourcesContent)).toMatch('customMultiply');
3541
});

packages/babel-jest/src/index.js renamed to packages/babel-jest/src/index.ts

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,31 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow
86
*/
97

10-
import type {Path, ProjectConfig} from 'types/Config';
11-
import type {
12-
CacheKeyOptions,
13-
Transformer,
14-
TransformOptions,
15-
TransformedSource,
16-
} from 'types/Transform';
17-
188
import crypto from 'crypto';
199
import fs from 'fs';
2010
import path from 'path';
21-
import {transformSync as babelTransform, loadPartialConfig} from '@babel/core';
11+
import {Config, Transform} from '@jest/types';
12+
import {
13+
transformSync as babelTransform,
14+
loadPartialConfig,
15+
TransformOptions,
16+
PartialConfig,
17+
} from '@babel/core';
2218
import chalk from 'chalk';
2319
import slash from 'slash';
2420

2521
const THIS_FILE = fs.readFileSync(__filename);
2622
const jestPresetPath = require.resolve('babel-preset-jest');
2723
const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul');
2824

29-
const createTransformer = (options: any): Transformer => {
25+
const createTransformer = (
26+
options: TransformOptions = {},
27+
): Transform.Transformer => {
3028
options = {
3129
...options,
30+
// @ts-ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/32955
3231
caller: {
3332
name: 'babel-jest',
3433
supportsStaticESM: false,
@@ -39,10 +38,13 @@ const createTransformer = (options: any): Transformer => {
3938
sourceMaps: 'both',
4039
};
4140

41+
// @ts-ignore: seems like this is removed. Is that true?
4242
delete options.cacheDirectory;
43-
delete options.filename;
4443

45-
function loadBabelConfig(cwd, filename) {
44+
function loadBabelConfig(
45+
cwd: Config.Path,
46+
filename: Config.Path,
47+
): PartialConfig {
4648
// `cwd` first to allow incoming options to override it
4749
const babelConfig = loadPartialConfig({cwd, ...options, filename});
4850

@@ -63,9 +65,13 @@ const createTransformer = (options: any): Transformer => {
6365
canInstrument: true,
6466
getCacheKey(
6567
fileData: string,
66-
filename: Path,
68+
filename: Config.Path,
6769
configString: string,
68-
{config, instrument, rootDir}: {config: ProjectConfig} & CacheKeyOptions,
70+
{
71+
config,
72+
instrument,
73+
rootDir,
74+
}: {config: Config.ProjectConfig} & Transform.CacheKeyOptions,
6975
): string {
7076
const babelOptions = loadBabelConfig(config.cwd, filename);
7177
const configPath = [
@@ -96,16 +102,16 @@ const createTransformer = (options: any): Transformer => {
96102
},
97103
process(
98104
src: string,
99-
filename: Path,
100-
config: ProjectConfig,
101-
transformOptions?: TransformOptions,
102-
): string | TransformedSource {
105+
filename: Config.Path,
106+
config: Config.ProjectConfig,
107+
transformOptions?: Transform.TransformOptions,
108+
): string | Transform.TransformedSource {
103109
const babelOptions = {...loadBabelConfig(config.cwd, filename).options};
104110

105111
if (transformOptions && transformOptions.instrument) {
106112
babelOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
107113
// Copied from jest-runtime transform.js
108-
babelOptions.plugins = babelOptions.plugins.concat([
114+
babelOptions.plugins = (babelOptions.plugins || []).concat([
109115
[
110116
babelIstanbulPlugin,
111117
{
@@ -119,10 +125,21 @@ const createTransformer = (options: any): Transformer => {
119125

120126
const transformResult = babelTransform(src, babelOptions);
121127

122-
return transformResult || src;
128+
if (transformResult && typeof transformResult.code === 'string') {
129+
// @ts-ignore: why doesn't TS understand this?
130+
return transformResult;
131+
}
132+
133+
return src;
123134
},
124135
};
125136
};
126137

127-
module.exports = createTransformer();
128-
(module.exports: any).createTransformer = createTransformer;
138+
const transformer = createTransformer();
139+
140+
// FIXME: This is not part of the exported TS types. When fixed, remember to
141+
// move @types/babel__core to `dependencies` instead of `devDependencies`
142+
// (one fix is to use ESM, maybe for Jest 25?)
143+
transformer.createTransformer = createTransformer;
144+
145+
export = transformer;

packages/babel-jest/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "build"
6+
},
7+
"references": [{"path": "../jest-types"}]
8+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {Script} from 'vm';
9+
import {Path, ProjectConfig} from './Config';
10+
11+
export type TransformedSource = {
12+
code: string;
13+
map?: // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
14+
{
15+
version: number;
16+
sources: string[];
17+
names: string[];
18+
sourceRoot?: string;
19+
sourcesContent?: string[];
20+
mappings: string;
21+
file: string;
22+
} | null;
23+
};
24+
25+
export type TransformResult = {
26+
script: Script;
27+
mapCoverage: boolean;
28+
sourceMapPath?: string;
29+
};
30+
31+
export type TransformOptions = {
32+
instrument: boolean;
33+
};
34+
35+
export type CacheKeyOptions = {
36+
config: ProjectConfig;
37+
instrument: boolean;
38+
rootDir: string;
39+
};
40+
41+
export type Transformer = {
42+
canInstrument?: boolean;
43+
createTransformer?: (options: any) => Transformer;
44+
45+
getCacheKey: (
46+
fileData: string,
47+
filePath: Path,
48+
configStr: string,
49+
options: CacheKeyOptions,
50+
) => string;
51+
52+
process: (
53+
sourceText: string,
54+
sourcePath: Path,
55+
config: ProjectConfig,
56+
options?: TransformOptions,
57+
) => string | TransformedSource;
58+
};

packages/jest-types/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ import * as Console from './Console';
1010
import * as SourceMaps from './SourceMaps';
1111
import * as TestResult from './TestResult';
1212
import * as Mocks from './Mocks';
13+
import * as Transform from './Transform';
1314

14-
export {Config, Console, SourceMaps, TestResult, Mocks};
15+
export {Config, Console, SourceMaps, TestResult, Mocks, Transform};

0 commit comments

Comments
 (0)