diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e9c4c1ef83f..34ea80f615cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -94,7 +94,9 @@ * `[jest-util]` Fix handling of NaN/Infinity in mock timer delay ([#5966](https://github.com/facebook/jest/pull/5966)) * `[jest-resolve]` Generalise test for package main entries equivalent to ".". - ([#5968)](https://github.com/facebook/jest/pull/5968) + ([#5968](https://github.com/facebook/jest/pull/5968)) +* `[jest-config]` Ensure that custom resolvers are used when resolving the configuration + ([#5976](https://github.com/facebook/jest/pull/5976)) ### Chore & Maintenance 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, }, );