From a1cbeb6b00787b14d70e151f62b44f97143bdb5a Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Sun, 17 Sep 2017 17:10:29 -0700 Subject: [PATCH 1/3] Support jest-preset.js files within Node modules. --- .../src/__tests__/normalize.test.js | 42 +++++++++++++++++++ packages/jest-config/src/normalize.js | 31 +++++++++----- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 19cbf8d62eb9..3145a8f16a12 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -859,6 +859,24 @@ describe('preset', () => { }), {virtual: true}, ); + jest.mock( + '/node_modules/with-json-ext/jest-preset.json', + () => ({ + moduleNameMapper: { + json: true, + }, + }), + {virtual: true}, + ); + jest.mock( + '/node_modules/with-js-ext/jest-preset.js', + () => ({ + moduleNameMapper: { + js: true, + }, + }), + {virtual: true}, + ); }); afterEach(() => { @@ -905,6 +923,30 @@ describe('preset', () => { }).not.toThrow(); }); + test('supports .json preset files', () => { + const {options} = normalize( + { + preset: 'with-json-ext', + rootDir: '/root/path/foo', + }, + {}, + ); + + expect(options.moduleNameMapper).toEqual([['json', true]]); + }); + + test('supports .js preset files', () => { + const {options} = normalize( + { + preset: 'with-js-ext', + rootDir: '/root/path/foo', + }, + {}, + ); + + expect(options.moduleNameMapper).toEqual([['js', true]]); + }); + test('merges with options', () => { const {options} = normalize( { diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index e7fba4434f83..87b91fa273b0 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -39,8 +39,8 @@ import DEPRECATED_CONFIG from './deprecated'; import setFromArgv from './set_from_argv'; import VALID_CONFIG from './valid_config'; const ERROR = `${BULLET}Validation Error`; -const JSON_EXTENSION = '.json'; -const PRESET_NAME = 'jest-preset' + JSON_EXTENSION; +const PRESET_EXTENSIONS = ['.json', '.js']; +const PRESET_NAME = 'jest-preset'; const createConfigError = message => new ValidationError(ERROR, message, DOCUMENTATION_NOTE); @@ -51,14 +51,25 @@ const setupPreset = ( ): InitialOptions => { let preset; const presetPath = _replaceRootDirInPath(options.rootDir, optionsPreset); - const presetModule = Resolver.findNodeModule( - presetPath.endsWith(JSON_EXTENSION) - ? presetPath - : path.join(presetPath, PRESET_NAME), - { - basedir: options.rootDir, - }, - ); + const foundModule = PRESET_EXTENSIONS.some(ext => { + const presetModule = Resolver.findNodeModule( + presetPath.charAt(0) === '.' || presetPath.endsWith(ext) + ? presetPath + : path.join(presetPath, PRESET_NAME + ext), + { + basedir: options.rootDir, + }, + ); + + try { + // $FlowFixMe + preset = (require(presetModule): InitialOptions); + } catch (error) { + return false; + } + + return true; + }); try { // $FlowFixMe From 821ddff66771a80c42fb3ffa12e950956d37a0f7 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Wed, 20 Dec 2017 22:02:53 -0800 Subject: [PATCH 2/3] Rebase fixes. --- docs/Configuration.md | 10 +++++++++- packages/jest-config/src/normalize.js | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index ad7c3d76d79f..09ef73b2437b 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -425,7 +425,15 @@ Activates notifications for test results. Default: `undefined` A preset that is used as a base for Jest's configuration. A preset should point -to an npm module that exports a `jest-preset.json` module on its top level. +to an npm module that exports a `jest-preset.json` or `jest-preset.js` module at its top level. + +Presets may also be relative filesystem paths. + +```json +{ + "preset": "./node_modules/foo-bar/jest-preset.js" +} +``` ### `projects` [array] diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 87b91fa273b0..974619bc8424 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -73,7 +73,7 @@ const setupPreset = ( try { // $FlowFixMe - preset = (require(presetModule): InitialOptions); + preset = (require(foundModule): InitialOptions); } catch (error) { if (error instanceof SyntaxError) { throw createConfigError( From dceacc5237e3cbe7c152a3c57c5be83efd14d15d Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Wed, 20 Dec 2017 22:21:44 -0800 Subject: [PATCH 3/3] Use resolver extensions options. --- packages/jest-config/src/normalize.js | 48 ++++++++++++++++----------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 974619bc8424..5e475fcfe03a 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -51,29 +51,39 @@ const setupPreset = ( ): InitialOptions => { let preset; const presetPath = _replaceRootDirInPath(options.rootDir, optionsPreset); - const foundModule = PRESET_EXTENSIONS.some(ext => { - const presetModule = Resolver.findNodeModule( - presetPath.charAt(0) === '.' || presetPath.endsWith(ext) - ? presetPath - : path.join(presetPath, PRESET_NAME + ext), - { - basedir: options.rootDir, - }, - ); - - try { - // $FlowFixMe - preset = (require(presetModule): InitialOptions); - } catch (error) { - return false; - } + const presetModule = Resolver.findNodeModule( + presetPath.charAt(0) === '.' + ? presetPath + : path.join(presetPath, PRESET_NAME), + { + basedir: options.rootDir, + extensions: PRESET_EXTENSIONS, + }, + ); - return true; - }); + // const foundModule = PRESET_EXTENSIONS.some(ext => { + // const presetModule = Resolver.findNodeModule( + // presetPath.charAt(0) === '.' || presetPath.endsWith(ext) + // ? presetPath + // : path.join(presetPath, PRESET_NAME + ext), + // { + // basedir: options.rootDir, + // }, + // ); + // + // try { + // // $FlowFixMe + // preset = (require(presetModule): InitialOptions); + // } catch (error) { + // return false; + // } + // + // return true; + // }); try { // $FlowFixMe - preset = (require(foundModule): InitialOptions); + preset = (require(presetModule): InitialOptions); } catch (error) { if (error instanceof SyntaxError) { throw createConfigError(