Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Features

- `[babel-jest]` Support passing `supportsDynamicImport` and `supportsStaticESM` ([#9766](https://github.com/facebook/jest/pull/9766))

### Fixes

### Chore & Maintenance
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/transform.test.ts.snap
Original file line number Diff line number Diff line change
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:191:13)
at loadBabelConfig (../../../packages/babel-jest/build/index.js:218:13)
`;

exports[`babel-jest instruments only specific files and collects coverage 1`] = `
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dependencies": {
"@jest/transform": "^25.2.6",
"@jest/types": "^25.2.6",
"@types/babel__core": "^7.1.0",
"@types/babel__core": "^7.1.7",
"babel-plugin-istanbul": "^6.0.0",
"babel-preset-jest": "^25.2.6",
"chalk": "^3.0.0",
Expand Down
70 changes: 66 additions & 4 deletions packages/babel-jest/src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
* LICENSE file in the root directory of this source tree.
*/

import babelJest from '../index';
import babelJest = require('../index');
import {loadPartialConfig} from '../loadBabelConfig';
import {makeProjectConfig} from '../../../../TestUtils';

jest.mock('../loadBabelConfig', () => {
const actual = jest.requireActual('@babel/core');

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

//Mock data for all the tests
const sourceString = `
const sum = (a, b) => a+b;
Expand All @@ -21,7 +30,11 @@ const customMultiply = (obj, mul) => {
customMultiply({a: 32, dummy: "test"}, 2);
`;

test(`Returns source string with inline maps when no transformOptions is passed`, () => {
beforeEach(() => {
jest.clearAllMocks();
});

test('Returns source string with inline maps when no transformOptions is passed', () => {
const result = babelJest.process(
sourceString,
'dummy_path.js',
Expand All @@ -32,6 +45,55 @@ test(`Returns source string with inline maps when no transformOptions is passed`
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');
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([
[
{
supportsDynamicImport: true,
supportsStaticESM: true,
},
{
supportsDynamicImport: true,
supportsStaticESM: true,
},
],
[
{
supportsDynamicImport: false,
supportsStaticESM: false,
},
{
supportsDynamicImport: false,
supportsStaticESM: false,
},
],
[
{supportsStaticESM: false},
{
supportsDynamicImport: false,
supportsStaticESM: false,
},
],
[
{supportsDynamicImport: true},
{
supportsDynamicImport: true,
supportsStaticESM: false,
},
],
])('%j -> %j', (input, output) => {
babelJest.process(sourceString, 'dummy_path.js', makeProjectConfig(), {
instrument: false,
...input,
});

expect(loadPartialConfig).toHaveBeenCalledTimes(1);
expect(loadPartialConfig).toHaveBeenCalledWith(
expect.objectContaining({caller: {name: 'babel-jest', ...output}}),
);
});
});
38 changes: 33 additions & 5 deletions packages/babel-jest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
PartialConfig,
TransformOptions,
transformSync as babelTransform,
loadPartialConfig,
} from '@babel/core';
import {loadPartialConfig} from './loadBabelConfig';
import chalk = require('chalk');
import slash = require('slash');

Expand All @@ -35,7 +35,9 @@ const createTransformer = (
...options,
caller: {
name: 'babel-jest',
supportsDynamicImport: false,
supportsStaticESM: false,
...options.caller,
},
compact: false,
plugins: (options && options.plugins) || [],
Expand All @@ -46,9 +48,23 @@ const createTransformer = (
function loadBabelConfig(
cwd: Config.Path,
filename: Config.Path,
supportsDynamicImport?: boolean,
supportsStaticESM?: boolean,
): PartialConfig {
// `cwd` first to allow incoming options to override it
const babelConfig = loadPartialConfig({cwd, ...options, filename});
const babelConfig = loadPartialConfig({
cwd,
...options,
caller: {
name: 'babel-jest',
...options.caller,
supportsDynamicImport:
supportsDynamicImport ?? options.caller?.supportsDynamicImport,
supportsStaticESM:
supportsStaticESM ?? options.caller?.supportsStaticESM,
},
filename,
});

if (!babelConfig) {
throw new Error(
Expand All @@ -69,9 +85,14 @@ const createTransformer = (
fileData,
filename,
configString,
{config, instrument, rootDir},
{config, instrument, rootDir, supportsDynamicImport, supportsStaticESM},
) {
const babelOptions = loadBabelConfig(config.cwd, filename);
const babelOptions = loadBabelConfig(
config.cwd,
filename,
supportsDynamicImport,
supportsStaticESM,
);
const configPath = [
babelOptions.config || '',
babelOptions.babelrc || '',
Expand All @@ -98,7 +119,14 @@ const createTransformer = (
.digest('hex');
},
process(src, filename, config, transformOptions) {
const babelOptions = {...loadBabelConfig(config.cwd, filename).options};
const babelOptions = {
...loadBabelConfig(
config.cwd,
filename,
transformOptions?.supportsDynamicImport,
transformOptions?.supportsStaticESM,
).options,
};

if (transformOptions && transformOptions.instrument) {
babelOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
Expand Down
9 changes: 9 additions & 0 deletions packages/babel-jest/src/loadBabelConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 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.
*/

// this is a separate file so it can be mocked in tests
export {loadPartialConfig} from '@babel/core';
6 changes: 1 addition & 5 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,7 @@ class Runtime {
): TransformationOptions {
return {
...options,
changedFiles: this._coverageOptions.changedFiles,
collectCoverage: this._coverageOptions.collectCoverage,
collectCoverageFrom: this._coverageOptions.collectCoverageFrom,
collectCoverageOnlyFrom: this._coverageOptions.collectCoverageOnlyFrom,
coverageProvider: this._coverageOptions.coverageProvider,
...this._coverageOptions,
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/jest-runtime/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
{"path": "../jest-snapshot"},
{"path": "../jest-source-map"},
{"path": "../jest-test-result"},
{"path": "../jest-transform"},
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this was missing, sometimes leading to weird type errors

{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../jest-validate"},
Expand Down
15 changes: 10 additions & 5 deletions packages/jest-transform/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export type Options = ShouldInstrumentOptions &
Partial<{
isCoreModule: boolean;
isInternalModule: boolean;
supportsDynamicImport: boolean;
supportsStaticESM: boolean;
}>;

// extends directly after https://github.com/sandersn/downlevel-dts/issues/33 is fixed
Expand All @@ -38,15 +40,18 @@ export type TransformedSource =

export type TransformResult = TransformTypes.TransformResult;

export type TransformOptions = {
export interface TransformOptions {
instrument: boolean;
};
// names are copied from babel
supportsDynamicImport?: boolean;
supportsStaticESM?: boolean;
}

export type CacheKeyOptions = {
// TODO: For Jest 26 we should combine these into one options shape
export interface CacheKeyOptions extends TransformOptions {
config: Config.ProjectConfig;
instrument: boolean;
rootDir: string;
};
}

export interface Transformer {
canInstrument?: boolean;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2137,10 +2137,10 @@
resolved "https://registry.yarnpkg.com/@types/babel__code-frame/-/babel__code-frame-7.0.1.tgz#baf2529c4abbfb5e4008c845efcfe39a187e2f99"
integrity sha512-FFfbQozKxYmOnCKFYV+EQprjBI7u2yaNc2ly/K9AhzyC8MzXtCtSRqptpw+HUJxhwCOo5mLwf1ATmzyhOaVbDg==

"@types/babel__core@^7.0.0", "@types/babel__core@^7.0.4", "@types/babel__core@^7.1.0":
version "7.1.6"
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.6.tgz#16ff42a5ae203c9af1c6e190ed1f30f83207b610"
integrity sha512-tTnhWszAqvXnhW7m5jQU9PomXSiKXk2sFxpahXvI20SZKu9ylPi8WtIxueZ6ehDWikPT0jeFujMj3X4ZHuf3Tg==
"@types/babel__core@^7.0.0", "@types/babel__core@^7.0.4", "@types/babel__core@^7.1.0", "@types/babel__core@^7.1.7":
version "7.1.7"
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.7.tgz#1dacad8840364a57c98d0dd4855c6dd3752c6b89"
integrity sha512-RL62NqSFPCDK2FM1pSDH0scHpJvsXtZNiYlMB73DgPBaG1E38ZYVL+ei5EkWRbr+KC4YNiAUNBnRj+bgwpgjMw==
dependencies:
"@babel/parser" "^7.1.0"
"@babel/types" "^7.0.0"
Expand Down