Skip to content

Commit

Permalink
Uses the resolver to resolve the other configuration values
Browse files Browse the repository at this point in the history
  • Loading branch information
Maël Nison committed Apr 13, 2018
1 parent b80dc79 commit 06feaa9
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 6 deletions.
16 changes: 16 additions & 0 deletions integration-tests/__tests__/custom-resolver.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
Original file line number Diff line number Diff line change
@@ -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');
});
1 change: 1 addition & 0 deletions integration-tests/custom-resolver/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => {};
1 change: 1 addition & 0 deletions integration-tests/custom-resolver/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => {};
7 changes: 7 additions & 0 deletions integration-tests/custom-resolver/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "custom-resolver",
"jest": {
"globalSetup": "foo",
"resolver": "./resolver.js"
}
}
9 changes: 9 additions & 0 deletions integration-tests/custom-resolver/resolver.js
Original file line number Diff line number Diff line change
@@ -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);
}
};
33 changes: 28 additions & 5 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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':
Expand Down Expand Up @@ -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];
Expand All @@ -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':
Expand All @@ -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),
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 7 additions & 1 deletion packages/jest-config/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
);

Expand Down

0 comments on commit 06feaa9

Please sign in to comment.