Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uses the resolver to resolve the other configuration values #5976

Merged
merged 2 commits into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what ensure that we're using the custom resolver in the configuration

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

"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