Socket
Socket
Sign inDemoInstall

jest-mock

Package Overview
Dependencies
Maintainers
6
Versions
238
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-mock - npm Package Compare versions

Comparing version 29.5.0 to 29.6.0

32

build/index.d.ts

@@ -285,7 +285,7 @@ /**

private _spyOnProperty;
replaceProperty<
T extends object,
K extends PropertyLikeKeys<T>,
V extends T[K],
>(object: T, propertyKey: K, value: V): Replaced<T[K]>;
replaceProperty<T extends object, K extends keyof T>(
object: T,
propertyKey: K,
value: T[K],
): Replaced<T[K]>;
clearAllMocks(): void;

@@ -328,21 +328,7 @@ resetAllMocks(): void;

export declare const replaceProperty: <
T extends object,
K_2 extends Exclude<
keyof T,
| keyof {
[K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K];
}
| keyof {
[K_1 in keyof T as Required<T>[K_1] extends FunctionLike
? K_1
: never]: T[K_1];
}
>,
V extends T[K_2],
>(
export declare const replaceProperty: <T extends object, K extends keyof T>(
object: T,
propertyKey: K_2,
value: V,
) => Replaced<T[K_2]>;
propertyKey: K,
value: T[K],
) => Replaced<T[K]>;

@@ -349,0 +335,0 @@ declare type ResolveType<T extends FunctionLike> =

@@ -28,2 +28,16 @@ 'use strict';

// TODO remove re-export in Jest 30
// TODO remove re-export in Jest 30
// TODO in Jest 30 remove `SpyInstance` in favour of `Spied`
// eslint-disable-next-line @typescript-eslint/no-empty-interface
/**
* All what the internal typings need is to be sure that we have any-function.
* `FunctionLike` type ensures that and helps to constrain the type as well.
* The default of `UnknownFunction` makes sure that `any`s do not leak to the
* user side. For instance, calling `fn()` without implementation will return
* a mock of `(...args: Array<unknown>) => unknown` type. If implementation
* is provided, its typings are inferred correctly.
*/
const MOCK_CONSTRUCTOR_NAME = 'mockConstructor';

@@ -714,3 +728,6 @@ const FUNCTION_NAME_RESERVED_PATTERN = /[\s!-/:-@[-`{-~]/;

spyOn(object, methodKey, accessType) {
if (typeof object !== 'object' && typeof object !== 'function') {
if (
object == null ||
(typeof object !== 'object' && typeof object !== 'function')
) {
throw new Error(

@@ -720,8 +737,3 @@ `Cannot use spyOn on a primitive value; ${this._typeOf(object)} given`

}
if (!object) {
throw new Error(
`spyOn could not find an object to spy on for ${String(methodKey)}`
);
}
if (!methodKey) {
if (methodKey == null) {
throw new Error('No property name supplied');

@@ -733,8 +745,15 @@ }

const original = object[methodKey];
if (!original) {
throw new Error(
`Property \`${String(
methodKey
)}\` does not exist in the provided object`
);
}
if (!this.isMockFunction(original)) {
if (typeof original !== 'function') {
throw new Error(
`Cannot spy on the ${String(
`Cannot spy on the \`${String(
methodKey
)} property because it is not a function; ${this._typeOf(
)}\` property because it is not a function; ${this._typeOf(
original

@@ -812,12 +831,18 @@ )} given instead.${

if (!descriptor) {
throw new Error(`${String(propertyKey)} property does not exist`);
throw new Error(
`Property \`${String(
propertyKey
)}\` does not exist in the provided object`
);
}
if (!descriptor.configurable) {
throw new Error(`${String(propertyKey)} is not declared configurable`);
throw new Error(
`Property \`${String(propertyKey)}\` is not declared configurable`
);
}
if (!descriptor[accessType]) {
throw new Error(
`Property ${String(
`Property \`${String(
propertyKey
)} does not have access type ${accessType}`
)}\` does not have access type ${accessType}`
);

@@ -863,19 +888,15 @@ }

replaceProperty(object, propertyKey, value) {
if (object === undefined || object == null) {
if (
object == null ||
(typeof object !== 'object' && typeof object !== 'function')
) {
throw new Error(
`replaceProperty could not find an object on which to replace ${String(
propertyKey
)}`
`Cannot use replaceProperty on a primitive value; ${this._typeOf(
object
)} given`
);
}
if (propertyKey === undefined || propertyKey === null) {
if (propertyKey == null) {
throw new Error('No property name supplied');
}
if (typeof object !== 'object') {
throw new Error(
`Cannot mock property on a non-object value; ${this._typeOf(
object
)} given`
);
}
let descriptor = Object.getOwnPropertyDescriptor(object, propertyKey);

@@ -888,12 +909,18 @@ let proto = Object.getPrototypeOf(object);

if (!descriptor) {
throw new Error(`${String(propertyKey)} property does not exist`);
throw new Error(
`Property \`${String(
propertyKey
)}\` does not exist in the provided object`
);
}
if (!descriptor.configurable) {
throw new Error(`${String(propertyKey)} is not declared configurable`);
throw new Error(
`Property \`${String(propertyKey)}\` is not declared configurable`
);
}
if (descriptor.get !== undefined) {
throw new Error(
`Cannot mock the ${String(
`Cannot replace the \`${String(
propertyKey
)} property because it has a getter. Use \`jest.spyOn(object, '${String(
)}\` property because it has a getter. Use \`jest.spyOn(object, '${String(
propertyKey

@@ -905,5 +932,5 @@ )}', 'get').mockReturnValue(value)\` instead.`

throw new Error(
`Cannot mock the ${String(
`Cannot replace the \`${String(
propertyKey
)} property because it has a setter. Use \`jest.spyOn(object, '${String(
)}\` property because it has a setter. Use \`jest.spyOn(object, '${String(
propertyKey

@@ -915,5 +942,5 @@ )}', 'set').mockReturnValue(value)\` instead.`

throw new Error(
`Cannot mock the ${String(
`Cannot replace the \`${String(
propertyKey
)} property because it is a function. Use \`jest.spyOn(object, '${String(
)}\` property because it is a function. Use \`jest.spyOn(object, '${String(
propertyKey

@@ -920,0 +947,0 @@ )}')\` instead.`

{
"name": "jest-mock",
"version": "29.5.0",
"version": "29.6.0",
"repository": {

@@ -20,9 +20,9 @@ "type": "git",

"dependencies": {
"@jest/types": "^29.5.0",
"@jest/types": "^29.6.0",
"@types/node": "*",
"jest-util": "^29.5.0"
"jest-util": "^29.6.0"
},
"devDependencies": {
"@tsd/typescript": "^4.9.0",
"tsd-lite": "^0.6.0"
"@tsd/typescript": "^5.0.4",
"tsd-lite": "^0.7.0"
},

@@ -35,3 +35,3 @@ "engines": {

},
"gitHead": "39f3beda6b396665bebffab94e8d7c45be30454c"
"gitHead": "c1e5b8a38ef54bb138409f89831942ebf6a7a67e"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc