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 #8952

Closed
wants to merge 8 commits into from
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -34,6 +34,7 @@
- `[jest-snapshot]` Distinguish empty string from external snapshot not written ([#8880](https://github.com/facebook/jest/pull/8880))
- `[jest-snapshot]` [**BREAKING**] Distinguish empty string from internal snapshot not written ([#8898](https://github.com/facebook/jest/pull/8898))
- `[jest-transform]` Properly cache transformed files across tests ([#8890](https://github.com/facebook/jest/pull/8890))
- `[jest-resolve-dependencies]` Resolve mocks as dependencies ([#8952](https://github.com/facebook/jest/pull/8952))
petevdp marked this conversation as resolved.
Show resolved Hide resolved

### 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 = 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,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.
*
*/

require('./file');
Expand Up @@ -58,6 +58,17 @@ test('resolves dependencies for existing path', () => {
]);
});

test('includes the mocks of dependencies as dependencies', () => {
jeysal marked this conversation as resolved.
Show resolved Hide resolved
const resolved = dependencyResolver.resolve(
path.resolve(__dirname, '__fixtures__', 'hasMocked', 'file.test.js'),
petevdp marked this conversation as resolved.
Show resolved Hide resolved
);

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

test('resolves dependencies for scoped packages', () => {
const resolved = dependencyResolver.resolve(
path.resolve(__dirname, '__fixtures__', 'scoped.js'),
Expand Down
16 changes: 15 additions & 1 deletion 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 {Config} from '@jest/types';
// eslint-disable-next-line import/no-extraneous-dependencies
import {FS as HasteFS} from 'jest-haste-map';
Expand Down Expand Up @@ -51,19 +52,32 @@ class DependencyResolver {
if (this._resolver.isCoreModule(dependency)) {
return acc;
}

let resolvedDependency;
let resolvedMockDependency;
try {
resolvedDependency = this._resolver.resolveModule(
file,
dependency,
options,
);
} catch (e) {
resolvedDependency = this._resolver.getMockModule(file, dependency);
resolvedMockDependency = this._resolver.getMockModule(file, dependency);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if we still need two code paths to resolve a mock. I'm unsure what this line is even for, maybe virtual mocks that do not have a real module counterpart?

Copy link
Contributor Author

@petevdp petevdp Sep 13, 2019

Choose a reason for hiding this comment

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

I'm not entirely sure but I assumed so, as the case where an error is thrown there expects the name to exist in (i think) the metadata but not be in the filesystem.

I left a note for prosterity in this commit: 8137d38

I could try to get to the bottom of this in this PR since it's pretty related, would that be a good idea?

If you do, is there a way I could figure out if this code is exercised in the test suite?

}

if (resolvedDependency) {
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(
path.dirname(resolvedDependency),
path.basename(dependency),
);
}

if (resolvedMockDependency) {
acc.push(resolvedMockDependency);
}

return acc;
Expand Down