Skip to content

Commit

Permalink
Provide better error checking for transformed content (#3807)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek authored and cpojer committed Jun 13, 2017
1 parent c3bc4c0 commit 79cbda4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/jest-mock/src/__tests__/jest-mock-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe('moduleMocker', () => {
expect(typeof multipleBoundFuncMock).toBe('function');
});

it('mocks methods that are bound after mocking', () => {
it('mocks methods that are bound after mocking', () => {
const fooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(() => {}),
);
Expand Down
7 changes: 6 additions & 1 deletion packages/jest-runtime/src/ScriptTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,13 @@ class ScriptTransformer {

if (typeof processed === 'string') {
transformed.code = processed;
} else {
} else 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.',
);
}
}

Expand Down
51 changes: 51 additions & 0 deletions packages/jest-runtime/src/__tests__/ScriptTransformer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ jest.mock(
{virtual: true},
);

jest.mock(
'passthrough-preprocessor',
() => {
return {
process: jest.fn(),
};
},
{virtual: true},
);

const getCachePath = (fs, config) => {
for (const path in mockFs) {
if (path.startsWith(config.cacheDirectory)) {
Expand All @@ -101,6 +111,9 @@ describe('ScriptTransformer', () => {

mockFs = object({
'/fruits/banana.js': ['module.exports = "banana";'].join('\n'),
'/fruits/grapefruit.js': [
'module.exports = function () { return "grapefruit"; }',
].join('\n'),
'/fruits/kiwi.js': ['module.exports = () => "kiwi";'].join('\n'),
'/node_modules/react.js': ['module.exports = "react";'].join('\n'),
'/styles/App.css': ['root {', ' font-family: Helvetica;', '}'].join(
Expand Down Expand Up @@ -180,6 +193,44 @@ describe('ScriptTransformer', () => {
expect(vm.Script.mock.calls[1][0]).toEqual(snapshot);
});

it(
"throws an error if `process` doesn't return a string or an object" +
'containing `code` key with processed string',
() => {
config = Object.assign(config, {
transform: [['^.+\\.js$', 'passthrough-preprocessor']],
});
const scriptTransformer = new ScriptTransformer(config);

const incorrectReturnValues = [
[undefined, '/fruits/banana.js'],
[{a: 'a'}, '/fruits/kiwi.js'],
[[], '/fruits/grapefruit.js'],
];

incorrectReturnValues.forEach(([returnValue, filePath]) => {
require('passthrough-preprocessor').process.mockReturnValue(
returnValue,
);
expect(() => scriptTransformer.transform(filePath, {})).toThrow(
'must return a string',
);
});

const correctReturnValues = [
['code', '/fruits/banana.js'],
[{code: 'code'}, '/fruits/kiwi.js'],
];

correctReturnValues.forEach(([returnValue, filePath]) => {
require('passthrough-preprocessor').process.mockReturnValue(
returnValue,
);
expect(() => scriptTransformer.transform(filePath, {})).not.toThrow();
});
},
);

it('uses the supplied preprocessor', () => {
config = Object.assign(config, {
transform: [['^.+\\.js$', 'test-preprocessor']],
Expand Down

0 comments on commit 79cbda4

Please sign in to comment.