Skip to content

Commit

Permalink
Allow skip process method if createTransformer definded (#5999)
Browse files Browse the repository at this point in the history
* Allow skip process method if createTransformer definded

* Add tests for Transformer

* Add change log

* Add test case

* Fix prettylint
  • Loading branch information
Connormiha authored and cpojer committed Apr 17, 2018
1 parent 25bc4bf commit a6d26b4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
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

0 comments on commit a6d26b4

Please sign in to comment.