-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
fix(jest-resolve-dependencies): resolve mocks as dependencies #8952
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
Changes from all commits
6da2875
5cce42e
014d4de
4cebe1b
d7e327c
39cb1ad
651b27c
8137d38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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'); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'; | ||
|
|
@@ -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), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason why it's not
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand, sorry. I would expect that |
||
| path.basename(dependency), | ||
| ); | ||
|
|
||
| if (resolvedMockDependency) { | ||
| const dependencyMockDir = path.join( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably also |
||
| 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; | ||
|
|
@@ -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, | ||
| }); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.