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

Allow skip process method if createTransformer definded #5999

Merged
merged 6 commits into from
Apr 17, 2018
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

### Features

* `[jest-runtime]` Allow for transform plugins to skip the definition process
method if createTransformer method was defined.
([#5999](https://github.com/facebook/jest/pull/5999))
* `[expect]` Add stack trace for async errors
([#6008](https://github.com/facebook/jest/pull/6008))
* `[jest-jasmine2]` Add stack trace for timeouts
Expand Down
70 changes: 70 additions & 0 deletions packages/jest-runtime/src/__tests__/script_transformer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,44 @@ jest.mock(
{virtual: true},
);

// Bad preprocessor
jest.mock(
'skipped-required-props-preprocessor',
() => {
return {};
},
{virtual: true},
);

// Bad preprocessor
jest.mock(
'skipped-required-create-transformer-props-preprocessor',
() => {
return {
createTransformer() {
return {};
},
};
},
{virtual: true},
);

jest.mock(
'skipped-process-method-preprocessor',
() => {
return {
createTransformer() {
const mockProcess = jest.fn();
mockProcess.mockReturnValue('code');
return {
process: mockProcess,
};
},
};
},
{virtual: true},
);

const getCachePath = (fs, config) => {
for (const path in mockFs) {
if (path.startsWith(config.cacheDirectory)) {
Expand Down Expand Up @@ -262,6 +300,38 @@ describe('ScriptTransformer', () => {
},
);

it("throws an error if `process` doesn't defined", () => {
Object.assign(config, {
transform: [['^.+\\.js$', 'skipped-required-props-preprocessor']],
});
const scriptTransformer = new ScriptTransformer(config);
expect(() =>
scriptTransformer.transformSource('sample.js', '', false),
).toThrow('Jest: a transform must export a `process` function.');
});

it('throws an error if createTransformer returns object without `process` method', () => {
Object.assign(config, {
transform: [
['^.+\\.js$', 'skipped-required-create-transformer-props-preprocessor'],
],
});
const scriptTransformer = new ScriptTransformer(config);
expect(() =>
scriptTransformer.transformSource('sample.js', '', false),
).toThrow('Jest: a transform must export a `process` function.');
});

it("shouldn't throw error without process method. But with corrent createTransformer method", () => {
Object.assign(config, {
transform: [['^.+\\.js$', 'skipped-process-method-preprocessor']],
});
const scriptTransformer = new ScriptTransformer(config);
expect(() =>
scriptTransformer.transformSource('sample.js', '', false),
).not.toThrow();
});

it('uses the supplied preprocessor', () => {
config = Object.assign(config, {
transform: [['^.+\\.js$', 'test_preprocessor']],
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-runtime/src/script_transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ export default class ScriptTransformer {

// $FlowFixMe
transform = (require(transformPath): Transformer);
if (typeof transform.createTransformer === 'function') {
transform = transform.createTransformer();
}
if (typeof transform.process !== 'function') {
throw new TypeError(
'Jest: a transform must export a `process` function.',
);
}
if (typeof transform.createTransformer === 'function') {
transform = transform.createTransformer();
}
this._transformCache.set(transformPath, transform);
}
return transform;
Expand Down