Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
fix: support jest transformer that directly returns string
Browse files Browse the repository at this point in the history
  • Loading branch information
lachrist committed Feb 17, 2023
1 parent 5fd1a54 commit 000dd6f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
33 changes: 21 additions & 12 deletions components/transformer-jest/node/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,26 @@ const loadDispatchingEntry = ([pattern, { specifier, options }]) => [
},
];

const sanitizeSource = ({ code = null, map = null }, specifier) => {
assert(
!logErrorWhen(
typeof code !== "string",
"Transformer at %j should return an object whose code property is a string, got: %o",
specifier,
code,
),
"Transformer should return an object whose code property is a string",
ExternalAppmapError,
);
return { code, map };
const sanitizeSource = (source, specifier) => {
// This is no documented by transformers can directly
// return a string rather than an object.
// This is the case for `[email protected]`.
if (typeof source === "string") {
return { code: source, map: null };
} else {
const { code = null, map = null } = source;
assert(
!logErrorWhen(
typeof code !== "string",
"Transformer at %j should return an object whose code property is a string, got: %o",
specifier,
code,
),
"Transformer should return an object whose code property is a string",
ExternalAppmapError,
);
return { code, map };
}
};

const transform = (
Expand Down Expand Up @@ -112,6 +120,7 @@ export const compileCreateTransformer = (configuration) => {
);
source = sanitizeSource(
transformer.process(content, path, options),
specifier,
);
break;
}
Expand Down
22 changes: 8 additions & 14 deletions components/transformer-jest/node/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const {
Reflect: { get },
} = globalThis;

//////////////////////////////////////////////
// CJS && No SourceMap && With processAsync //
//////////////////////////////////////////////
///////////////////////////////////////////////////////////////
// CJS && No SourceMap && With processAsync && String Source //
///////////////////////////////////////////////////////////////

{
const specifier = toAbsoluteUrl(`${getUuid()}.cjs`, getTmpUrl());
Expand All @@ -34,14 +34,8 @@ const {
createTransformer: (options) => {
assertEqual(options, "options");
return {
process: (content, path, options) => ({
code: content + content,
map: null,
}),
processAsync: (content, path, options) => Promise.resolve({
code: content + content,
map: null,
}),
process: (content, path, options) => content + content,
processAsync: (content, path, options) => content + content,
};
},
},
Expand Down Expand Up @@ -120,9 +114,9 @@ const {
}
}

///////////////////////////////////////////////
// ESM && With Source Map && No processAsync //
///////////////////////////////////////////////
////////////////////////////////////////////////////////////////
// ESM && With Source Map && No processAsync && Object Source //
////////////////////////////////////////////////////////////////

{
const specifier = toAbsoluteUrl(`${getUuid()}.js`, getTmpUrl());
Expand Down

0 comments on commit 000dd6f

Please sign in to comment.