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

fix(jest-resolve-dependencies): resolve mocks as dependencies #10713

Merged
merged 16 commits into from Oct 27, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
### Fixes

- `[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),
);
jeysal marked this conversation as resolved.
Show resolved Hide resolved

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)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a test that fails if this acc.push is unconditional?

Copy link
Contributor Author

@brapifra brapifra Oct 27, 2020

Choose a reason for hiding this comment

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

Yes! Both tests fail if this gets removed. Removing it would basically add an extra non-valid dependency to the list.

acc.push(resolvedMockDependency);
}
}

return acc;
Expand Down