Skip to content

chore: transformers must return objects, not strings #10773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- `[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**] Refactor API of transformers to pass an options bag rather than separate `config` and other options
- `[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))
- `[pretty-format]` [**BREAKING**] Convert to ES Modules ([#10515](https://github.com/facebook/jest/pull/10515))

### Chore & Maintenance
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-jest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ const createTransformer: CreateTransformer = userOptions => {
}
}

return sourceText;
return {code: sourceText};
},
};
};
Expand Down
5 changes: 1 addition & 4 deletions packages/jest-repl/src/cli/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ const evalCommand: repl.REPLEval = (
supportsTopLevelAwait: false,
},
);
cmd =
typeof transformResult === 'string'
? transformResult
: transformResult.code;
cmd = transformResult.code;
}
result = runInThisContext(cmd);
} catch (e) {
Expand Down
23 changes: 9 additions & 14 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,7 @@ export default class ScriptTransformer {
canMapToInput: boolean,
options: ReducedTransformOptions,
): 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: {
Expand All @@ -228,15 +225,15 @@ export default class ScriptTransformer {
cwd: this._config.rootDir,
exclude: [],
extension: false,
inputSourceMap: inputMap,
inputSourceMap: input.map,
useInlineSourceMaps: false,
},
],
],
sourceMaps: canMapToInput ? 'both' : false,
});

if (result && result.code) {
if (result?.code) {
return result as TransformResult;
}

Expand Down Expand Up @@ -292,14 +289,13 @@ export default class ScriptTransformer {
configString: this._cache.configString,
});

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.',
);
}
}
Expand Down Expand Up @@ -344,9 +340,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;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-transform/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ interface FixedRawSourceMap extends Omit<RawSourceMap, 'version'> {
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;

Expand Down