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 3 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:190:13)
at loadBabelConfig (../../../packages/babel-jest/build/index.js:191:13)
`;

exports[`babel-jest instruments only specific files and collects coverage 1`] = `
Expand Down
75 changes: 47 additions & 28 deletions packages/babel-jest/src/index.ts
Expand Up @@ -11,6 +11,7 @@ import {
PartialConfig,
TransformOptions,
transformSync as babelTransform,
transformAsync as babelTransformAsync,
} from '@babel/core';
import chalk = require('chalk');
import * as fs from 'graceful-fs';
Expand Down Expand Up @@ -48,10 +49,10 @@ const createTransformer: CreateTransformer = userOptions => {
} as const;

function loadBabelConfig(
cwd: Config.Path,
filename: Config.Path,
transformOptions: JestTransformOptions,
): PartialConfig {
const {cwd} = transformOptions.config;
// `cwd` first to allow incoming options to override it
const babelConfig = loadPartialConfig({
Copy link
Contributor

Choose a reason for hiding this comment

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

There is also the async version of this function, and I think it allows using await in config files.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I assumed there wasn't as it's not in the types 馃槄

image

Copy link
Contributor

Choose a reason for hiding this comment

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

Uh it's because the types are not maintained by us.

We are slowly converting our codebase to TS, we'll release official type definitions when we finish.

Copy link
Member Author

Choose a reason for hiding this comment

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

I can send a PR to DT 馃憤

Copy link
Member Author

Choose a reason for hiding this comment

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

cwd,
Expand Down Expand Up @@ -87,16 +88,38 @@ const createTransformer: CreateTransformer = userOptions => {
return babelConfig;
}

function loadBabelOptions(
filename: Config.Path,
transformOptions: JestTransformOptions,
): TransformOptions {
const babelOptions = {
...loadBabelConfig(filename, transformOptions).options,
};

if (transformOptions.instrument) {
babelOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
// Copied from jest-runtime transform.js
babelOptions.plugins = (babelOptions.plugins || []).concat([
[
babelIstanbulPlugin,
{
// files outside `cwd` will not be instrumented
cwd: transformOptions.config.cwd,
exclude: [],
},
],
]);
}

return babelOptions;
}

return {
canInstrument: true,
getCacheKey(sourceText, sourcePath, transformOptions) {
const {config, configString, instrument} = transformOptions;

const babelOptions = loadBabelConfig(
config.cwd,
sourcePath,
transformOptions,
);
const babelOptions = loadBabelConfig(sourcePath, transformOptions);
const configPath = [
babelOptions.config || '',
babelOptions.babelrc || '',
Expand All @@ -123,28 +146,7 @@ const createTransformer: CreateTransformer = userOptions => {
.digest('hex');
},
process(sourceText, sourcePath, transformOptions) {
const babelOptions = {
...loadBabelConfig(
transformOptions.config.cwd,
sourcePath,
transformOptions,
).options,
};

if (transformOptions?.instrument) {
babelOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
// Copied from jest-runtime transform.js
babelOptions.plugins = (babelOptions.plugins || []).concat([
[
babelIstanbulPlugin,
{
// files outside `cwd` will not be instrumented
cwd: transformOptions.config.rootDir,
exclude: [],
},
],
]);
}
const babelOptions = loadBabelOptions(sourcePath, transformOptions);

const transformResult = babelTransform(sourceText, babelOptions);

Expand All @@ -155,6 +157,23 @@ const createTransformer: CreateTransformer = userOptions => {
}
}

return sourceText;
},
async processAsync(sourceText, sourcePath, transformOptions) {
const babelOptions = loadBabelOptions(sourcePath, transformOptions);

const transformResult = await babelTransformAsync(
sourceText,
babelOptions,
);

if (transformResult) {
const {code, map} = transformResult;
if (typeof code === 'string') {
return {code, map};
}
}

return sourceText;
},
};
Expand Down