Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add async support to babel-jest #11192

Merged
merged 11 commits into from Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

### Features

- `[babel-jest]` Add async transformation ([#11192](https://github.com/facebook/jest/pull/11192))
- `[jest-changed-files]` Use '--' to separate paths from revisions ([#11160](https://github.com/facebook/jest/pull/11160))
- `[jest-circus]` [**BREAKING**] Fail tests when multiple `done()` calls are made ([#10624](https://github.com/facebook/jest/pull/10624))
- `[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))
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/transform.test.ts.snap
Expand Up @@ -6,7 +6,7 @@ FAIL __tests__/ignoredFile.test.js

babel-jest: Babel ignores __tests__/ignoredFile.test.js - make sure to include the file in Jest's transformIgnorePatterns as well.

at loadBabelConfig (../../../packages/babel-jest/build/index.js:195:13)
at assertLoadedBabelConfig (../../../packages/babel-jest/build/index.js:130:11)
`;

exports[`babel-jest instruments only specific files and collects coverage 1`] = `
Expand Down
17 changes: 17 additions & 0 deletions e2e/__tests__/transform.test.ts
Expand Up @@ -264,4 +264,21 @@ onNodeVersions('^12.17.0 || >=13.2.0', () => {
expect(json.numPassedTests).toBe(1);
});
});

describe('babel-jest-async', () => {
const dir = path.resolve(__dirname, '../transform/babel-jest-async');

beforeAll(() => {
runYarnInstall(dir);
});

it("should use babel-jest's async transforms", () => {
const {json, stderr} = runWithJson(dir, ['--no-cache'], {
nodeOptions: '--experimental-vm-modules',
});
expect(stderr).toMatch(/PASS/);
expect(json.success).toBe(true);
expect(json.numPassedTests).toBe(1);
});
});
});
12 changes: 12 additions & 0 deletions e2e/transform/babel-jest-async/__tests__/babelJest.test.js
@@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import nullReturningFunc from '../only-file-to-transform.js';

it('strips flowtypes using babel-jest', () => {
expect(nullReturningFunc()).toBe(null);
});
10 changes: 10 additions & 0 deletions e2e/transform/babel-jest-async/only-file-to-transform.js
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const someFunction = (): null => null;

export default someFunction;
12 changes: 12 additions & 0 deletions e2e/transform/babel-jest-async/package.json
@@ -0,0 +1,12 @@
{
"type": "module",
"dependencies": {
"@babel/preset-flow": "^7.0.0"
},
"jest": {
"testEnvironment": "node",
"transform": {
"only-file-to-transform\\.js$": "<rootDir>/transformer.js"
}
}
}
19 changes: 19 additions & 0 deletions e2e/transform/babel-jest-async/transformer.js
@@ -0,0 +1,19 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {fileURLToPath} from 'url';
import babelJest from 'babel-jest';

export default {
...babelJest.default.createTransformer({
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not too happy about this one 馃檨 We should output ESM from Jest 28 or something

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding export { createTransformer } to babel-jest would be enough.

Copy link
Member Author

@SimenB SimenB Mar 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep that'd work, but "real" ESM should use a single default export of an object, so the issue is a "fake" esm module imported directly in native ESM. Edge casey enough that I don't think it's worth changing before just using native ESM

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and just to be explicit - the .default change is not because of this PR, but the change made in #11193

presets: ['@babel/preset-flow'],
root: fileURLToPath(import.meta.url),
}),
// remove the synchronous functions
getCacheKey: undefined,
process: undefined,
};
56 changes: 56 additions & 0 deletions e2e/transform/babel-jest-async/yarn.lock
@@ -0,0 +1,56 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!

__metadata:
version: 4
cacheKey: 7

"@babel/helper-plugin-utils@npm:^7.12.13, @babel/helper-plugin-utils@npm:^7.13.0":
version: 7.13.0
resolution: "@babel/helper-plugin-utils@npm:7.13.0"
checksum: 229ac1917b43ad38732d2d4a9a826f87d8945719249efe1d6191f3e25ba6027a289af70380d82d62a03fc9e82558a0ea6f12739cbb55b64bb280d6b511b4ca65
languageName: node
linkType: hard

"@babel/plugin-syntax-flow@npm:^7.12.13":
version: 7.12.13
resolution: "@babel/plugin-syntax-flow@npm:7.12.13"
dependencies:
"@babel/helper-plugin-utils": ^7.12.13
peerDependencies:
"@babel/core": ^7.0.0-0
checksum: de8845354dda62b7857a518a54f85bf30809ed1d7cc5ace93ced6da16d095cba78487d18651f1b2277db58d8e749cb910c703f96529af198369226e374df5f73
languageName: node
linkType: hard

"@babel/plugin-transform-flow-strip-types@npm:^7.12.13":
version: 7.13.0
resolution: "@babel/plugin-transform-flow-strip-types@npm:7.13.0"
dependencies:
"@babel/helper-plugin-utils": ^7.13.0
"@babel/plugin-syntax-flow": ^7.12.13
peerDependencies:
"@babel/core": ^7.0.0-0
checksum: 60903f5e3619b4f4a19d6d00a4d10c5b97566f5d4c56dd35ccdaa6e621fc955ec4003f12cd73ec99475894a7eca6a34aa4b38f87c7c81e93d5fe03d006aae77b
languageName: node
linkType: hard

"@babel/preset-flow@npm:^7.0.0":
version: 7.12.13
resolution: "@babel/preset-flow@npm:7.12.13"
dependencies:
"@babel/helper-plugin-utils": ^7.12.13
"@babel/plugin-transform-flow-strip-types": ^7.12.13
peerDependencies:
"@babel/core": ^7.0.0-0
checksum: 47fe1001194a57fbdb33250adcb4c3aa9ff551cfb4eea1a16b123ff5fe78730a7ebfb839bacbe18390fc50fa4bf67fdd5293703b859876de45d52f50d4da0d44
languageName: node
linkType: hard

"root-workspace-0b6124@workspace:.":
version: 0.0.0-use.local
resolution: "root-workspace-0b6124@workspace:."
dependencies:
"@babel/preset-flow": ^7.0.0
languageName: unknown
linkType: soft
2 changes: 1 addition & 1 deletion packages/babel-jest/package.json
Expand Up @@ -30,7 +30,7 @@
"@types/graceful-fs": "^4.1.3"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
"@babel/core": "^7.8.0"
},
"engines": {
"node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0"
Expand Down
22 changes: 22 additions & 0 deletions packages/babel-jest/src/__tests__/index.ts
Expand Up @@ -14,6 +14,9 @@ jest.mock('../loadBabelConfig', () => {

return {
loadPartialConfig: jest.fn((...args) => actual.loadPartialConfig(...args)),
loadPartialConfigAsync: jest.fn((...args) =>
actual.loadPartialConfigAsync(...args),
),
};
});

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

test('Returns source string with inline maps when no transformOptions is passed async', async () => {
const result: any = await babelJest.processAsync!(
sourceString,
'dummy_path.js',
{
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
},
);
expect(typeof result).toBe('object');
expect(result.code).toBeDefined();
expect(result.map).toBeDefined();
expect(result.code).toMatch('//# sourceMappingURL');
expect(result.code).toMatch('customMultiply');
expect(result.map!.sources).toEqual(['dummy_path.js']);
expect(JSON.stringify(result.map!.sourcesContent)).toMatch('customMultiply');
});

describe('caller option correctly merges from defaults and options', () => {
test.each([
[
Expand Down