Skip to content

Commit

Permalink
feat: add async support to babel-jest
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Mar 13, 2021
1 parent dd13096 commit 6e4c1c7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

- `[babel-jest]` Add async transformation
- `[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
75 changes: 47 additions & 28 deletions packages/babel-jest/src/index.ts
Original file line number Diff line number Diff line change
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({
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

0 comments on commit 6e4c1c7

Please sign in to comment.