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

Provide better error checking for transformed content #3807

Merged
merged 1 commit into from
Jun 13, 2017
Merged
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
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(),
};
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we fit this in one line, or it's less readable?

() => ({process: jest.fn()})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prettier modified it to use this syntax :)

{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