From 6e4c1c7dd5b8467c7a519665502c0ad0e6a5e146 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 13 Mar 2021 17:01:42 +0100 Subject: [PATCH 01/10] 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; }, }; From 960d6d2beb1804a0ffa42c0404cc4bfd163a9d35 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 13 Mar 2021 17:03:45 +0100 Subject: [PATCH 02/10] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9907710e6568..4034622b6ee5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### Features -- `[babel-jest]` Add async transformation +- `[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)) From 0c86e333cb04add91621075b22f6398be87c4c50 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 13 Mar 2021 17:16:49 +0100 Subject: [PATCH 03/10] snap --- e2e/__tests__/__snapshots__/transform.test.ts.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/__tests__/__snapshots__/transform.test.ts.snap b/e2e/__tests__/__snapshots__/transform.test.ts.snap index a68d884e885c..7949dec09984 100644 --- a/e2e/__tests__/__snapshots__/transform.test.ts.snap +++ b/e2e/__tests__/__snapshots__/transform.test.ts.snap @@ -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`] = ` From 536efaaf9a133f0e074ab31fed0a81238bf638ff Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 14 Mar 2021 10:26:04 +0100 Subject: [PATCH 04/10] async config loading as well --- packages/babel-jest/src/index.ts | 197 ++++++++++++++------- packages/babel-jest/src/loadBabelConfig.ts | 11 ++ 2 files changed, 147 insertions(+), 61 deletions(-) diff --git a/packages/babel-jest/src/index.ts b/packages/babel-jest/src/index.ts index 12547a5f1ebc..fb9e18b8d185 100644 --- a/packages/babel-jest/src/index.ts +++ b/packages/babel-jest/src/index.ts @@ -21,7 +21,7 @@ import type { SyncTransformer, } from '@jest/transform'; import type {Config} from '@jest/types'; -import {loadPartialConfig} from './loadBabelConfig'; +import {loadPartialConfig, loadPartialConfigAsync} from './loadBabelConfig'; const THIS_FILE = fs.readFileSync(__filename); const jestPresetPath = require.resolve('babel-preset-jest'); @@ -29,6 +29,78 @@ const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul'); type CreateTransformer = SyncTransformer['createTransformer']; +function assertLoadedBabelConfig( + babelConfig: Readonly | null, + cwd: Config.Path, + filename: Config.Path, +): asserts babelConfig { + if (!babelConfig) { + throw new Error( + `babel-jest: Babel ignores ${chalk.bold( + slash(path.relative(cwd, filename)), + )} - make sure to include the file in Jest's ${chalk.bold( + 'transformIgnorePatterns', + )} as well.`, + ); + } +} + +function addIstanbulInstrumentation( + babelOptions: TransformOptions, + transformOptions: JestTransformOptions, +): TransformOptions { + if (transformOptions.instrument) { + const copiedBabelOptions: TransformOptions = {...babelOptions}; + copiedBabelOptions.auxiliaryCommentBefore = ' istanbul ignore next '; + // Copied from jest-runtime transform.js + copiedBabelOptions.plugins = (copiedBabelOptions.plugins || []).concat([ + [ + babelIstanbulPlugin, + { + // files outside `cwd` will not be instrumented + cwd: transformOptions.config.cwd, + exclude: [], + }, + ], + ]); + + return copiedBabelOptions; + } + + return babelOptions; +} + +function getCacheKeyFromConfig( + sourceText: string, + sourcePath: Config.Path, + babelOptions: PartialConfig, + transformOptions: JestTransformOptions, +): string { + const {config, configString, instrument} = transformOptions; + + const configPath = [babelOptions.config || '', babelOptions.babelrc || '']; + + return createHash('md5') + .update(THIS_FILE) + .update('\0', 'utf8') + .update(JSON.stringify(babelOptions.options)) + .update('\0', 'utf8') + .update(sourceText) + .update('\0', 'utf8') + .update(path.relative(config.rootDir, sourcePath)) + .update('\0', 'utf8') + .update(configString) + .update('\0', 'utf8') + .update(configPath.join('')) + .update('\0', 'utf8') + .update(instrument ? 'instrument' : '') + .update('\0', 'utf8') + .update(process.env.NODE_ENV || '') + .update('\0', 'utf8') + .update(process.env.BABEL_ENV || '') + .digest('hex'); +} + const createTransformer: CreateTransformer = userOptions => { const inputOptions = userOptions ?? {}; @@ -48,13 +120,13 @@ const createTransformer: CreateTransformer = userOptions => { sourceMaps: 'both', } as const; - function loadBabelConfig( + function mergeBabelTransformOptions( filename: Config.Path, transformOptions: JestTransformOptions, - ): PartialConfig { + ): TransformOptions { const {cwd} = transformOptions.config; // `cwd` first to allow incoming options to override it - const babelConfig = loadPartialConfig({ + return { cwd, ...options, caller: { @@ -73,17 +145,31 @@ const createTransformer: CreateTransformer = userOptions => { options.caller.supportsTopLevelAwait, }, filename, - }); - - if (!babelConfig) { - throw new Error( - `babel-jest: Babel ignores ${chalk.bold( - slash(path.relative(cwd, filename)), - )} - make sure to include the file in Jest's ${chalk.bold( - 'transformIgnorePatterns', - )} as well.`, - ); - } + }; + } + + function loadBabelConfig( + filename: Config.Path, + transformOptions: JestTransformOptions, + ): PartialConfig { + const babelConfig = loadPartialConfig( + mergeBabelTransformOptions(filename, transformOptions), + ); + + assertLoadedBabelConfig(babelConfig, transformOptions.config.cwd, filename); + + return babelConfig; + } + + async function loadBabelConfigAsync( + filename: Config.Path, + transformOptions: JestTransformOptions, + ): Promise { + const babelConfig = await loadPartialConfigAsync( + mergeBabelTransformOptions(filename, transformOptions), + ); + + assertLoadedBabelConfig(babelConfig, transformOptions.config.cwd, filename); return babelConfig; } @@ -92,58 +178,44 @@ const createTransformer: CreateTransformer = userOptions => { filename: Config.Path, transformOptions: JestTransformOptions, ): TransformOptions { - const babelOptions = { - ...loadBabelConfig(filename, transformOptions).options, - }; + const {options} = loadBabelConfig(filename, transformOptions); + + return addIstanbulInstrumentation(options, transformOptions); + } + + async function loadBabelOptionsAsync( + filename: Config.Path, + transformOptions: JestTransformOptions, + ): Promise { + const {options} = await loadBabelConfigAsync(filename, transformOptions); - 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 addIstanbulInstrumentation(options, transformOptions); } return { canInstrument: true, getCacheKey(sourceText, sourcePath, transformOptions) { - const {config, configString, instrument} = transformOptions; - const babelOptions = loadBabelConfig(sourcePath, transformOptions); - const configPath = [ - babelOptions.config || '', - babelOptions.babelrc || '', - ]; - - return createHash('md5') - .update(THIS_FILE) - .update('\0', 'utf8') - .update(JSON.stringify(babelOptions.options)) - .update('\0', 'utf8') - .update(sourceText) - .update('\0', 'utf8') - .update(path.relative(config.rootDir, sourcePath)) - .update('\0', 'utf8') - .update(configString) - .update('\0', 'utf8') - .update(configPath.join('')) - .update('\0', 'utf8') - .update(instrument ? 'instrument' : '') - .update('\0', 'utf8') - .update(process.env.NODE_ENV || '') - .update('\0', 'utf8') - .update(process.env.BABEL_ENV || '') - .digest('hex'); + + return getCacheKeyFromConfig( + sourceText, + sourcePath, + babelOptions, + transformOptions, + ); + }, + async getCacheKeyAsync(sourceText, sourcePath, transformOptions) { + const babelOptions = await loadBabelConfigAsync( + sourcePath, + transformOptions, + ); + + return getCacheKeyFromConfig( + sourceText, + sourcePath, + babelOptions, + transformOptions, + ); }, process(sourceText, sourcePath, transformOptions) { const babelOptions = loadBabelOptions(sourcePath, transformOptions); @@ -160,7 +232,10 @@ const createTransformer: CreateTransformer = userOptions => { return sourceText; }, async processAsync(sourceText, sourcePath, transformOptions) { - const babelOptions = loadBabelOptions(sourcePath, transformOptions); + const babelOptions = await loadBabelOptionsAsync( + sourcePath, + transformOptions, + ); const transformResult = await babelTransformAsync( sourceText, diff --git a/packages/babel-jest/src/loadBabelConfig.ts b/packages/babel-jest/src/loadBabelConfig.ts index 91bbd1725980..715c2ec84405 100644 --- a/packages/babel-jest/src/loadBabelConfig.ts +++ b/packages/babel-jest/src/loadBabelConfig.ts @@ -7,3 +7,14 @@ // this is a separate file so it can be mocked in tests export {loadPartialConfig} from '@babel/core'; + +import { + PartialConfig, + TransformOptions, + // @ts-expect-error: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/51741 + loadPartialConfigAsync as asyncVersion, +} from '@babel/core'; + +export const loadPartialConfigAsync: ( + options?: TransformOptions, +) => Promise | null> = asyncVersion; From dca6de210f23f93de66e39281ab5c8a2c1cd2835 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 14 Mar 2021 10:28:32 +0100 Subject: [PATCH 05/10] update peer dep for async config support --- packages/babel-jest/package.json | 2 +- yarn.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-jest/package.json b/packages/babel-jest/package.json index 077b6c00048f..0955d7a3f419 100644 --- a/packages/babel-jest/package.json +++ b/packages/babel-jest/package.json @@ -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" diff --git a/yarn.lock b/yarn.lock index 99a1bd11e13d..dcdb81c36e34 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6249,7 +6249,7 @@ __metadata: graceful-fs: ^4.2.4 slash: ^3.0.0 peerDependencies: - "@babel/core": ^7.0.0 + "@babel/core": ^7.8.0 languageName: unknown linkType: soft From 8eb6234025a18c10717c7e9b642d7526ad70f1fb Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 14 Mar 2021 10:30:42 +0100 Subject: [PATCH 06/10] snap --- e2e/__tests__/__snapshots__/transform.test.ts.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/__tests__/__snapshots__/transform.test.ts.snap b/e2e/__tests__/__snapshots__/transform.test.ts.snap index 7949dec09984..e230bd94c6b1 100644 --- a/e2e/__tests__/__snapshots__/transform.test.ts.snap +++ b/e2e/__tests__/__snapshots__/transform.test.ts.snap @@ -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:191:13) + at assertLoadedBabelConfig (../../../packages/babel-jest/build/index.js:125:11) `; exports[`babel-jest instruments only specific files and collects coverage 1`] = ` From 8608fc19fb1d6716113bf023677c757c147c54f6 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 14 Mar 2021 10:38:15 +0100 Subject: [PATCH 07/10] move things out of closure --- packages/babel-jest/src/index.ts | 118 +++++++++++++++++-------------- 1 file changed, 65 insertions(+), 53 deletions(-) diff --git a/packages/babel-jest/src/index.ts b/packages/babel-jest/src/index.ts index fb9e18b8d185..0c6f0c7dda84 100644 --- a/packages/babel-jest/src/index.ts +++ b/packages/babel-jest/src/index.ts @@ -101,22 +101,66 @@ function getCacheKeyFromConfig( .digest('hex'); } -const createTransformer: CreateTransformer = userOptions => { - const inputOptions = userOptions ?? {}; +function loadBabelConfig( + cwd: Config.Path, + filename: Config.Path, + transformOptions: TransformOptions, +): PartialConfig { + const babelConfig = loadPartialConfig(transformOptions); + + assertLoadedBabelConfig(babelConfig, cwd, filename); + + return babelConfig; +} + +async function loadBabelConfigAsync( + cwd: Config.Path, + filename: Config.Path, + transformOptions: TransformOptions, +): Promise { + const babelConfig = await loadPartialConfigAsync(transformOptions); + + assertLoadedBabelConfig(babelConfig, cwd, filename); + + return babelConfig; +} +function loadBabelOptions( + cwd: Config.Path, + filename: Config.Path, + transformOptions: TransformOptions, + jestTransformOptions: JestTransformOptions, +): TransformOptions { + const {options} = loadBabelConfig(cwd, filename, transformOptions); + + return addIstanbulInstrumentation(options, jestTransformOptions); +} + +async function loadBabelOptionsAsync( + cwd: Config.Path, + filename: Config.Path, + transformOptions: TransformOptions, + jestTransformOptions: JestTransformOptions, +): Promise { + const {options} = await loadBabelConfigAsync(cwd, filename, transformOptions); + + return addIstanbulInstrumentation(options, jestTransformOptions); +} + +const createTransformer: CreateTransformer = (userOptions = {}) => { const options = { - ...inputOptions, + ...userOptions, caller: { name: 'babel-jest', supportsDynamicImport: false, supportsExportNamespaceFrom: false, supportsStaticESM: false, supportsTopLevelAwait: false, - ...inputOptions.caller, + ...userOptions.caller, }, compact: false, - plugins: inputOptions.plugins ?? [], - presets: (inputOptions.presets ?? []).concat(jestPresetPath), + plugins: userOptions.plugins ?? [], + presets: (userOptions.presets ?? []).concat(jestPresetPath), sourceMaps: 'both', } as const; @@ -148,54 +192,14 @@ const createTransformer: CreateTransformer = userOptions => { }; } - function loadBabelConfig( - filename: Config.Path, - transformOptions: JestTransformOptions, - ): PartialConfig { - const babelConfig = loadPartialConfig( - mergeBabelTransformOptions(filename, transformOptions), - ); - - assertLoadedBabelConfig(babelConfig, transformOptions.config.cwd, filename); - - return babelConfig; - } - - async function loadBabelConfigAsync( - filename: Config.Path, - transformOptions: JestTransformOptions, - ): Promise { - const babelConfig = await loadPartialConfigAsync( - mergeBabelTransformOptions(filename, transformOptions), - ); - - assertLoadedBabelConfig(babelConfig, transformOptions.config.cwd, filename); - - return babelConfig; - } - - function loadBabelOptions( - filename: Config.Path, - transformOptions: JestTransformOptions, - ): TransformOptions { - const {options} = loadBabelConfig(filename, transformOptions); - - return addIstanbulInstrumentation(options, transformOptions); - } - - async function loadBabelOptionsAsync( - filename: Config.Path, - transformOptions: JestTransformOptions, - ): Promise { - const {options} = await loadBabelConfigAsync(filename, transformOptions); - - return addIstanbulInstrumentation(options, transformOptions); - } - return { canInstrument: true, getCacheKey(sourceText, sourcePath, transformOptions) { - const babelOptions = loadBabelConfig(sourcePath, transformOptions); + const babelOptions = loadBabelConfig( + transformOptions.config.cwd, + sourcePath, + mergeBabelTransformOptions(sourcePath, transformOptions), + ); return getCacheKeyFromConfig( sourceText, @@ -206,8 +210,9 @@ const createTransformer: CreateTransformer = userOptions => { }, async getCacheKeyAsync(sourceText, sourcePath, transformOptions) { const babelOptions = await loadBabelConfigAsync( + transformOptions.config.cwd, sourcePath, - transformOptions, + mergeBabelTransformOptions(sourcePath, transformOptions), ); return getCacheKeyFromConfig( @@ -218,7 +223,12 @@ const createTransformer: CreateTransformer = userOptions => { ); }, process(sourceText, sourcePath, transformOptions) { - const babelOptions = loadBabelOptions(sourcePath, transformOptions); + const babelOptions = loadBabelOptions( + transformOptions.config.cwd, + sourcePath, + mergeBabelTransformOptions(sourcePath, transformOptions), + transformOptions, + ); const transformResult = babelTransform(sourceText, babelOptions); @@ -233,7 +243,9 @@ const createTransformer: CreateTransformer = userOptions => { }, async processAsync(sourceText, sourcePath, transformOptions) { const babelOptions = await loadBabelOptionsAsync( + transformOptions.config.cwd, sourcePath, + mergeBabelTransformOptions(sourcePath, transformOptions), transformOptions, ); From d052f81fc608db3aa6d1f1985125356ca3154cdd Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 14 Mar 2021 10:57:15 +0100 Subject: [PATCH 08/10] fix test --- packages/babel-jest/src/index.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/babel-jest/src/index.ts b/packages/babel-jest/src/index.ts index 0c6f0c7dda84..d6f54b92430d 100644 --- a/packages/babel-jest/src/index.ts +++ b/packages/babel-jest/src/index.ts @@ -147,20 +147,22 @@ async function loadBabelOptionsAsync( return addIstanbulInstrumentation(options, jestTransformOptions); } -const createTransformer: CreateTransformer = (userOptions = {}) => { +const createTransformer: CreateTransformer = userOptions => { + const inputOptions = userOptions ?? {}; + const options = { - ...userOptions, + ...inputOptions, caller: { name: 'babel-jest', supportsDynamicImport: false, supportsExportNamespaceFrom: false, supportsStaticESM: false, supportsTopLevelAwait: false, - ...userOptions.caller, + ...inputOptions.caller, }, compact: false, - plugins: userOptions.plugins ?? [], - presets: (userOptions.presets ?? []).concat(jestPresetPath), + plugins: inputOptions.plugins ?? [], + presets: (inputOptions.presets ?? []).concat(jestPresetPath), sourceMaps: 'both', } as const; From 809380d8115f3710ee1480e934662d07e3c4db93 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 14 Mar 2021 11:16:15 +0100 Subject: [PATCH 09/10] add async test --- packages/babel-jest/src/__tests__/index.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/babel-jest/src/__tests__/index.ts b/packages/babel-jest/src/__tests__/index.ts index 445a50d8dec8..c04aa5f7c500 100644 --- a/packages/babel-jest/src/__tests__/index.ts +++ b/packages/babel-jest/src/__tests__/index.ts @@ -14,6 +14,9 @@ jest.mock('../loadBabelConfig', () => { return { loadPartialConfig: jest.fn((...args) => actual.loadPartialConfig(...args)), + loadPartialConfigAsync: jest.fn((...args) => + actual.loadPartialConfigAsync(...args), + ), }; }); @@ -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([ [ From 6275f6a31aa996ad653840c4dc7673e168689525 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 14 Mar 2021 11:39:38 +0100 Subject: [PATCH 10/10] integration test --- e2e/__tests__/transform.test.ts | 17 ++++++ .../__tests__/babelJest.test.js | 12 ++++ .../only-file-to-transform.js | 10 ++++ e2e/transform/babel-jest-async/package.json | 12 ++++ e2e/transform/babel-jest-async/transformer.js | 19 +++++++ e2e/transform/babel-jest-async/yarn.lock | 56 +++++++++++++++++++ 6 files changed, 126 insertions(+) create mode 100644 e2e/transform/babel-jest-async/__tests__/babelJest.test.js create mode 100644 e2e/transform/babel-jest-async/only-file-to-transform.js create mode 100644 e2e/transform/babel-jest-async/package.json create mode 100644 e2e/transform/babel-jest-async/transformer.js create mode 100644 e2e/transform/babel-jest-async/yarn.lock diff --git a/e2e/__tests__/transform.test.ts b/e2e/__tests__/transform.test.ts index a325a7a4e7f9..e58da7e802b2 100644 --- a/e2e/__tests__/transform.test.ts +++ b/e2e/__tests__/transform.test.ts @@ -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); + }); + }); }); diff --git a/e2e/transform/babel-jest-async/__tests__/babelJest.test.js b/e2e/transform/babel-jest-async/__tests__/babelJest.test.js new file mode 100644 index 000000000000..2a68c85ba88c --- /dev/null +++ b/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); +}); diff --git a/e2e/transform/babel-jest-async/only-file-to-transform.js b/e2e/transform/babel-jest-async/only-file-to-transform.js new file mode 100644 index 000000000000..058eea6bdaae --- /dev/null +++ b/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; diff --git a/e2e/transform/babel-jest-async/package.json b/e2e/transform/babel-jest-async/package.json new file mode 100644 index 000000000000..8a506ac99e15 --- /dev/null +++ b/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$": "/transformer.js" + } + } +} diff --git a/e2e/transform/babel-jest-async/transformer.js b/e2e/transform/babel-jest-async/transformer.js new file mode 100644 index 000000000000..de31652cb7bd --- /dev/null +++ b/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.createTransformer({ + presets: ['@babel/preset-flow'], + root: fileURLToPath(import.meta.url), + }), + // remove the synchronous functions + getCacheKey: undefined, + process: undefined, +}; diff --git a/e2e/transform/babel-jest-async/yarn.lock b/e2e/transform/babel-jest-async/yarn.lock new file mode 100644 index 000000000000..a54bf6305382 --- /dev/null +++ b/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