Skip to content

Commit

Permalink
fix(jest-resolve-dependencies): resolve mocks as dependencies (#10713)
Browse files Browse the repository at this point in the history
Co-authored-by: grey275 <pjvanderpol@gmail.com>
  • Loading branch information
brapifra and petevdp committed Oct 27, 2020
1 parent c49e2e3 commit 7b1568d
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@

- `[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))
- `[expect]` Stop modifying the sample in `expect.objectContaining()` ([#10711](https://github.com/facebook/jest/pull/10711))
- `[jest-resolve-dependencies]` Resolve mocks as dependencies ([#10713](https://github.com/facebook/jest/pull/10713))

### Chore & Maintenance

Expand Down
@@ -0,0 +1,9 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*
*/

module.exports = str => str;
@@ -0,0 +1,9 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*
*/

module.exports = jest.fn();
@@ -0,0 +1,9 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*
*/

module.exports = str => str;
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*
*/

require('./file');
require('fake-node-module');
Expand Up @@ -61,6 +61,18 @@ test('resolves dependencies for existing path', () => {
]);
});

test('includes the mocks of dependencies as dependencies', () => {
const resolved = dependencyResolver.resolve(
path.resolve(__dirname, '__fixtures__/hasMocked/file.test.js'),
);

expect(resolved).toEqual([
expect.stringContaining(path.join('hasMocked', 'file.js')),
expect.stringContaining(path.join('hasMocked', '__mocks__', 'file.js')),
expect.stringContaining(path.join('__mocks__', 'fake-node-module.js')),
]);
});

test('resolves dependencies for scoped packages', () => {
const resolved = dependencyResolver.resolve(
path.resolve(__dirname, '__fixtures__', 'scoped.js'),
Expand Down Expand Up @@ -92,6 +104,19 @@ test('resolves inverse dependencies for existing path', () => {
]);
});

test('resolves inverse dependencies of mock', () => {
const paths = new Set([
path.resolve(__dirname, '__fixtures__/hasMocked/__mocks__/file.js'),
]);
const resolved = dependencyResolver.resolveInverse(paths, filter);

expect(resolved).toEqual([
expect.stringContaining(
path.join('__tests__/__fixtures__/hasMocked/file.test.js'),
),
]);
});

test('resolves inverse dependencies from available snapshot', () => {
const paths = new Set([
path.resolve(__dirname, '__fixtures__/file.js'),
Expand Down
30 changes: 28 additions & 2 deletions packages/jest-resolve-dependencies/src/index.ts
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import * as path from 'path';
import type {Config} from '@jest/types';
import type {FS as HasteFS} from 'jest-haste-map';
import type {ResolveModuleConfig, ResolverType} from 'jest-resolve';
Expand Down Expand Up @@ -49,7 +50,9 @@ class DependencyResolver {
if (this._resolver.isCoreModule(dependency)) {
return acc;
}

let resolvedDependency;
let resolvedMockDependency;
try {
resolvedDependency = this._resolver.resolveModule(
file,
Expand All @@ -64,8 +67,31 @@ class DependencyResolver {
}
}

if (resolvedDependency) {
acc.push(resolvedDependency);
if (!resolvedDependency) {
return acc;
}

acc.push(resolvedDependency);

// If we resolve a dependency, then look for a mock dependency
// of the same name in that dependency's directory.
resolvedMockDependency = this._resolver.getMockModule(
resolvedDependency,
path.basename(dependency),
);

if (resolvedMockDependency) {
const dependencyMockDir = path.resolve(
path.dirname(resolvedDependency),
'__mocks__',
);

resolvedMockDependency = path.resolve(resolvedMockDependency);

// make sure mock is in the correct directory
if (dependencyMockDir === path.dirname(resolvedMockDependency)) {
acc.push(resolvedMockDependency);
}
}

return acc;
Expand Down

0 comments on commit 7b1568d

Please sign in to comment.