Skip to content

Commit

Permalink
chore: support more caller options in babel-jest (#10752)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Oct 31, 2020
1 parent 16c082c commit 95169d3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- `[jest-config]` Throw correct error for missing preset modules ([#10737](https://github.com/facebook/jest/pull/10737))
- `[jest-resolve-dependencies]` Resolve mocks as dependencies ([#10713](https://github.com/facebook/jest/pull/10713))
- `[jest-runtime]` Handle file URLs in dynamic imports ([#10744](https://github.com/facebook/jest/pull/10744))
- `[jest-runtime, babel-jest]` Pass more ESM options to `@jest/transform` ([#10752](https://github.com/facebook/jest/pull/10752))
- `[jest-transform]` Link to ESM docs on syntax errors ([#10748](https://github.com/facebook/jest/pull/10748))

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

exports[`babel-jest instruments only specific files and collects coverage 1`] = `
Expand Down
16 changes: 16 additions & 0 deletions packages/babel-jest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ interface BabelJestTransformOptions extends TransformOptions {
sourceMaps: 'both';
}

// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/49267
declare module '@babel/core' {
interface TransformCaller {
supportsExportNamespaceFrom?: boolean;
supportsTopLevelAwait?: boolean;
}
}

const createTransformer = (
userOptions?: TransformOptions | null,
): BabelJestTransformer => {
Expand All @@ -49,7 +57,9 @@ const createTransformer = (
caller: {
name: 'babel-jest',
supportsDynamicImport: false,
supportsExportNamespaceFrom: false,
supportsStaticESM: false,
supportsTopLevelAwait: false,
...inputOptions.caller,
},
compact: false,
Expand All @@ -72,9 +82,15 @@ const createTransformer = (
supportsDynamicImport:
transformOptions?.supportsDynamicImport ??
options.caller.supportsDynamicImport,
supportsExportNamespaceFrom:
transformOptions?.supportsExportNamespaceFrom ??
options.caller.supportsExportNamespaceFrom,
supportsStaticESM:
transformOptions?.supportsStaticESM ??
options.caller.supportsStaticESM,
supportsTopLevelAwait:
transformOptions?.supportsTopLevelAwait ??
options.caller.supportsTopLevelAwait,
},
filename,
});
Expand Down
26 changes: 22 additions & 4 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {formatStackTrace, separateMessageFromStack} from 'jest-message-util';
import {createDirectory, deepCyclicCopy} from 'jest-util';
import {escapePathForRegex} from 'jest-regex-util';
import {
CallerTransformOptions,
ScriptTransformer,
ShouldInstrumentOptions,
TransformationOptions,
Expand Down Expand Up @@ -75,16 +76,16 @@ type HasteMapOptions = {
watchman: boolean;
};

type InternalModuleOptions = {
interface InternalModuleOptions extends Required<CallerTransformOptions> {
isInternalModule: boolean;
supportsDynamicImport: boolean;
supportsStaticESM: boolean;
};
}

const defaultTransformOptions: InternalModuleOptions = {
isInternalModule: false,
supportsDynamicImport: esmIsAvailable,
supportsExportNamespaceFrom: false,
supportsStaticESM: false,
supportsTopLevelAwait: false,
};

type InitialModule = Omit<Module, 'require' | 'parent' | 'paths'>;
Expand Down Expand Up @@ -141,6 +142,19 @@ type RunScriptEvalResult = {[EVAL_RESULT_VARIABLE]: ModuleWrapper};

const runtimeSupportsVmModules = typeof SyntheticModule === 'function';

const supportsTopLevelAwait =
runtimeSupportsVmModules &&
(() => {
try {
// eslint-disable-next-line no-new
new SourceTextModule('await Promise.resolve()');

return true;
} catch {
return false;
}
})();

class Runtime {
private _cacheFS: StringMap;
private _config: Config.ProjectConfig;
Expand Down Expand Up @@ -378,7 +392,9 @@ class Runtime {
const transformedCode = this.transformFile(modulePath, {
isInternalModule: false,
supportsDynamicImport: true,
supportsExportNamespaceFrom: true,
supportsStaticESM: true,
supportsTopLevelAwait,
});

const module = new SourceTextModule(transformedCode, {
Expand Down Expand Up @@ -608,7 +624,9 @@ class Runtime {
return this.requireModule<T>(from, to, {
isInternalModule: true,
supportsDynamicImport: esmIsAvailable,
supportsExportNamespaceFrom: false,
supportsStaticESM: false,
supportsTopLevelAwait: false,
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/jest-transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
export {default as shouldInstrument} from './shouldInstrument';
export type {
CacheKeyOptions,
CallerTransformOptions,
Transformer,
ShouldInstrumentOptions,
Options as TransformationOptions,
Expand Down
16 changes: 10 additions & 6 deletions packages/jest-transform/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ export type Options = ShouldInstrumentOptions &
Partial<{
isCoreModule: boolean;
isInternalModule: boolean;
supportsDynamicImport: boolean;
supportsStaticESM: boolean;
}>;
}> &
CallerTransformOptions;

// This is fixed in [email protected], but we can't upgrade yet since it's async
interface FixedRawSourceMap extends Omit<RawSourceMap, 'version'> {
Expand All @@ -39,11 +38,16 @@ export type TransformedSource =

export type TransformResult = TransformTypes.TransformResult;

export interface TransformOptions {
instrument: boolean;
// names are copied from babel
export interface CallerTransformOptions {
// names are copied from babel: https://babeljs.io/docs/en/options#caller
supportsDynamicImport?: boolean;
supportsExportNamespaceFrom?: boolean;
supportsStaticESM?: boolean;
supportsTopLevelAwait?: boolean;
}

export interface TransformOptions extends CallerTransformOptions {
instrument: boolean;
}

// TODO: For Jest 26 we should combine these into one options shape
Expand Down

0 comments on commit 95169d3

Please sign in to comment.