Skip to content

Commit 9419034

Browse files
MrJohzSimenB
authored andcommittedDec 28, 2019
Resolve dynamic dependencies correctly when a mapping exists (#9303)
1 parent a2fcda6 commit 9419034

File tree

8 files changed

+87
-2
lines changed

8 files changed

+87
-2
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
- `[jest-reporters]` Make node-notifier an optional dependency ([#8918](https://github.com/facebook/jest/pull/8918))
6464
- `[jest-reporters]` Make all arguments to methods on `BaseReporter` optional ([#9159](https://github.com/facebook/jest/pull/9159))
6565
- `[jest-resolve]`: Set MODULE_NOT_FOUND as error code when module is not resolved from paths ([#8487](https://github.com/facebook/jest/pull/8487))
66+
- `[jest-resolve-dependencies]` Handle dynamic dependencies correctly even when using module maps ([#9303](https://github.com/facebook/jest/pull/9303))
6667
- `[jest-snapshot]` Remove only the added newlines in multiline snapshots ([#8859](https://github.com/facebook/jest/pull/8859))
6768
- `[jest-snapshot]` Distinguish empty string from external snapshot not written ([#8880](https://github.com/facebook/jest/pull/8880))
6869
- `[jest-snapshot]` [**BREAKING**] Distinguish empty string from internal snapshot not written ([#8898](https://github.com/facebook/jest/pull/8898))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import * as path from 'path';
9+
import {json as runWithJson} from '../runJest';
10+
11+
const dir = path.resolve(__dirname, '../dynamic-require-dependencies');
12+
13+
test('successfully runs tests with dynamic dependencies', () => {
14+
const {json} = runWithJson(dir, ['--findRelatedTests', 'dynamicRequire.js']);
15+
expect(json.success).toBe(true);
16+
expect(json.numTotalTests).toBe(2);
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
test('loading a file with a dynamic local require should work', () => {
9+
const {withStandardResolution} = require('../dynamicRequire');
10+
expect(withStandardResolution()).toBe(1);
11+
});
12+
13+
test('loading a file with a dynamic require and custom resolve should work', () => {
14+
const {withCustomResolution} = require('../dynamicRequire');
15+
expect(withCustomResolution()).toBe(1);
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
const dynamicModuleName = 'source';
9+
10+
module.exports.withStandardResolution = () =>
11+
require(`./${dynamicModuleName}.js`);
12+
module.exports.withCustomResolution = () =>
13+
require(`$asdf/${dynamicModuleName}.js`);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"jest": {
3+
"moduleNameMapper": {
4+
"^\\$asdf/(.*)$": "<rootDir>/$1"
5+
}
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
module.exports = 1;

‎packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import Resolver = require('jest-resolve');
89
import {tmpdir} from 'os';
910
import * as path from 'path';
1011
import {Config} from '@jest/types';
@@ -15,6 +16,7 @@ import DependencyResolver from '../index';
1516

1617
const maxWorkers = 1;
1718
let dependencyResolver: DependencyResolver;
19+
let runtimeContextResolver: Resolver;
1820
let Runtime;
1921
let config: Config.ProjectConfig;
2022
const cases: Record<string, jest.Mock> = {
@@ -29,11 +31,13 @@ beforeEach(() => {
2931
config = makeProjectConfig({
3032
cacheDirectory: path.resolve(tmpdir(), 'jest-resolve-dependencies-test'),
3133
moduleDirectories: ['node_modules'],
34+
moduleNameMapper: [['^\\$asdf/(.*)$', '<rootDir>/$1']],
3235
rootDir: '.',
3336
roots: ['./packages/jest-resolve-dependencies'],
3437
});
3538
return Runtime.createContext(config, {maxWorkers, watchman: false}).then(
3639
(runtimeContext: any) => {
40+
runtimeContextResolver = runtimeContext.resolver;
3741
dependencyResolver = new DependencyResolver(
3842
runtimeContext.resolver,
3943
runtimeContext.hasteFS,
@@ -106,3 +110,18 @@ test('resolves inverse dependencies from available snapshot', () => {
106110
]),
107111
);
108112
});
113+
114+
test('resolves dependencies correctly when dependency resolution fails', () => {
115+
jest.spyOn(runtimeContextResolver, 'resolveModule').mockImplementation(() => {
116+
throw new Error('resolveModule has failed');
117+
});
118+
jest.spyOn(runtimeContextResolver, 'getMockModule').mockImplementation(() => {
119+
throw new Error('getMockModule has failed');
120+
});
121+
122+
const resolved = dependencyResolver.resolve(
123+
path.resolve(__dirname, '__fixtures__', 'file.test.js'),
124+
);
125+
126+
expect(resolved).toEqual([]);
127+
});

‎packages/jest-resolve-dependencies/src/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ class DependencyResolver {
5858
dependency,
5959
options,
6060
);
61-
} catch (e) {
62-
resolvedDependency = this._resolver.getMockModule(file, dependency);
61+
} catch {
62+
try {
63+
resolvedDependency = this._resolver.getMockModule(file, dependency);
64+
} catch {
65+
// leave resolvedDependency as undefined if nothing can be found
66+
}
6367
}
6468

6569
if (resolvedDependency) {

0 commit comments

Comments
 (0)
Please sign in to comment.