Skip to content

Commit 7b1568d

Browse files
brapifrapetevdp
andauthored
fix(jest-resolve-dependencies): resolve mocks as dependencies (#10713)
Co-authored-by: grey275 <[email protected]>
1 parent c49e2e3 commit 7b1568d

File tree

7 files changed

+91
-2
lines changed

7 files changed

+91
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
- `[jest-config]` Fix bug introduced in watch mode by PR[#10678](https://github.com/facebook/jest/pull/10678/files#r511037803) ([#10692](https://github.com/facebook/jest/pull/10692))
1010
- `[expect]` Stop modifying the sample in `expect.objectContaining()` ([#10711](https://github.com/facebook/jest/pull/10711))
11+
- `[jest-resolve-dependencies]` Resolve mocks as dependencies ([#10713](https://github.com/facebook/jest/pull/10713))
1112

1213
### Chore & Maintenance
1314

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
9+
module.exports = str => str;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
9+
module.exports = jest.fn();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
9+
module.exports = str => str;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
9+
require('./file');
10+
require('fake-node-module');

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ test('resolves dependencies for existing path', () => {
6161
]);
6262
});
6363

64+
test('includes the mocks of dependencies as dependencies', () => {
65+
const resolved = dependencyResolver.resolve(
66+
path.resolve(__dirname, '__fixtures__/hasMocked/file.test.js'),
67+
);
68+
69+
expect(resolved).toEqual([
70+
expect.stringContaining(path.join('hasMocked', 'file.js')),
71+
expect.stringContaining(path.join('hasMocked', '__mocks__', 'file.js')),
72+
expect.stringContaining(path.join('__mocks__', 'fake-node-module.js')),
73+
]);
74+
});
75+
6476
test('resolves dependencies for scoped packages', () => {
6577
const resolved = dependencyResolver.resolve(
6678
path.resolve(__dirname, '__fixtures__', 'scoped.js'),
@@ -92,6 +104,19 @@ test('resolves inverse dependencies for existing path', () => {
92104
]);
93105
});
94106

107+
test('resolves inverse dependencies of mock', () => {
108+
const paths = new Set([
109+
path.resolve(__dirname, '__fixtures__/hasMocked/__mocks__/file.js'),
110+
]);
111+
const resolved = dependencyResolver.resolveInverse(paths, filter);
112+
113+
expect(resolved).toEqual([
114+
expect.stringContaining(
115+
path.join('__tests__/__fixtures__/hasMocked/file.test.js'),
116+
),
117+
]);
118+
});
119+
95120
test('resolves inverse dependencies from available snapshot', () => {
96121
const paths = new Set([
97122
path.resolve(__dirname, '__fixtures__/file.js'),

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

Lines changed: 28 additions & 2 deletions
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 * as path from 'path';
89
import type {Config} from '@jest/types';
910
import type {FS as HasteFS} from 'jest-haste-map';
1011
import type {ResolveModuleConfig, ResolverType} from 'jest-resolve';
@@ -49,7 +50,9 @@ class DependencyResolver {
4950
if (this._resolver.isCoreModule(dependency)) {
5051
return acc;
5152
}
53+
5254
let resolvedDependency;
55+
let resolvedMockDependency;
5356
try {
5457
resolvedDependency = this._resolver.resolveModule(
5558
file,
@@ -64,8 +67,31 @@ class DependencyResolver {
6467
}
6568
}
6669

67-
if (resolvedDependency) {
68-
acc.push(resolvedDependency);
70+
if (!resolvedDependency) {
71+
return acc;
72+
}
73+
74+
acc.push(resolvedDependency);
75+
76+
// If we resolve a dependency, then look for a mock dependency
77+
// of the same name in that dependency's directory.
78+
resolvedMockDependency = this._resolver.getMockModule(
79+
resolvedDependency,
80+
path.basename(dependency),
81+
);
82+
83+
if (resolvedMockDependency) {
84+
const dependencyMockDir = path.resolve(
85+
path.dirname(resolvedDependency),
86+
'__mocks__',
87+
);
88+
89+
resolvedMockDependency = path.resolve(resolvedMockDependency);
90+
91+
// make sure mock is in the correct directory
92+
if (dependencyMockDir === path.dirname(resolvedMockDependency)) {
93+
acc.push(resolvedMockDependency);
94+
}
6995
}
7096

7197
return acc;

0 commit comments

Comments
 (0)