From 6e4c1c7dd5b8467c7a519665502c0ad0e6a5e146 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 13 Mar 2021 17:01:42 +0100 Subject: [PATCH] feat: add async support to babel-jest --- CHANGELOG.md | 1 + packages/babel-jest/src/index.ts | 75 ++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bddfa129c71..9907710e6568 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/packages/babel-jest/src/index.ts b/packages/babel-jest/src/index.ts index c0bff0b5d336..12547a5f1ebc 100644 --- a/packages/babel-jest/src/index.ts +++ b/packages/babel-jest/src/index.ts @@ -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'; @@ -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, @@ -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 || '', @@ -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); @@ -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; }, };