jest-mock
Advanced tools
Comparing version 29.4.2 to 29.4.3
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
@@ -221,3 +221,2 @@ * This source code is licensed under the MIT license found in the | ||
private _invocationCallCounter; | ||
private _originalFn; | ||
/** | ||
@@ -263,2 +262,3 @@ * @see README.md | ||
fn<T extends FunctionLike = UnknownFunction>(implementation?: T): Mock<T>; | ||
private _attachMockImplementation; | ||
spyOn< | ||
@@ -265,0 +265,0 @@ T extends object, |
@@ -20,3 +20,3 @@ 'use strict'; | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
@@ -212,3 +212,2 @@ * This source code is licensed under the MIT license found in the | ||
_invocationCallCounter; | ||
_originalFn; | ||
@@ -226,3 +225,2 @@ /** | ||
this._invocationCallCounter = 1; | ||
this._originalFn = new WeakMap(); | ||
} | ||
@@ -292,4 +290,3 @@ _getSlots(object) { | ||
mockName: 'jest.fn()', | ||
specificMockImpls: [], | ||
specificReturnValues: [] | ||
specificMockImpls: [] | ||
}; | ||
@@ -306,3 +303,3 @@ } | ||
} | ||
_makeComponent(metadata, restore) { | ||
_makeComponent(metadata, spyState) { | ||
if (metadata.type === 'object') { | ||
@@ -418,4 +415,4 @@ return new this._environmentGlobal.Object(); | ||
f.getMockImplementation = () => this._ensureMockConfig(f).mockImpl; | ||
if (typeof restore === 'function') { | ||
this._spyState.add(restore); | ||
if (spyState != null) { | ||
this._spyState.add(spyState); | ||
} | ||
@@ -436,13 +433,15 @@ this._mockState.set(f, this._defaultMockState()); | ||
f.mockClear(); | ||
const originalFn = this._originalFn.get(f); | ||
const originalMockImpl = { | ||
...this._defaultMockConfig(), | ||
mockImpl: originalFn | ||
}; | ||
this._mockConfigRegistry.set(f, originalMockImpl); | ||
this._mockConfigRegistry.delete(f); | ||
if (spyState != null) { | ||
spyState.reset?.(); | ||
} | ||
return f; | ||
}; | ||
f.mockRestore = () => { | ||
f.mockReset(); | ||
return restore ? restore() : undefined; | ||
f.mockClear(); | ||
this._mockConfigRegistry.delete(f); | ||
if (spyState != null) { | ||
spyState.restore(); | ||
this._spyState.delete(spyState); | ||
} | ||
}; | ||
@@ -483,3 +482,5 @@ f.mockReturnValueOnce = value => | ||
const previousImplementation = mockConfig.mockImpl; | ||
const previousSpecificImplementations = mockConfig.specificMockImpls; | ||
mockConfig.mockImpl = fn; | ||
mockConfig.specificMockImpls = []; | ||
const returnedValue = callback(); | ||
@@ -489,5 +490,7 @@ if ((0, _jestUtil().isPromise)(returnedValue)) { | ||
mockConfig.mockImpl = previousImplementation; | ||
mockConfig.specificMockImpls = previousSpecificImplementations; | ||
}); | ||
} else { | ||
mockConfig.mockImpl = previousImplementation; | ||
mockConfig.specificMockImpls = previousSpecificImplementations; | ||
} | ||
@@ -606,10 +609,10 @@ } | ||
_findReplacedProperty(object, propertyKey) { | ||
for (const spyState of this._spyState) { | ||
for (const {restore} of this._spyState) { | ||
if ( | ||
'object' in spyState && | ||
'property' in spyState && | ||
spyState.object === object && | ||
spyState.property === propertyKey | ||
'object' in restore && | ||
'property' in restore && | ||
restore.object === object && | ||
restore.property === propertyKey | ||
) { | ||
return spyState; | ||
return restore; | ||
} | ||
@@ -714,2 +717,7 @@ } | ||
} | ||
_attachMockImplementation(mock, original) { | ||
mock.mockImplementation(function () { | ||
return original.apply(this, arguments); | ||
}); | ||
} | ||
spyOn(object, methodKey, accessType) { | ||
@@ -766,5 +774,10 @@ if (typeof object !== 'object' && typeof object !== 'function') { | ||
}, | ||
() => { | ||
descriptor.get = originalGet; | ||
Object.defineProperty(object, methodKey, descriptor); | ||
{ | ||
reset: () => { | ||
this._attachMockImplementation(mock, original); | ||
}, | ||
restore: () => { | ||
descriptor.get = originalGet; | ||
Object.defineProperty(object, methodKey, descriptor); | ||
} | ||
} | ||
@@ -779,18 +792,20 @@ ); | ||
}, | ||
() => { | ||
if (isMethodOwner) { | ||
object[methodKey] = original; | ||
} else { | ||
delete object[methodKey]; | ||
{ | ||
reset: () => { | ||
this._attachMockImplementation(mock, original); | ||
}, | ||
restore: () => { | ||
if (isMethodOwner) { | ||
object[methodKey] = original; | ||
} else { | ||
delete object[methodKey]; | ||
} | ||
} | ||
} | ||
); | ||
// @ts-expect-error overriding original method with a Mock | ||
// @ts-expect-error: overriding original method with a mock | ||
object[methodKey] = mock; | ||
} | ||
mock.mockImplementation(function () { | ||
return original.apply(this, arguments); | ||
}); | ||
this._attachMockImplementation(mock, original); | ||
} | ||
this._originalFn.set(object[methodKey], original); | ||
return object[methodKey]; | ||
@@ -839,12 +854,14 @@ } | ||
}, | ||
() => { | ||
// @ts-expect-error: mock is assignable | ||
descriptor[accessType] = original; | ||
Object.defineProperty(object, propertyKey, descriptor); | ||
{ | ||
reset: () => { | ||
this._attachMockImplementation(descriptor[accessType], original); | ||
}, | ||
restore: () => { | ||
// @ts-expect-error: overriding original method with a mock | ||
descriptor[accessType] = original; | ||
Object.defineProperty(object, propertyKey, descriptor); | ||
} | ||
} | ||
); | ||
descriptor[accessType].mockImplementation(function () { | ||
// @ts-expect-error - wrong context | ||
return original.apply(this, arguments); | ||
}); | ||
this._attachMockImplementation(descriptor[accessType], original); | ||
} | ||
@@ -934,3 +951,5 @@ Object.defineProperty(object, propertyKey, descriptor); | ||
restore(); | ||
this._spyState.delete(restore); | ||
this._spyState.delete({ | ||
restore | ||
}); | ||
} | ||
@@ -941,3 +960,5 @@ }; | ||
restore.replaced = replaced; | ||
this._spyState.add(restore); | ||
this._spyState.add({ | ||
restore | ||
}); | ||
return replaced.replaceValue(value); | ||
@@ -949,8 +970,10 @@ } | ||
resetAllMocks() { | ||
this._spyState.forEach(reset => reset()); | ||
this.clearAllMocks(); | ||
this._mockConfigRegistry = new WeakMap(); | ||
this._mockState = new WeakMap(); | ||
this._spyState.forEach(spyState => spyState.reset?.()); | ||
} | ||
restoreAllMocks() { | ||
this._spyState.forEach(restore => restore()); | ||
this.clearAllMocks(); | ||
this._mockConfigRegistry = new WeakMap(); | ||
this._spyState.forEach(spyState => spyState.restore()); | ||
this._spyState = new Set(); | ||
@@ -957,0 +980,0 @@ } |
{ | ||
"name": "jest-mock", | ||
"version": "29.4.2", | ||
"version": "29.4.3", | ||
"repository": { | ||
@@ -20,5 +20,5 @@ "type": "git", | ||
"dependencies": { | ||
"@jest/types": "^29.4.2", | ||
"@jest/types": "^29.4.3", | ||
"@types/node": "*", | ||
"jest-util": "^29.4.2" | ||
"jest-util": "^29.4.3" | ||
}, | ||
@@ -35,3 +35,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "f0fc92e8443f09546c7ec0472bf9bce44fe5898f" | ||
"gitHead": "a49c88610e49a3242576160740a32a2fe11161e1" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47859
1347
Updated@jest/types@^29.4.3
Updatedjest-util@^29.4.3