diff --git a/CHANGELOG.md b/CHANGELOG.md index e8493698859b..2fc5a7d18573 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - `[jest-runtime]` [**BREAKING**] Do not inject `global` variable into module wrapper ([#10644](https://github.com/facebook/jest/pull/10644)) - `[jest-transform]` Show enhanced `SyntaxError` message for all `SyntaxError`s ([#10749](https://github.com/facebook/jest/pull/10749)) - `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753)) +- `[jest-transform]` [**BREAKING**] All transformers must now return `{code: string, map?: SourceMap | string | null}`. Returning a string is no longer supported ([#10773](https://github.com/facebook/jest/pull/10773)) ### Chore & Maintenance diff --git a/packages/babel-jest/src/index.ts b/packages/babel-jest/src/index.ts index 07181095ccc7..91c55dd0965b 100644 --- a/packages/babel-jest/src/index.ts +++ b/packages/babel-jest/src/index.ts @@ -164,7 +164,7 @@ const createTransformer = ( } } - return src; + return {code: src}; }, }; }; diff --git a/packages/jest-repl/src/cli/repl.ts b/packages/jest-repl/src/cli/repl.ts index 831b2323489a..280ae085159a 100644 --- a/packages/jest-repl/src/cli/repl.ts +++ b/packages/jest-repl/src/cli/repl.ts @@ -30,10 +30,7 @@ const evalCommand: repl.REPLEval = ( jestGlobalConfig.replname || 'jest.js', jestProjectConfig, ); - cmd = - typeof transformResult === 'string' - ? transformResult - : transformResult.code; + cmd = transformResult.code; } result = runInThisContext(cmd); } catch (e) { diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index f99310ab3911..2161aa1dec90 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -208,10 +208,7 @@ export default class ScriptTransformer { canMapToInput: boolean, options: TransformOptions, ): TransformedSource { - const inputCode = typeof input === 'string' ? input : input.code; - const inputMap = typeof input === 'string' ? null : input.map; - - const result = babelTransform(inputCode, { + const result = babelTransform(input.code, { auxiliaryCommentBefore: ' istanbul ignore next ', babelrc: false, caller: { @@ -232,7 +229,7 @@ export default class ScriptTransformer { cwd: this._config.rootDir, exclude: [], extension: false, - inputSourceMap: inputMap, + inputSourceMap: input.map, useInlineSourceMaps: false, }, ], @@ -240,7 +237,7 @@ export default class ScriptTransformer { sourceMaps: canMapToInput ? 'both' : false, }); - if (result && result.code) { + if (result?.code) { return result as TransformResult; } @@ -297,14 +294,13 @@ export default class ScriptTransformer { options, ); - if (typeof processed === 'string') { - transformed.code = processed; - } else if (processed != null && typeof processed.code === 'string') { + if (processed != null && typeof processed.code === 'string') { transformed = processed; } else { throw new TypeError( - "Jest: a transform's `process` function must return a string, " + - 'or an object with `code` key containing this string.', + "Jest: a transform's `process` function must return an object with " + + '`code` key containing a string and an optional `map` key with ' + + 'the source map.', ); } } @@ -349,9 +345,8 @@ export default class ScriptTransformer { options, ); - code = - typeof instrumented === 'string' ? instrumented : instrumented.code; - map = typeof instrumented === 'string' ? null : instrumented.map; + code = instrumented.code; + map = instrumented.map; } else { code = transformed.code; } diff --git a/packages/jest-transform/src/types.ts b/packages/jest-transform/src/types.ts index 979c16c8ad42..521e66ef0bca 100644 --- a/packages/jest-transform/src/types.ts +++ b/packages/jest-transform/src/types.ts @@ -31,10 +31,10 @@ interface FixedRawSourceMap extends Omit { version: number; } -// TODO: For Jest 26 normalize this (always structured data, never a string) -export type TransformedSource = - | {code: string; map?: FixedRawSourceMap | string | null} - | string; +export type TransformedSource = { + code: string; + map?: FixedRawSourceMap | string | null; +}; export type TransformResult = TransformTypes.TransformResult;