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 @@ -30,6 +30,7 @@
- `[jest-leak-detector]` [**BREAKING**] Use `weak-napi` instead of `weak` package ([#8686](https://github.com/facebook/jest/pull/8686))
- `[jest-mock]` Fix for mockReturnValue overriding mockImplementationOnce ([#8398](https://github.com/facebook/jest/pull/8398))
- `[jest-reporters]` Make node-notifer an optional dependency ([#8918](https://github.com/facebook/jest/pull/8918))
- `[jest-resolve-dependencies]` Resolve mocks as dependencies ([#8952](https://github.com/facebook/jest/pull/8952))
- `[jest-snapshot]` Remove only the added newlines in multiline snapshots ([#8859](https://github.com/facebook/jest/pull/8859))
- `[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))
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'),
);

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 Expand Up @@ -89,6 +100,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
38 changes: 34 additions & 4 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 {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,47 @@ 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);
// TODO: This snippet might not be necessary, should be investigated later.
const mockDependency = this._resolver.getMockModule(file, dependency);
mockDependency && acc.push(mockDependency);
return acc;
}

if (!resolvedDependency) {
return acc;
}

if (resolvedDependency) {
acc.push(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),
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 reason why it's not file, dependency like above? Would be nice if we could just try to resolve, ignoring failure, and then look for a mock in a single call regardless of whether the resolution failed, to simplify the logic here

Copy link
Contributor Author

@petevdp petevdp Feb 21, 2020

Choose a reason for hiding this comment

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

getMockModule(file, dependency) actually does not work as expected.

Taking another look at it though, what does work with the tests that I added is this:

  resolvedMockDependency = this._resolver.getMockModule(
    dependency
    path.basename(dependency),
  );

Which is definitely better than what I had before.

Unfortunately, this will fail for the case where the dependency is in a different directory from the dependent, and there's still the existing issue of mocks of the same name from different directories.

If this isn't good enough, I'm thinking we'll have to introduce a new method into jest-resolve which mirrors the functionality resolveModule, or expand the functionality of resolveModule, or change what information is collected about mocks to handle these cases. This would also serve to avoid some of the hackyness of the rest of my solution.

I do feel as though I'm slightly out of my depth here since it seems like some fairly important changes need to happen to pick up those cases, so if anyone has any guidance that would be appreciated.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand, sorry. I would expect that getMockModule does no more than look for a __mocks__/file.js next to the file.js. Why does it matter where the dependent is, it's not being passed to getMockModule?

path.basename(dependency),
);

if (resolvedMockDependency) {
const dependencyMockDir = path.join(
Copy link
Contributor

Choose a reason for hiding this comment

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

Should probably also resolve this like the one it's being compared with, in case of symlinks or something

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 Expand Up @@ -127,8 +156,9 @@ class DependencyResolver {
}
const modules: Array<DependencyResolver.ResolvedModule> = [];
for (const file of this._hasteFS.getAbsoluteFileIterator()) {
const res = this.resolve(file, options);
modules.push({
dependencies: this.resolve(file, options),
dependencies: res,
file,
});
}
Expand Down