From 06feaa992ae4d48cd0d7817df7153e0247919a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Thu, 12 Apr 2018 16:12:31 -0700 Subject: [PATCH] Uses the resolver to resolve the other configuration values --- .../__tests__/custom-resolver.test.js | 16 +++++++++ .../__tests__/custom-resolver.test.js | 11 +++++++ integration-tests/custom-resolver/bar.js | 1 + integration-tests/custom-resolver/foo.js | 1 + .../custom-resolver/package.json | 7 ++++ integration-tests/custom-resolver/resolver.js | 9 +++++ packages/jest-config/src/normalize.js | 33 ++++++++++++++++--- packages/jest-config/src/utils.js | 8 ++++- 8 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 integration-tests/__tests__/custom-resolver.test.js create mode 100644 integration-tests/custom-resolver/__tests__/custom-resolver.test.js create mode 100644 integration-tests/custom-resolver/bar.js create mode 100644 integration-tests/custom-resolver/foo.js create mode 100644 integration-tests/custom-resolver/package.json create mode 100644 integration-tests/custom-resolver/resolver.js diff --git a/integration-tests/__tests__/custom-resolver.test.js b/integration-tests/__tests__/custom-resolver.test.js new file mode 100644 index 000000000000..5e3582ec861f --- /dev/null +++ b/integration-tests/__tests__/custom-resolver.test.js @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ +'use strict'; + +const runJest = require('../runJest'); + +test('use the custom resolver', () => { + const result = runJest('custom-resolver'); + expect(result.status).toBe(0); +}); diff --git a/integration-tests/custom-resolver/__tests__/custom-resolver.test.js b/integration-tests/custom-resolver/__tests__/custom-resolver.test.js new file mode 100644 index 000000000000..ac91d1c88fa0 --- /dev/null +++ b/integration-tests/custom-resolver/__tests__/custom-resolver.test.js @@ -0,0 +1,11 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +'use strict'; + +test('should use the custom resolver', () => { + require('foo'); +}); diff --git a/integration-tests/custom-resolver/bar.js b/integration-tests/custom-resolver/bar.js new file mode 100644 index 000000000000..cc40a4649c9f --- /dev/null +++ b/integration-tests/custom-resolver/bar.js @@ -0,0 +1 @@ +module.exports = () => {}; diff --git a/integration-tests/custom-resolver/foo.js b/integration-tests/custom-resolver/foo.js new file mode 100644 index 000000000000..cc40a4649c9f --- /dev/null +++ b/integration-tests/custom-resolver/foo.js @@ -0,0 +1 @@ +module.exports = () => {}; diff --git a/integration-tests/custom-resolver/package.json b/integration-tests/custom-resolver/package.json new file mode 100644 index 000000000000..dd319619f468 --- /dev/null +++ b/integration-tests/custom-resolver/package.json @@ -0,0 +1,7 @@ +{ + "name": "custom-resolver", + "jest": { + "globalSetup": "foo", + "resolver": "./resolver.js" + } +} diff --git a/integration-tests/custom-resolver/resolver.js b/integration-tests/custom-resolver/resolver.js new file mode 100644 index 000000000000..0764e575a6e1 --- /dev/null +++ b/integration-tests/custom-resolver/resolver.js @@ -0,0 +1,9 @@ +const defaultResolver = require('jest-resolve/build/default_resolver').default; + +module.exports = (name, options) => { + if (name === 'foo' || name === 'bar') { + return `${__dirname}/${name}.js`; + } else { + return defaultResolver(name, options); + } +}; diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 93b3ce7ae6cb..ef07905c3ee6 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -363,7 +363,21 @@ export default function normalize(options: InitialOptions, argv: Argv) { const newOptions = Object.assign({}, DEFAULT_CONFIG); // Cast back to exact type options = (options: InitialOptions); + + if (options.resolver) { + newOptions.resolver = resolve( + null, + options.rootDir, + 'resolver', + options.resolver, + ); + } + Object.keys(options).reduce((newOptions, key) => { + // The resolver has been resolved separately; skip it + if (key === 'resolver') { + return newOptions; + } let value; switch (key) { case 'collectCoverageOnlyFrom': @@ -373,7 +387,9 @@ export default function normalize(options: InitialOptions, argv: Argv) { case 'snapshotSerializers': value = options[key] && - options[key].map(resolve.bind(null, options.rootDir, key)); + options[key].map( + resolve.bind(null, newOptions.resolver, options.rootDir, key), + ); break; case 'modulePaths': case 'roots': @@ -401,12 +417,13 @@ export default function normalize(options: InitialOptions, argv: Argv) { case 'globalSetup': case 'globalTeardown': case 'moduleLoader': - case 'resolver': case 'runner': case 'setupTestFrameworkScriptFile': case 'testResultsProcessor': case 'testRunner': - value = options[key] && resolve(options.rootDir, key, options[key]); + value = + options[key] && + resolve(newOptions.resolver, options.rootDir, key, options[key]); break; case 'moduleNameMapper': const moduleNameMapper = options[key]; @@ -423,7 +440,12 @@ export default function normalize(options: InitialOptions, argv: Argv) { transform && Object.keys(transform).map(regex => [ regex, - resolve(options.rootDir, key, transform[regex]), + resolve( + newOptions.resolver, + options.rootDir, + key, + transform[regex], + ), ]); break; case 'coveragePathIgnorePatterns': @@ -438,6 +460,7 @@ export default function normalize(options: InitialOptions, argv: Argv) { value = Object.assign({}, options[key]); if (value.hasteImplModulePath != null) { value.hasteImplModulePath = resolve( + newOptions.resolver, options.rootDir, 'haste.hasteImplModulePath', replaceRootDirInPath(options.rootDir, value.hasteImplModulePath), @@ -520,7 +543,7 @@ export default function normalize(options: InitialOptions, argv: Argv) { break; case 'watchPlugins': value = (options[key] || []).map(watchPlugin => - resolve(options.rootDir, key, watchPlugin), + resolve(newOptions.resolver, options.rootDir, key, watchPlugin), ); break; } diff --git a/packages/jest-config/src/utils.js b/packages/jest-config/src/utils.js index c85998edbfe4..d31cc17503e3 100644 --- a/packages/jest-config/src/utils.js +++ b/packages/jest-config/src/utils.js @@ -28,11 +28,17 @@ const createValidationError = (message: string) => { ); }; -export const resolve = (rootDir: string, key: string, filePath: Path) => { +export const resolve = ( + resolver: ?string, + rootDir: string, + key: string, + filePath: Path, +) => { const module = Resolver.findNodeModule( replaceRootDirInPath(rootDir, filePath), { basedir: rootDir, + resolver, }, );