-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Add jest.isolateModules
for scoped module initialization
#6701
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
93a11a1
First iteration of withResetModules
rogeliog 1f86b12
Make withResetModules "not async"
rogeliog 65e426e
Reimplement withResetModules without the stack
rogeliog 68d4e40
Merge branch 'master' into with-reset-modules
rogeliog 927773d
Rename withResetModules to isolateModules
rogeliog fffd74f
Add changelog entry
rogeliog 8c6910d
Fix eslint
rogeliog 9e4c58c
Add docs for isolateModules
rogeliog c6ca84a
Merge branch 'master' into with-reset-modules
rogeliog 59fb053
Fix typo, remove provideModules and extra async stuff
rogeliog 6eab501
Update Configuration.md
SimenB a829797
Merge branch 'master' into with-reset-modules
rogeliog 380e9c6
Rename implementation to isolated* and change docs to JestObjectAPI
rogeliog 8c9e2d0
Merge branch 'master' into with-reset-modules
thymikee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/jest-runtime/src/__tests__/test_root/ModuleWithState.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. 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. | ||
* | ||
*/ | ||
|
||
let state = 1; | ||
|
||
export const increment = () => { | ||
state += 1; | ||
}; | ||
|
||
export const getState = () => state; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,7 +97,9 @@ class Runtime { | |
_mockFactories: {[key: string]: () => any, __proto__: null}; | ||
_mockMetaDataCache: {[key: string]: MockFunctionMetadata, __proto__: null}; | ||
_mockRegistry: {[key: string]: any, __proto__: null}; | ||
_sandboxMockRegistry: ?{[key: string]: any, __proto__: null}; | ||
_moduleMocker: ModuleMocker; | ||
_sandboxModuleRegistry: ?ModuleRegistry; | ||
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 think 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 like it way better! |
||
_moduleRegistry: ModuleRegistry; | ||
_needsCoverageMapped: Set<string>; | ||
_resolver: Resolver; | ||
|
@@ -132,6 +134,7 @@ class Runtime { | |
this._mockFactories = Object.create(null); | ||
this._mockRegistry = Object.create(null); | ||
this._moduleMocker = this._environment.moduleMocker; | ||
this._sandboxModuleRegistry = null; | ||
this._moduleRegistry = Object.create(null); | ||
this._needsCoverageMapped = new Set(); | ||
this._resolver = resolver; | ||
|
@@ -289,11 +292,6 @@ class Runtime { | |
); | ||
let modulePath; | ||
|
||
const moduleRegistry = | ||
!options || !options.isInternalModule | ||
? this._moduleRegistry | ||
: this._internalModuleRegistry; | ||
|
||
// Some old tests rely on this mocking behavior. Ideally we'll change this | ||
// to be more explicit. | ||
const moduleResource = moduleName && this._resolver.getModule(moduleName); | ||
|
@@ -317,6 +315,18 @@ class Runtime { | |
modulePath = this._resolveModule(from, moduleName); | ||
} | ||
|
||
let moduleRegistry; | ||
|
||
if (!options || !options.isInternalModule) { | ||
if (this._moduleRegistry[modulePath] || !this._sandboxModuleRegistry) { | ||
moduleRegistry = this._moduleRegistry; | ||
} else { | ||
moduleRegistry = this._sandboxModuleRegistry; | ||
} | ||
} else { | ||
moduleRegistry = this._internalModuleRegistry; | ||
} | ||
|
||
if (!moduleRegistry[modulePath]) { | ||
// We must register the pre-allocated module object first so that any | ||
// circular dependencies that may arise while evaluating the module can | ||
|
@@ -358,12 +368,16 @@ class Runtime { | |
moduleName, | ||
); | ||
|
||
if (this._mockRegistry[moduleID]) { | ||
if (this._sandboxMockRegistry && this._sandboxMockRegistry[moduleID]) { | ||
return this._sandboxMockRegistry[moduleID]; | ||
} else if (this._mockRegistry[moduleID]) { | ||
return this._mockRegistry[moduleID]; | ||
} | ||
|
||
const mockRegistry = this._sandboxMockRegistry || this._mockRegistry; | ||
|
||
if (moduleID in this._mockFactories) { | ||
return (this._mockRegistry[moduleID] = this._mockFactories[moduleID]()); | ||
return (mockRegistry[moduleID] = this._mockFactories[moduleID]()); | ||
} | ||
|
||
let manualMock = this._resolver.getMockModule(from, moduleName); | ||
|
@@ -407,15 +421,15 @@ class Runtime { | |
|
||
// Only include the fromPath if a moduleName is given. Else treat as root. | ||
const fromPath = moduleName ? from : null; | ||
this._execModule(localModule, undefined, this._mockRegistry, fromPath); | ||
this._mockRegistry[moduleID] = localModule.exports; | ||
this._execModule(localModule, undefined, mockRegistry, fromPath); | ||
mockRegistry[moduleID] = localModule.exports; | ||
localModule.loaded = true; | ||
} else { | ||
// Look for a real module to generate an automock from | ||
this._mockRegistry[moduleID] = this._generateMock(from, moduleName); | ||
mockRegistry[moduleID] = this._generateMock(from, moduleName); | ||
} | ||
|
||
return this._mockRegistry[moduleID]; | ||
return mockRegistry[moduleID]; | ||
} | ||
|
||
requireModuleOrMock(from: Path, moduleName: string) { | ||
|
@@ -441,7 +455,22 @@ class Runtime { | |
} | ||
} | ||
|
||
isolateModules(fn: () => void) { | ||
if (this._sandboxModuleRegistry || this._sandboxMockRegistry) { | ||
throw new Error( | ||
'isolateModules cannot be nested inside another isolateModules.', | ||
); | ||
} | ||
this._sandboxModuleRegistry = Object.create(null); | ||
this._sandboxMockRegistry = Object.create(null); | ||
fn(); | ||
this._sandboxModuleRegistry = null; | ||
this._sandboxMockRegistry = null; | ||
} | ||
|
||
resetModules() { | ||
this._sandboxModuleRegistry = null; | ||
this._sandboxMockRegistry = null; | ||
this._mockRegistry = Object.create(null); | ||
this._moduleRegistry = Object.create(null); | ||
|
||
|
@@ -900,6 +929,10 @@ class Runtime { | |
this.resetModules(); | ||
return jestObject; | ||
}; | ||
const isolateModules = (fn: () => void) => { | ||
this.isolateModules(fn); | ||
return jestObject; | ||
}; | ||
const fn = this._moduleMocker.fn.bind(this._moduleMocker); | ||
const spyOn = this._moduleMocker.spyOn.bind(this._moduleMocker); | ||
|
||
|
@@ -936,6 +969,7 @@ class Runtime { | |
this._generateMock(from, moduleName), | ||
getTimerCount: () => this._environment.fakeTimers.getTimerCount(), | ||
isMockFunction: this._moduleMocker.isMockFunction, | ||
isolateModules, | ||
mock, | ||
requireActual: localRequire.requireActual, | ||
requireMock: localRequire.requireMock, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should move the example to https://jestjs.io/docs/en/jest-object (and fix the link to
jest.isolateModules()
above)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I think it should only live in jest-object. This was my bad.