jest-runtime
Advanced tools
Comparing version 27.1.0 to 27.1.1
@@ -38,2 +38,3 @@ /** | ||
private readonly _explicitShouldMock; | ||
private readonly _explicitShouldMockModule; | ||
private _fakeTimersImplementation; | ||
@@ -47,2 +48,4 @@ private readonly _internalModuleRegistry; | ||
private _isolatedMockRegistry; | ||
private _moduleMockRegistry; | ||
private readonly _moduleMockFactories; | ||
private readonly _moduleMocker; | ||
@@ -68,2 +71,3 @@ private _isolatedModuleRegistry; | ||
private readonly _virtualMocks; | ||
private readonly _virtualModuleMocks; | ||
private _moduleImplementation?; | ||
@@ -90,4 +94,5 @@ private readonly jestObjectCaches; | ||
private loadCjsAsEsm; | ||
private importMock; | ||
private getExportsOfCjs; | ||
requireModule<T = unknown>(from: Config.Path, moduleName?: string, options?: InternalModuleOptions, isRequireActual?: boolean | null): T; | ||
requireModule<T = unknown>(from: Config.Path, moduleName?: string, options?: InternalModuleOptions, isRequireActual?: boolean): T; | ||
requireInternalModule<T = unknown>(from: Config.Path, to?: string): T; | ||
@@ -109,2 +114,3 @@ requireActual<T = unknown>(from: Config.Path, moduleName: string): T; | ||
}): void; | ||
private setModuleMock; | ||
restoreAllMocks(): void; | ||
@@ -111,0 +117,0 @@ resetAllMocks(): void; |
@@ -343,2 +343,4 @@ 'use strict'; | ||
_defineProperty(this, '_explicitShouldMockModule', void 0); | ||
_defineProperty(this, '_fakeTimersImplementation', void 0); | ||
@@ -360,2 +362,6 @@ | ||
_defineProperty(this, '_moduleMockRegistry', void 0); | ||
_defineProperty(this, '_moduleMockFactories', void 0); | ||
_defineProperty(this, '_moduleMocker', void 0); | ||
@@ -401,2 +407,4 @@ | ||
_defineProperty(this, '_virtualModuleMocks', void 0); | ||
_defineProperty(this, '_moduleImplementation', void 0); | ||
@@ -414,2 +422,3 @@ | ||
this._explicitShouldMock = new Map(); | ||
this._explicitShouldMockModule = new Map(); | ||
this._internalModuleRegistry = new Map(); | ||
@@ -420,2 +429,4 @@ this._isCurrentlyExecutingManualMock = null; | ||
this._mockRegistry = new Map(); | ||
this._moduleMockRegistry = new Map(); | ||
this._moduleMockFactories = new Map(); | ||
invariant( | ||
@@ -440,2 +451,3 @@ this._environment.moduleMocker, | ||
this._virtualMocks = new Map(); | ||
this._virtualModuleMocks = new Map(); | ||
this.jestObjectCaches = new Map(); | ||
@@ -682,2 +694,12 @@ this._mockMetaDataCache = new Map(); | ||
if ( | ||
this._shouldMock( | ||
referencingIdentifier, | ||
path, | ||
this._explicitShouldMockModule | ||
) | ||
) { | ||
return this.importMock(referencingIdentifier, path, context); | ||
} | ||
const resolved = this._resolveModule(referencingIdentifier, path); | ||
@@ -765,2 +787,39 @@ | ||
async importMock(from, moduleName, context) { | ||
const moduleID = this._resolver.getModuleID( | ||
this._virtualModuleMocks, | ||
from, | ||
moduleName | ||
); | ||
if (this._moduleMockRegistry.has(moduleID)) { | ||
return this._moduleMockRegistry.get(moduleID); | ||
} | ||
if (this._moduleMockFactories.has(moduleID)) { | ||
const invokedFactory = await this._moduleMockFactories.get( | ||
moduleID // has check above makes this ok | ||
)(); | ||
const module = new (_vm().SyntheticModule)( | ||
Object.keys(invokedFactory), | ||
function () { | ||
Object.entries(invokedFactory).forEach(([key, value]) => { | ||
// @ts-expect-error: TS doesn't know what `this` is | ||
this.setExport(key, value); | ||
}); | ||
}, | ||
{ | ||
context, | ||
identifier: moduleName | ||
} | ||
); | ||
this._moduleMockRegistry.set(moduleID, module); | ||
return evaluateSyntheticModule(module); | ||
} | ||
throw new Error('Attempting to import a mock without a factory'); | ||
} | ||
getExportsOfCjs(modulePath) { | ||
@@ -798,3 +857,3 @@ var _this$_fileTransforms, _this$_fileTransforms2; | ||
requireModule(from, moduleName, options, isRequireActual) { | ||
requireModule(from, moduleName, options, isRequireActual = false) { | ||
const moduleID = this._resolver.getModuleID( | ||
@@ -935,13 +994,8 @@ this._virtualMocks, | ||
if ( | ||
this._isolatedMockRegistry && | ||
this._isolatedMockRegistry.get(moduleID) | ||
) { | ||
return this._isolatedMockRegistry.get(moduleID); | ||
} else if (this._mockRegistry.get(moduleID)) { | ||
return this._mockRegistry.get(moduleID); | ||
const mockRegistry = this._isolatedMockRegistry || this._mockRegistry; | ||
if (mockRegistry.get(moduleID)) { | ||
return mockRegistry.get(moduleID); | ||
} | ||
const mockRegistry = this._isolatedMockRegistry || this._mockRegistry; | ||
if (this._mockFactories.has(moduleID)) { | ||
@@ -1062,3 +1116,3 @@ // has check above makes this ok | ||
try { | ||
if (this._shouldMock(from, moduleName)) { | ||
if (this._shouldMock(from, moduleName, this._explicitShouldMock)) { | ||
return this.requireMock(from, moduleName); | ||
@@ -1146,2 +1200,4 @@ } else { | ||
this._moduleMockRegistry.clear(); | ||
if (this._environment) { | ||
@@ -1242,2 +1298,20 @@ if (this._environment.global) { | ||
setModuleMock(from, moduleName, mockFactory, options) { | ||
if (options !== null && options !== void 0 && options.virtual) { | ||
const mockPath = this._resolver.getModulePath(from, moduleName); | ||
this._virtualModuleMocks.set(mockPath, true); | ||
} | ||
const moduleID = this._resolver.getModuleID( | ||
this._virtualModuleMocks, | ||
from, | ||
moduleName | ||
); | ||
this._explicitShouldMockModule.set(moduleID, true); | ||
this._moduleMockFactories.set(moduleID, mockFactory); | ||
} | ||
restoreAllMocks() { | ||
@@ -1266,2 +1340,4 @@ this._moduleMocker.restoreAllMocks(); | ||
this._moduleMockFactories.clear(); | ||
this._mockMetaDataCache.clear(); | ||
@@ -1275,2 +1351,4 @@ | ||
this._explicitShouldMockModule.clear(); | ||
this._transitiveShouldMock.clear(); | ||
@@ -1280,2 +1358,4 @@ | ||
this._virtualModuleMocks.clear(); | ||
this._cacheFS.clear(); | ||
@@ -1718,5 +1798,3 @@ | ||
_shouldMock(from, moduleName) { | ||
const explicitShouldMock = this._explicitShouldMock; | ||
_shouldMock(from, moduleName, explicitShouldMock) { | ||
const moduleID = this._resolver.getModuleID( | ||
@@ -1915,2 +1993,11 @@ this._virtualMocks, | ||
const mockModule = (moduleName, mockFactory, options) => { | ||
if (typeof mockFactory !== 'function') { | ||
throw new Error('`unstable_mockModule` must be passed a mock factory'); | ||
} | ||
this.setModuleMock(from, moduleName, mockFactory, options); | ||
return jestObject; | ||
}; | ||
const clearAllMocks = () => { | ||
@@ -2061,2 +2148,3 @@ this.clearAllMocks(); | ||
unmock, | ||
unstable_mockModule: mockModule, | ||
useFakeTimers, | ||
@@ -2063,0 +2151,0 @@ useRealTimers |
{ | ||
"name": "jest-runtime", | ||
"version": "27.1.0", | ||
"version": "27.1.1", | ||
"repository": { | ||
@@ -17,10 +17,10 @@ "type": "git", | ||
"dependencies": { | ||
"@jest/console": "^27.1.0", | ||
"@jest/environment": "^27.1.0", | ||
"@jest/fake-timers": "^27.1.0", | ||
"@jest/globals": "^27.1.0", | ||
"@jest/console": "^27.1.1", | ||
"@jest/environment": "^27.1.1", | ||
"@jest/fake-timers": "^27.1.1", | ||
"@jest/globals": "^27.1.1", | ||
"@jest/source-map": "^27.0.6", | ||
"@jest/test-result": "^27.1.0", | ||
"@jest/transform": "^27.1.0", | ||
"@jest/types": "^27.1.0", | ||
"@jest/test-result": "^27.1.1", | ||
"@jest/transform": "^27.1.1", | ||
"@jest/types": "^27.1.1", | ||
"@types/yargs": "^16.0.0", | ||
@@ -34,10 +34,10 @@ "chalk": "^4.0.0", | ||
"graceful-fs": "^4.2.4", | ||
"jest-haste-map": "^27.1.0", | ||
"jest-message-util": "^27.1.0", | ||
"jest-mock": "^27.1.0", | ||
"jest-haste-map": "^27.1.1", | ||
"jest-message-util": "^27.1.1", | ||
"jest-mock": "^27.1.1", | ||
"jest-regex-util": "^27.0.6", | ||
"jest-resolve": "^27.1.0", | ||
"jest-snapshot": "^27.1.0", | ||
"jest-util": "^27.1.0", | ||
"jest-validate": "^27.1.0", | ||
"jest-resolve": "^27.1.1", | ||
"jest-snapshot": "^27.1.1", | ||
"jest-util": "^27.1.1", | ||
"jest-validate": "^27.1.1", | ||
"slash": "^3.0.0", | ||
@@ -48,3 +48,3 @@ "strip-bom": "^4.0.0", | ||
"devDependencies": { | ||
"@jest/test-utils": "^27.1.0", | ||
"@jest/test-utils": "^27.1.1", | ||
"@types/exit": "^0.1.30", | ||
@@ -54,3 +54,3 @@ "@types/glob": "^7.1.1", | ||
"@types/node": "^14.0.27", | ||
"jest-environment-node": "^27.1.0", | ||
"jest-environment-node": "^27.1.1", | ||
"jest-snapshot-serializer-raw": "^1.1.0" | ||
@@ -64,3 +64,3 @@ }, | ||
}, | ||
"gitHead": "5ef792e957e83428d868a18618b8629e32719993" | ||
"gitHead": "111198b62dbfc3a730f7b1693e311608e834fe1d" | ||
} |
77428
2145
Updated@jest/console@^27.1.1
Updated@jest/environment@^27.1.1
Updated@jest/fake-timers@^27.1.1
Updated@jest/globals@^27.1.1
Updated@jest/test-result@^27.1.1
Updated@jest/transform@^27.1.1
Updated@jest/types@^27.1.1
Updatedjest-haste-map@^27.1.1
Updatedjest-message-util@^27.1.1
Updatedjest-mock@^27.1.1
Updatedjest-resolve@^27.1.1
Updatedjest-snapshot@^27.1.1
Updatedjest-util@^27.1.1
Updatedjest-validate@^27.1.1