Skip to content
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

feat(@jest/transform)!: require process() and processAsync() methods to always return structured data #12638

Merged
merged 12 commits into from Apr 6, 2022
10 changes: 6 additions & 4 deletions e2e/__tests__/dependencyClash.test.ts
Expand Up @@ -64,10 +64,12 @@ test('does not require project modules from inside node_modules', () => {
if (!threw) {
throw new Error('It used the wrong invariant module!');
}
return script.replace(
'INVALID CODE FRAGMENT THAT WILL BE REMOVED BY THE TRANSFORMER',
''
);
return {
code: script.replace(
'INVALID CODE FRAGMENT THAT WILL BE REMOVED BY THE TRANSFORMER',
'',
),
};
},
};
`,
Expand Down
Expand Up @@ -21,6 +21,6 @@ export default {

return {code: outputText, map: sourceMapText};
}
return sourceText;
return {code: sourceText};
},
};
2 changes: 1 addition & 1 deletion e2e/coverage-provider-v8/no-sourcemap/cssTransform.js
Expand Up @@ -7,5 +7,5 @@

module.exports = {
getCacheKey: () => 'cssTransform',
process: () => 'module.exports = {};',
process: () => ({code: 'module.exports = {};'}),
};
2 changes: 1 addition & 1 deletion e2e/coverage-remapping/typescriptPreprocessor.js
Expand Up @@ -22,6 +22,6 @@ module.exports = {
map: JSON.parse(result.sourceMapText),
};
}
return src;
return {code: src};
},
};
4 changes: 2 additions & 2 deletions e2e/global-setup-custom-transform/transformer.js
Expand Up @@ -12,9 +12,9 @@ const fileToTransform = require.resolve('./index.js');
module.exports = {
process(src, filename) {
if (filename === fileToTransform) {
return src.replace('hello', 'hello, world');
return {code: src.replace('hello', 'hello, world')};
}

return src;
return {code: src};
},
};
4 changes: 2 additions & 2 deletions e2e/snapshot-serializers/transformer.js
Expand Up @@ -10,8 +10,8 @@
module.exports = {
process(src, filename) {
if (/bar.js$/.test(filename)) {
return `${src};\nmodule.exports = createPlugin('bar');`;
return {code: `${src};\nmodule.exports = createPlugin('bar');`};
}
return src;
return {code: src};
},
};
23 changes: 14 additions & 9 deletions e2e/stack-trace-source-maps-with-coverage/preprocessor.js
Expand Up @@ -8,14 +8,19 @@
const tsc = require('typescript');

module.exports = {
process(src, path) {
return tsc.transpileModule(src, {
compilerOptions: {
inlineSourceMap: true,
module: tsc.ModuleKind.CommonJS,
target: 'es5',
},
fileName: path,
}).outputText;
process(sourceText, fileName) {
if (fileName.endsWith('.ts') || fileName.endsWith('.tsx')) {
const {outputText, sourceMapText} = tsc.transpileModule(sourceText, {
compilerOptions: {
inlineSourceMap: true,
module: tsc.ModuleKind.CommonJS,
target: 'es5',
},
fileName,
});

return {code: outputText, map: sourceMapText};
}
return {code: sourceText};
},
};
23 changes: 14 additions & 9 deletions e2e/stack-trace-source-maps/preprocessor.js
Expand Up @@ -8,14 +8,19 @@
const tsc = require('typescript');

module.exports = {
process(src, path) {
return tsc.transpileModule(src, {
compilerOptions: {
inlineSourceMap: true,
module: tsc.ModuleKind.CommonJS,
target: 'es5',
},
fileName: path,
}).outputText;
process(sourceText, fileName) {
if (fileName.endsWith('.ts') || fileName.endsWith('.tsx')) {
const {outputText, sourceMapText} = tsc.transpileModule(sourceText, {
compilerOptions: {
inlineSourceMap: true,
module: tsc.ModuleKind.CommonJS,
target: 'es5',
},
fileName,
});

return {code: outputText, map: sourceMapText};
}
return {code: sourceText};
},
};
2 changes: 1 addition & 1 deletion e2e/transform-linked-modules/preprocessor.js
Expand Up @@ -7,6 +7,6 @@

module.exports = {
process() {
return 'module.exports = "transformed"';
return {code: 'module.exports = "transformed"'};
},
};
12 changes: 7 additions & 5 deletions e2e/transform/async-transformer/my-transform.cjs
Expand Up @@ -24,12 +24,14 @@ module.exports = {
// we want to wait to ensure the module cache is populated with the correct module
await wait(100);

return src;
return {code: src};
}

return src.replace(
"export default 'It was not transformed!!'",
'export default 42',
);
return {
code: src.replace(
"export default 'It was not transformed!!'",
'export default 42',
),
};
},
};
4 changes: 2 additions & 2 deletions e2e/transform/cache/transformer.js
Expand Up @@ -6,11 +6,11 @@
*/

module.exports = {
process(src, path) {
process(code, path) {
if (path.includes('common')) {
console.log(path);
}

return src;
return {code};
},
};
Expand Up @@ -8,12 +8,12 @@
module.exports = {
canInstrument: true,
process(src, filename, options) {
src = `${src};\nglobalThis.__PREPROCESSED__ = true;`;
let code = `${src};\nglobalThis.__PREPROCESSED__ = true;`;

if (options.instrument) {
src = `${src};\nglobalThis.__INSTRUMENTED__ = true;`;
code = `${src};\nglobalThis.__INSTRUMENTED__ = true;`;
}

return src;
return {code};
},
};
4 changes: 2 additions & 2 deletions e2e/transform/esm-transformer/my-transform.mjs
Expand Up @@ -14,9 +14,9 @@ const fileToTransform = require.resolve('./module');
export default {
process(src, filepath) {
if (filepath === fileToTransform) {
return 'module.exports = 42;';
return {code: 'module.exports = 42;'};
}

return src;
return {code: src};
},
};
18 changes: 10 additions & 8 deletions e2e/transform/multiple-transformers/cssPreprocessor.js
Expand Up @@ -7,13 +7,15 @@

module.exports = {
process() {
return `
module.exports = {
root: 'App-root',
header: 'App-header',
logo: 'App-logo',
intro: 'App-intro',
};
`;
const code = `
module.exports = {
root: 'App-root',
header: 'App-header',
logo: 'App-logo',
intro: 'App-intro',
};
`;

return {code};
},
};
8 changes: 5 additions & 3 deletions e2e/transform/multiple-transformers/filePreprocessor.js
Expand Up @@ -9,8 +9,10 @@ const path = require('path');

module.exports = {
process(src, filename) {
return `
module.exports = '${path.basename(filename)}';
`;
const code = `
module.exports = '${path.basename(filename)}';
`;

return {code};
},
};
19 changes: 10 additions & 9 deletions e2e/typescript-coverage/typescriptPreprocessor.js
Expand Up @@ -8,18 +8,19 @@
const tsc = require('typescript');

module.exports = {
process(src, path) {
if (path.endsWith('.ts') || path.endsWith('.tsx')) {
return tsc.transpile(
src,
{
process(sourceText, fileName) {
if (fileName.endsWith('.ts') || fileName.endsWith('.tsx')) {
const {outputText, sourceMapText} = tsc.transpileModule(sourceText, {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
compilerOptions: {
jsx: tsc.JsxEmit.React,
module: tsc.ModuleKind.CommonJS,
sourceMap: true, // if code is transformed, source map is necessary for coverage
},
path,
[],
);
fileName,
});

return {code: outputText, map: sourceMapText};
}
return src;
return {code: sourceText};
},
};
4 changes: 2 additions & 2 deletions packages/babel-jest/src/index.ts
Expand Up @@ -244,7 +244,7 @@ export const createTransformer: TransformerCreator<
}
}

return sourceText;
return {code: sourceText};
},
async processAsync(sourceText, sourcePath, transformOptions) {
const babelOptions = await loadBabelOptionsAsync(
Expand All @@ -266,7 +266,7 @@ export const createTransformer: TransformerCreator<
}
}

return sourceText;
return {code: sourceText};
},
};
};
Expand Down
Expand Up @@ -10,5 +10,5 @@
module.exports.process = source => {
const json = JSON.parse(source);
Object.keys(json).forEach(k => (json[k] = k));
return JSON.stringify(json);
return {code: JSON.stringify(json)};
};
Expand Up @@ -7,4 +7,6 @@

'use strict';

module.exports.process = () => "throw new Error('preprocessor must not run.');";
module.exports.process = () => ({
code: "throw new Error('preprocessor must not run.');",
});
6 changes: 2 additions & 4 deletions packages/jest-transform/src/ScriptTransformer.ts
Expand Up @@ -379,9 +379,7 @@ class ScriptTransformer {
};

if (transformer && shouldCallTransform) {
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 Error(makeInvalidReturnValueError());
Expand Down Expand Up @@ -483,7 +481,7 @@ class ScriptTransformer {
};
}

let processed = null;
let processed: TransformedSource | null = null;

let shouldCallTransform = false;

Expand Down