Socket
Socket
Sign inDemoInstall

jest-mock-extended

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-mock-extended - npm Package Compare versions

Comparing version 3.0.1 to 3.0.2

4

lib/CalledWithFn.d.ts
import { CalledWithMock } from './Mock';
export declare const calledWithFn: <T, Y extends any[]>() => CalledWithMock<T, Y>;
export declare const calledWithFn: <T, Y extends any[]>({ fallbackMockImplementation, }?: {
fallbackMockImplementation?: ((...args: Y) => T) | undefined;
}) => CalledWithMock<T, Y>;
export default calledWithFn;

@@ -21,4 +21,4 @@ "use strict";

};
const calledWithFn = () => {
const fn = jest.fn();
const calledWithFn = ({ fallbackMockImplementation, } = {}) => {
const fn = jest.fn(fallbackMockImplementation);
let calledWithStack = [];

@@ -28,3 +28,3 @@ fn.calledWith = (...args) => {

// If that set of args is matched, we just call that jest.fn() for the result.
const calledWithFn = jest.fn();
const calledWithFn = jest.fn(fallbackMockImplementation);
if (!fn.getMockImplementation()) {

@@ -31,0 +31,0 @@ // Our original function gets a mock implementation which handles the matching

export { JestMockExtended, GlobalConfig, mockDeep, MockProxy, DeepMockProxy, CalledWithMock, mockClear, mockReset, mockFn, stub, } from './Mock';
export declare const mock: <T, MockedReturn extends { [K in keyof T]: T[K] extends (...args: infer A) => infer B ? import("./Mock").CalledWithMock<B, A> : T[K]; } & T = { [K in keyof T]: T[K] extends (...args: infer A) => infer B ? import("./Mock").CalledWithMock<B, A> : T[K]; } & T>(mockImplementation?: import("ts-essentials").DeepPartial<T>, opts?: import("./Mock").MockOpts | undefined) => MockedReturn;
export declare const calledWithFn: <T, Y extends any[]>() => import("./Mock").CalledWithMock<T, Y>;
export declare const calledWithFn: <T, Y extends any[]>({ fallbackMockImplementation, }?: {
fallbackMockImplementation?: ((...args: Y) => T) | undefined;
}) => import("./Mock").CalledWithMock<T, Y>;
export * from './Matchers';

@@ -27,2 +27,3 @@ /// <reference types="jest" />

deep?: boolean;
fallbackMockImplementation?: (...args: any[]) => any;
}

@@ -32,3 +33,4 @@ export declare const mockClear: (mock: MockProxy<any>) => any;

export declare function mockDeep<T>(opts: {
funcPropSupport: true;
funcPropSupport?: true;
fallbackMockImplementation?: MockOpts['fallbackMockImplementation'];
}, mockImplementation?: DeepPartial<T>): DeepMockProxyWithFuncPropSupport<T>;

@@ -35,0 +37,0 @@ export declare function mockDeep<T>(mockImplementation?: DeepPartial<T>): DeepMockProxy<T>;

@@ -61,3 +61,6 @@ "use strict";

function mockDeep(arg1, arg2) {
return mock(arg1 && 'funcPropSupport' in arg1 ? arg2 : arg1, { deep: true });
const [opts, mockImplementation] = typeof arg1 === 'object' && (typeof arg1.fallbackMockImplementation === 'function' || arg1.funcPropSupport === true)
? [arg1, arg2]
: [{}, arg1];
return mock(mockImplementation, { deep: true, fallbackMockImplementation: opts.fallbackMockImplementation });
}

@@ -88,3 +91,3 @@ exports.mockDeep = mockDeep;

var _a;
let fn = CalledWithFn_1.default();
let fn = CalledWithFn_1.default({ fallbackMockImplementation: opts === null || opts === void 0 ? void 0 : opts.fallbackMockImplementation });
// @ts-ignore

@@ -112,3 +115,3 @@ if (!(property in obj)) {

// @ts-ignore
obj[property] = CalledWithFn_1.default();
obj[property] = CalledWithFn_1.default({ fallbackMockImplementation: opts === null || opts === void 0 ? void 0 : opts.fallbackMockImplementation });
}

@@ -115,0 +118,0 @@ }

@@ -110,2 +110,10 @@ "use strict";

});
test('Can specify fallbackMockImplementation', () => {
const mockObj = Mock_1.default({}, {
fallbackMockImplementation: () => {
throw new Error('not mocked');
},
});
expect(() => mockObj.getSomethingWithArgs(1, 2)).toThrowError('not mocked');
});
test('Can specify multiple calledWith', () => {

@@ -259,2 +267,23 @@ const mockObj = Mock_1.default();

});
test('fallback mock implementation can be overridden', () => {
const mockObj = Mock_1.mockDeep({
fallbackMockImplementation: () => {
throw new Error('not mocked');
},
});
expect(() => mockObj.getNumber()).toThrowError('not mocked');
});
test('fallback mock implementation can be overridden while also providing a mock implementation', () => {
const mockObj = Mock_1.mockDeep({
fallbackMockImplementation: () => {
throw new Error('not mocked');
},
}, {
getNumber: () => {
return 150;
},
});
expect(mockObj.getNumber()).toBe(150);
expect(() => mockObj.deepProp.getNumber(1)).toThrowError('not mocked');
});
});

@@ -261,0 +290,0 @@ describe('Deep mock support for class variables which are functions but also have nested properties and functions', () => {

{
"name": "jest-mock-extended",
"version": "3.0.1",
"version": "3.0.2",
"homepage": "https://github.com/marchaos/jest-mock-extended",

@@ -5,0 +5,0 @@ "description": "Type safe mocking extensions for jest",

@@ -53,3 +53,15 @@ # jest-mock-extended

});
});
test('throwing an error if we forget to specify the return value')
const mock = mock<PartyProvider>(
{},
{
fallbackMockImplementation: () => {
throw new Error('not mocked');
},
}
);
expect(() => mock.getPartyType()).toThrowError('not mocked');
});
```

@@ -145,2 +157,15 @@

Can can provide a fallback mock implementation used if you do not define a return value using `calledWith`.
```ts
import { mockDeep } from 'jest-mock-extended';
const mockObj = mockDeep<Test1>({
fallbackMockImplementation: () => {
throw new Error('please add expected return value using calledWith');
},
});
expect(() => mockObj.getNumber()).toThrowError('not mocked');
```
## Available Matchers

@@ -147,0 +172,0 @@

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