Skip to content

Commit 4feab3e

Browse files
authored
feat: add async support to babel-jest (#11192)
1 parent 8717975 commit 4feab3e

File tree

13 files changed

+338
-70
lines changed

13 files changed

+338
-70
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Features
44

5+
- `[babel-jest]` Add async transformation ([#11192](https://github.com/facebook/jest/pull/11192))
56
- `[jest-changed-files]` Use '--' to separate paths from revisions ([#11160](https://github.com/facebook/jest/pull/11160))
67
- `[jest-circus]` [**BREAKING**] Fail tests when multiple `done()` calls are made ([#10624](https://github.com/facebook/jest/pull/10624))
78
- `[jest-circus, jest-jasmine2]` [**BREAKING**] Fail the test instead of just warning when describe returns a value ([#10947](https://github.com/facebook/jest/pull/10947))

e2e/__tests__/__snapshots__/transform.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ FAIL __tests__/ignoredFile.test.js
66
77
babel-jest: Babel ignores __tests__/ignoredFile.test.js - make sure to include the file in Jest's transformIgnorePatterns as well.
88
9-
at loadBabelConfig (../../../packages/babel-jest/build/index.js:195:13)
9+
at assertLoadedBabelConfig (../../../packages/babel-jest/build/index.js:130:11)
1010
`;
1111

1212
exports[`babel-jest instruments only specific files and collects coverage 1`] = `

e2e/__tests__/transform.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,21 @@ onNodeVersions('^12.17.0 || >=13.2.0', () => {
264264
expect(json.numPassedTests).toBe(1);
265265
});
266266
});
267+
268+
describe('babel-jest-async', () => {
269+
const dir = path.resolve(__dirname, '../transform/babel-jest-async');
270+
271+
beforeAll(() => {
272+
runYarnInstall(dir);
273+
});
274+
275+
it("should use babel-jest's async transforms", () => {
276+
const {json, stderr} = runWithJson(dir, ['--no-cache'], {
277+
nodeOptions: '--experimental-vm-modules',
278+
});
279+
expect(stderr).toMatch(/PASS/);
280+
expect(json.success).toBe(true);
281+
expect(json.numPassedTests).toBe(1);
282+
});
283+
});
267284
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 nullReturningFunc from '../only-file-to-transform.js';
9+
10+
it('strips flowtypes using babel-jest', () => {
11+
expect(nullReturningFunc()).toBe(null);
12+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
const someFunction = (): null => null;
9+
10+
export default someFunction;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "module",
3+
"dependencies": {
4+
"@babel/preset-flow": "^7.0.0"
5+
},
6+
"jest": {
7+
"testEnvironment": "node",
8+
"transform": {
9+
"only-file-to-transform\\.js$": "<rootDir>/transformer.js"
10+
}
11+
}
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 {fileURLToPath} from 'url';
9+
import babelJest from 'babel-jest';
10+
11+
export default {
12+
...babelJest.default.createTransformer({
13+
presets: ['@babel/preset-flow'],
14+
root: fileURLToPath(import.meta.url),
15+
}),
16+
// remove the synchronous functions
17+
getCacheKey: undefined,
18+
process: undefined,
19+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# This file is generated by running "yarn install" inside your project.
2+
# Manual changes might be lost - proceed with caution!
3+
4+
__metadata:
5+
version: 4
6+
cacheKey: 7
7+
8+
"@babel/helper-plugin-utils@npm:^7.12.13, @babel/helper-plugin-utils@npm:^7.13.0":
9+
version: 7.13.0
10+
resolution: "@babel/helper-plugin-utils@npm:7.13.0"
11+
checksum: 229ac1917b43ad38732d2d4a9a826f87d8945719249efe1d6191f3e25ba6027a289af70380d82d62a03fc9e82558a0ea6f12739cbb55b64bb280d6b511b4ca65
12+
languageName: node
13+
linkType: hard
14+
15+
"@babel/plugin-syntax-flow@npm:^7.12.13":
16+
version: 7.12.13
17+
resolution: "@babel/plugin-syntax-flow@npm:7.12.13"
18+
dependencies:
19+
"@babel/helper-plugin-utils": ^7.12.13
20+
peerDependencies:
21+
"@babel/core": ^7.0.0-0
22+
checksum: de8845354dda62b7857a518a54f85bf30809ed1d7cc5ace93ced6da16d095cba78487d18651f1b2277db58d8e749cb910c703f96529af198369226e374df5f73
23+
languageName: node
24+
linkType: hard
25+
26+
"@babel/plugin-transform-flow-strip-types@npm:^7.12.13":
27+
version: 7.13.0
28+
resolution: "@babel/plugin-transform-flow-strip-types@npm:7.13.0"
29+
dependencies:
30+
"@babel/helper-plugin-utils": ^7.13.0
31+
"@babel/plugin-syntax-flow": ^7.12.13
32+
peerDependencies:
33+
"@babel/core": ^7.0.0-0
34+
checksum: 60903f5e3619b4f4a19d6d00a4d10c5b97566f5d4c56dd35ccdaa6e621fc955ec4003f12cd73ec99475894a7eca6a34aa4b38f87c7c81e93d5fe03d006aae77b
35+
languageName: node
36+
linkType: hard
37+
38+
"@babel/preset-flow@npm:^7.0.0":
39+
version: 7.12.13
40+
resolution: "@babel/preset-flow@npm:7.12.13"
41+
dependencies:
42+
"@babel/helper-plugin-utils": ^7.12.13
43+
"@babel/plugin-transform-flow-strip-types": ^7.12.13
44+
peerDependencies:
45+
"@babel/core": ^7.0.0-0
46+
checksum: 47fe1001194a57fbdb33250adcb4c3aa9ff551cfb4eea1a16b123ff5fe78730a7ebfb839bacbe18390fc50fa4bf67fdd5293703b859876de45d52f50d4da0d44
47+
languageName: node
48+
linkType: hard
49+
50+
"root-workspace-0b6124@workspace:.":
51+
version: 0.0.0-use.local
52+
resolution: "root-workspace-0b6124@workspace:."
53+
dependencies:
54+
"@babel/preset-flow": ^7.0.0
55+
languageName: unknown
56+
linkType: soft

packages/babel-jest/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"@types/graceful-fs": "^4.1.3"
3131
},
3232
"peerDependencies": {
33-
"@babel/core": "^7.0.0"
33+
"@babel/core": "^7.8.0"
3434
},
3535
"engines": {
3636
"node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0"

packages/babel-jest/src/__tests__/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ jest.mock('../loadBabelConfig', () => {
1414

1515
return {
1616
loadPartialConfig: jest.fn((...args) => actual.loadPartialConfig(...args)),
17+
loadPartialConfigAsync: jest.fn((...args) =>
18+
actual.loadPartialConfigAsync(...args),
19+
),
1720
};
1821
});
1922

@@ -49,6 +52,25 @@ test('Returns source string with inline maps when no transformOptions is passed'
4952
expect(JSON.stringify(result.map!.sourcesContent)).toMatch('customMultiply');
5053
});
5154

55+
test('Returns source string with inline maps when no transformOptions is passed async', async () => {
56+
const result: any = await babelJest.processAsync!(
57+
sourceString,
58+
'dummy_path.js',
59+
{
60+
config: makeProjectConfig(),
61+
configString: JSON.stringify(makeProjectConfig()),
62+
instrument: false,
63+
},
64+
);
65+
expect(typeof result).toBe('object');
66+
expect(result.code).toBeDefined();
67+
expect(result.map).toBeDefined();
68+
expect(result.code).toMatch('//# sourceMappingURL');
69+
expect(result.code).toMatch('customMultiply');
70+
expect(result.map!.sources).toEqual(['dummy_path.js']);
71+
expect(JSON.stringify(result.map!.sourcesContent)).toMatch('customMultiply');
72+
});
73+
5274
describe('caller option correctly merges from defaults and options', () => {
5375
test.each([
5476
[

0 commit comments

Comments
 (0)