Skip to content

Commit

Permalink
Add tests for Transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
Connormiha committed Apr 16, 2018
1 parent f0926d6 commit 6d208ee
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
50 changes: 50 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,34 @@ jest.mock(
{virtual: true},
);

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

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

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

const getCachePath = (fs, config) => {
for (const path in mockFs) {
if (path.startsWith(config.cacheDirectory)) {
Expand Down Expand Up @@ -262,6 +290,28 @@ 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('uses the supplied preprocessor', () => {
config = Object.assign(config, {
transform: [['^.+\\.js$', 'test_preprocessor']],
Expand Down
13 changes: 5 additions & 8 deletions packages/jest-runtime/src/script_transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,14 @@ export default class ScriptTransformer {

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

0 comments on commit 6d208ee

Please sign in to comment.