From f0926d63bd2d5a159ecd971acb8213a7ff1dac27 Mon Sep 17 00:00:00 2001 From: Mihail Bodrov Date: Mon, 16 Apr 2018 03:13:53 +0300 Subject: [PATCH 1/5] Allow skip process method if createTransformer definded --- packages/jest-runtime/src/script_transformer.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/jest-runtime/src/script_transformer.js b/packages/jest-runtime/src/script_transformer.js index 43604830a335..8216c8b770d8 100644 --- a/packages/jest-runtime/src/script_transformer.js +++ b/packages/jest-runtime/src/script_transformer.js @@ -136,9 +136,12 @@ export default class ScriptTransformer { // $FlowFixMe transform = (require(transformPath): Transformer); - if (typeof transform.process !== 'function') { + if ( + typeof transform.process !== 'function' && + typeof transform.createTransformer !== 'function' + ) { throw new TypeError( - 'Jest: a transform must export a `process` function.', + 'Jest: a transform must export a `process` or `createTransformer` function.', ); } if (typeof transform.createTransformer === 'function') { From 61b06fcb7b84b75f1b2db6e3d290fd2fa7ce5e97 Mon Sep 17 00:00:00 2001 From: Mihail Bodrov Date: Tue, 17 Apr 2018 00:56:58 +0300 Subject: [PATCH 2/5] Add tests for Transformer --- .../src/__tests__/script_transformer.test.js | 42 +++++++++++++++++++ .../jest-runtime/src/script_transformer.js | 13 +++--- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/packages/jest-runtime/src/__tests__/script_transformer.test.js b/packages/jest-runtime/src/__tests__/script_transformer.test.js index a7b74ffb95c6..4f4a30fae925 100644 --- a/packages/jest-runtime/src/__tests__/script_transformer.test.js +++ b/packages/jest-runtime/src/__tests__/script_transformer.test.js @@ -85,6 +85,26 @@ jest.mock( {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)) { @@ -262,6 +282,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']], diff --git a/packages/jest-runtime/src/script_transformer.js b/packages/jest-runtime/src/script_transformer.js index 8216c8b770d8..75397d85a96f 100644 --- a/packages/jest-runtime/src/script_transformer.js +++ b/packages/jest-runtime/src/script_transformer.js @@ -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; From 268744d8c150ef457725cd5fb04fcab2882998e6 Mon Sep 17 00:00:00 2001 From: Mihail Bodrov Date: Tue, 17 Apr 2018 01:28:28 +0300 Subject: [PATCH 3/5] Add change log --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a2785cb7c69..9c6235e401df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) * `[jest-runtime]` Prevent modules from marking themselves as their own parent ([#5235](https://github.com/facebook/jest/issues/5235)) * `[jest-mock]` Add support for auto-mocking generator functions From d501ef1fbb2b11bf614bf8b15c669250e380f1d4 Mon Sep 17 00:00:00 2001 From: Mihail Bodrov Date: Tue, 17 Apr 2018 01:42:41 +0300 Subject: [PATCH 4/5] Add test case --- .../src/__tests__/script_transformer.test.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/packages/jest-runtime/src/__tests__/script_transformer.test.js b/packages/jest-runtime/src/__tests__/script_transformer.test.js index 4f4a30fae925..b0ee5c1e5512 100644 --- a/packages/jest-runtime/src/__tests__/script_transformer.test.js +++ b/packages/jest-runtime/src/__tests__/script_transformer.test.js @@ -85,6 +85,7 @@ jest.mock( {virtual: true}, ); +// Bad preprocessor jest.mock( 'skipped-required-props-preprocessor', () => { @@ -93,6 +94,7 @@ jest.mock( {virtual: true}, ); +// Bad preprocessor jest.mock( 'skipped-required-create-transformer-props-preprocessor', () => { @@ -105,6 +107,22 @@ jest.mock( {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)) { @@ -304,6 +322,16 @@ describe('ScriptTransformer', () => { ).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']], From 9a004bb8a9c27f7c5901df5fb24f9dca15a2ec7a Mon Sep 17 00:00:00 2001 From: Mihail Bodrov Date: Tue, 17 Apr 2018 02:02:11 +0300 Subject: [PATCH 5/5] Fix prettylint --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c6235e401df..6f1eb5f26497 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,8 @@ ### Features -* `[jest-runtime]` Allow for transform plugins to skip the definition process method - if createTransformer method was defined. +* `[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)) * `[jest-runtime]` Prevent modules from marking themselves as their own parent ([#5235](https://github.com/facebook/jest/issues/5235))