jest-mock-extended
Advanced tools
Comparing version 3.0.7 to 4.0.0-beta1
import { CalledWithMock } from './Mock'; | ||
export declare const calledWithFn: <T, Y extends any[]>({ fallbackMockImplementation, }?: { | ||
fallbackMockImplementation?: (...args: Y) => T; | ||
}) => CalledWithMock<T, Y>; | ||
import type { FunctionLike } from 'jest-mock'; | ||
export declare const calledWithFn: <T extends FunctionLike>({ fallbackMockImplementation, }?: { | ||
fallbackMockImplementation?: T; | ||
}) => CalledWithMock<T>; | ||
export default calledWithFn; |
@@ -5,2 +5,3 @@ "use strict"; | ||
const Matchers_1 = require("./Matchers"); | ||
const globals_1 = require("@jest/globals"); | ||
function isJestAsymmetricMatcher(obj) { | ||
@@ -19,3 +20,2 @@ return !!obj && typeof obj === 'object' && 'asymmetricMatch' in obj && typeof obj.asymmetricMatch === 'function'; | ||
})); | ||
// @ts-ignore cannot return undefined, but this will fail the test if there is an expectation which is what we want | ||
return calledWithInstance | ||
@@ -26,3 +26,3 @@ ? calledWithInstance.calledWithFn(...actualArgs) | ||
const calledWithFn = ({ fallbackMockImplementation, } = {}) => { | ||
const fn = jest.fn(fallbackMockImplementation); | ||
const fn = globals_1.jest.fn(fallbackMockImplementation); | ||
let calledWithStack = []; | ||
@@ -32,6 +32,7 @@ fn.calledWith = (...args) => { | ||
// If that set of args is matched, we just call that jest.fn() for the result. | ||
const calledWithFn = jest.fn(fallbackMockImplementation); | ||
const calledWithFn = globals_1.jest.fn(fallbackMockImplementation); | ||
const mockImplementation = fn.getMockImplementation(); | ||
if (!mockImplementation || mockImplementation === fallbackMockImplementation) { | ||
// Our original function gets a mock implementation which handles the matching | ||
// @ts-expect-error '(...args: any) => ReturnType<T>' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'FunctionLike'. | ||
fn.mockImplementation((...args) => checkCalledWith(calledWithStack, args, fallbackMockImplementation)); | ||
@@ -38,0 +39,0 @@ calledWithStack = []; |
export { JestMockExtended, GlobalConfig, mockDeep, MockProxy, DeepMockProxy, CalledWithMock, mockClear, mockReset, mockFn, stub, } from './Mock'; | ||
export declare const mock: <T, MockedReturn extends import("./Mock")._MockProxy<T> & T = import("./Mock")._MockProxy<T> & T>(mockImplementation?: import("ts-essentials").DeepPartial<T>, opts?: import("./Mock").MockOpts | undefined) => MockedReturn; | ||
export declare const calledWithFn: <T, Y extends any[]>({ fallbackMockImplementation, }?: { | ||
fallbackMockImplementation?: ((...args: Y) => T) | undefined; | ||
}) => import("./Mock").CalledWithMock<T, Y>; | ||
export declare const calledWithFn: <T extends import("jest-mock").FunctionLike>({ fallbackMockImplementation, }?: { | ||
fallbackMockImplementation?: T | undefined; | ||
}) => import("./Mock").CalledWithMock<T>; | ||
export * from './Matchers'; |
export type MatcherFn<T> = (actualValue: T) => boolean; | ||
export declare class Matcher<T> { | ||
export interface MatcherLike<T> { | ||
asymmetricMatch(other: unknown): boolean; | ||
toString(): string; | ||
getExpectedType?(): string; | ||
toAsymmetricMatcher?(): string; | ||
} | ||
export declare class Matcher<T> implements MatcherLike<T> { | ||
readonly asymmetricMatch: MatcherFn<T>; | ||
@@ -26,3 +32,3 @@ private readonly description; | ||
export type MatchersOrLiterals<Y extends any[]> = { | ||
[K in keyof Y]: Matcher<Y[K]> | Y[K]; | ||
[K in keyof Y]: MatcherLike<Y[K]> | Y[K]; | ||
}; | ||
@@ -29,0 +35,0 @@ export declare const any: MatcherCreator<any>; |
@@ -27,3 +27,3 @@ "use strict"; | ||
this.asymmetricMatch = (actualValue) => { | ||
// @ts-ignore | ||
// @ts-expect-error | ||
this.value = actualValue; | ||
@@ -30,0 +30,0 @@ this.values.push(actualValue); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const globals_1 = require("@jest/globals"); | ||
const Matchers_1 = require("./Matchers"); | ||
class Cls { | ||
} | ||
describe('Matchers', () => { | ||
describe('any', () => { | ||
test('returns true for false', () => { | ||
expect((0, Matchers_1.any)().asymmetricMatch(false)).toBe(true); | ||
(0, globals_1.describe)('Matchers', () => { | ||
(0, globals_1.describe)('any', () => { | ||
(0, globals_1.test)('returns true for false', () => { | ||
(0, globals_1.expect)((0, Matchers_1.any)().asymmetricMatch(false)).toBe(true); | ||
}); | ||
test('returns true for undefined', () => { | ||
expect((0, Matchers_1.any)().asymmetricMatch(undefined)).toBe(true); | ||
(0, globals_1.test)('returns true for undefined', () => { | ||
(0, globals_1.expect)((0, Matchers_1.any)().asymmetricMatch(undefined)).toBe(true); | ||
}); | ||
test('returns true for null', () => { | ||
expect((0, Matchers_1.any)().asymmetricMatch(null)).toBe(true); | ||
(0, globals_1.test)('returns true for null', () => { | ||
(0, globals_1.expect)((0, Matchers_1.any)().asymmetricMatch(null)).toBe(true); | ||
}); | ||
test('Supports undefined in chain', () => { | ||
const f = jest.fn(); | ||
(0, globals_1.test)('Supports undefined in chain', () => { | ||
const f = globals_1.jest.fn(); | ||
f(undefined); | ||
expect(f).toHaveBeenCalledWith((0, Matchers_1.any)()); | ||
(0, globals_1.expect)(f).toHaveBeenCalledWith((0, Matchers_1.any)()); | ||
}); | ||
}); | ||
describe('anyString', () => { | ||
test('returns true for empty string', () => { | ||
expect((0, Matchers_1.anyString)().asymmetricMatch('')).toBe(true); | ||
(0, globals_1.describe)('anyString', () => { | ||
(0, globals_1.test)('returns true for empty string', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyString)().asymmetricMatch('')).toBe(true); | ||
}); | ||
test('returns true for non-empty string', () => { | ||
expect((0, Matchers_1.anyString)().asymmetricMatch('123')).toBe(true); | ||
(0, globals_1.test)('returns true for non-empty string', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyString)().asymmetricMatch('123')).toBe(true); | ||
}); | ||
test('returns false for number', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyString)().asymmetricMatch(123)).toBe(false); | ||
(0, globals_1.test)('returns false for number', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyString)().asymmetricMatch(123)).toBe(false); | ||
}); | ||
test('returns false for null', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyString)().asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false for null', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyString)().asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns false for undefined', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyString)().asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false for undefined', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyString)().asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('anyNumber', () => { | ||
test('returns true for 0', () => { | ||
expect((0, Matchers_1.anyNumber)().asymmetricMatch(0)).toBe(true); | ||
(0, globals_1.describe)('anyNumber', () => { | ||
(0, globals_1.test)('returns true for 0', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyNumber)().asymmetricMatch(0)).toBe(true); | ||
}); | ||
test('returns true for normal number', () => { | ||
expect((0, Matchers_1.anyNumber)().asymmetricMatch(123)).toBe(true); | ||
(0, globals_1.test)('returns true for normal number', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyNumber)().asymmetricMatch(123)).toBe(true); | ||
}); | ||
test('returns false for string', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyNumber)().asymmetricMatch('123')).toBe(false); | ||
(0, globals_1.test)('returns false for string', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyNumber)().asymmetricMatch('123')).toBe(false); | ||
}); | ||
test('returns false for null', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyNumber)().asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false for null', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyNumber)().asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns false for undefined', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyNumber)().asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false for undefined', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyNumber)().asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
test('returns false for NaN', () => { | ||
expect((0, Matchers_1.anyNumber)().asymmetricMatch(NaN)).toBe(false); | ||
(0, globals_1.test)('returns false for NaN', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyNumber)().asymmetricMatch(NaN)).toBe(false); | ||
}); | ||
}); | ||
describe('anyBoolean', () => { | ||
test('returns true for true', () => { | ||
expect((0, Matchers_1.anyBoolean)().asymmetricMatch(true)).toBe(true); | ||
(0, globals_1.describe)('anyBoolean', () => { | ||
(0, globals_1.test)('returns true for true', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyBoolean)().asymmetricMatch(true)).toBe(true); | ||
}); | ||
test('returns true for false', () => { | ||
expect((0, Matchers_1.anyBoolean)().asymmetricMatch(false)).toBe(true); | ||
(0, globals_1.test)('returns true for false', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyBoolean)().asymmetricMatch(false)).toBe(true); | ||
}); | ||
test('returns false for string', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyBoolean)().asymmetricMatch('true')).toBe(false); | ||
(0, globals_1.test)('returns false for string', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyBoolean)().asymmetricMatch('true')).toBe(false); | ||
}); | ||
test('returns false for null', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyBoolean)().asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false for null', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyBoolean)().asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns false for undefined', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyBoolean)().asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false for undefined', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyBoolean)().asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('anyFunction', () => { | ||
test('returns true for function', () => { | ||
expect((0, Matchers_1.anyFunction)().asymmetricMatch(() => { })).toBe(true); | ||
(0, globals_1.describe)('anyFunction', () => { | ||
(0, globals_1.test)('returns true for function', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyFunction)().asymmetricMatch(() => { })).toBe(true); | ||
}); | ||
test('returns false for string', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyFunction)().asymmetricMatch('true')).toBe(false); | ||
(0, globals_1.test)('returns false for string', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyFunction)().asymmetricMatch('true')).toBe(false); | ||
}); | ||
test('returns false for null', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyFunction)().asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false for null', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyFunction)().asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns false for undefined', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyFunction)().asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false for undefined', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyFunction)().asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('anySymbol', () => { | ||
test('returns true for symbol', () => { | ||
expect((0, Matchers_1.anySymbol)().asymmetricMatch(Symbol('123'))).toBe(true); | ||
(0, globals_1.describe)('anySymbol', () => { | ||
(0, globals_1.test)('returns true for symbol', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anySymbol)().asymmetricMatch(Symbol('123'))).toBe(true); | ||
}); | ||
test('returns false for string', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anySymbol)().asymmetricMatch('123')).toBe(false); | ||
(0, globals_1.test)('returns false for string', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anySymbol)().asymmetricMatch('123')).toBe(false); | ||
}); | ||
test('returns false for null', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anySymbol)().asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false for null', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anySymbol)().asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns false for undefined', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anySymbol)().asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false for undefined', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anySymbol)().asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('anyObject', () => { | ||
test('returns true for object', () => { | ||
expect((0, Matchers_1.anyObject)().asymmetricMatch({})).toBe(true); | ||
(0, globals_1.describe)('anyObject', () => { | ||
(0, globals_1.test)('returns true for object', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyObject)().asymmetricMatch({})).toBe(true); | ||
}); | ||
test('returns true for new object', () => { | ||
expect((0, Matchers_1.anyObject)().asymmetricMatch(new Object())).toBe(true); | ||
(0, globals_1.test)('returns true for new object', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyObject)().asymmetricMatch(new Object())).toBe(true); | ||
}); | ||
test('returns true for new instance', () => { | ||
expect((0, Matchers_1.anyObject)().asymmetricMatch(new Cls())).toBe(true); | ||
(0, globals_1.test)('returns true for new instance', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyObject)().asymmetricMatch(new Cls())).toBe(true); | ||
}); | ||
test('returns true for new builtin', () => { | ||
expect((0, Matchers_1.anyObject)().asymmetricMatch(new Map())).toBe(true); | ||
(0, globals_1.test)('returns true for new builtin', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyObject)().asymmetricMatch(new Map())).toBe(true); | ||
}); | ||
test('returns false for string', () => { | ||
expect((0, Matchers_1.anyObject)().asymmetricMatch('123')).toBe(false); | ||
(0, globals_1.test)('returns false for string', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyObject)().asymmetricMatch('123')).toBe(false); | ||
}); | ||
test('returns false for number', () => { | ||
expect((0, Matchers_1.anyObject)().asymmetricMatch(123)).toBe(false); | ||
(0, globals_1.test)('returns false for number', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyObject)().asymmetricMatch(123)).toBe(false); | ||
}); | ||
test('returns false for null', () => { | ||
expect((0, Matchers_1.anyObject)().asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false for null', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyObject)().asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns false for undefined', () => { | ||
expect((0, Matchers_1.anyObject)().asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false for undefined', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyObject)().asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('anyArray', () => { | ||
test('returns true for empty array', () => { | ||
expect((0, Matchers_1.anyArray)().asymmetricMatch([])).toBe(true); | ||
(0, globals_1.describe)('anyArray', () => { | ||
(0, globals_1.test)('returns true for empty array', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyArray)().asymmetricMatch([])).toBe(true); | ||
}); | ||
test('returns true for non empty', () => { | ||
expect((0, Matchers_1.anyArray)().asymmetricMatch([1, 2, 3])).toBe(true); | ||
(0, globals_1.test)('returns true for non empty', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyArray)().asymmetricMatch([1, 2, 3])).toBe(true); | ||
}); | ||
test('returns false for object', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyArray)().asymmetricMatch({})).toBe(false); | ||
(0, globals_1.test)('returns false for object', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyArray)().asymmetricMatch({})).toBe(false); | ||
}); | ||
test('returns false for null', () => { | ||
// @ts-ignored | ||
expect((0, Matchers_1.anyArray)().asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false for null', () => { | ||
// @ts-expect-errord | ||
(0, globals_1.expect)((0, Matchers_1.anyArray)().asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns false for undefined', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyArray)().asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false for undefined', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyArray)().asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('anyMap', () => { | ||
test('returns true for empty Map', () => { | ||
expect((0, Matchers_1.anyMap)().asymmetricMatch(new Map())).toBe(true); | ||
(0, globals_1.describe)('anyMap', () => { | ||
(0, globals_1.test)('returns true for empty Map', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anyMap)().asymmetricMatch(new Map())).toBe(true); | ||
}); | ||
test('returns true for non empty', () => { | ||
(0, globals_1.test)('returns true for non empty', () => { | ||
const map = new Map(); | ||
map.set(1, 2); | ||
expect((0, Matchers_1.anyMap)().asymmetricMatch(map)).toBe(true); | ||
(0, globals_1.expect)((0, Matchers_1.anyMap)().asymmetricMatch(map)).toBe(true); | ||
}); | ||
test('returns false for object', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyMap)().asymmetricMatch({})).toBe(false); | ||
(0, globals_1.test)('returns false for object', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyMap)().asymmetricMatch({})).toBe(false); | ||
}); | ||
test('returns false for null', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyMap)().asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false for null', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyMap)().asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns false for undefined', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anyMap)().asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false for undefined', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anyMap)().asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('anySet', () => { | ||
test('returns true for empty Set', () => { | ||
expect((0, Matchers_1.anySet)().asymmetricMatch(new Set())).toBe(true); | ||
(0, globals_1.describe)('anySet', () => { | ||
(0, globals_1.test)('returns true for empty Set', () => { | ||
(0, globals_1.expect)((0, Matchers_1.anySet)().asymmetricMatch(new Set())).toBe(true); | ||
}); | ||
test('returns true for non empty', () => { | ||
(0, globals_1.test)('returns true for non empty', () => { | ||
const set = new Set(); | ||
set.add(2); | ||
expect((0, Matchers_1.anySet)().asymmetricMatch(set)).toBe(true); | ||
(0, globals_1.expect)((0, Matchers_1.anySet)().asymmetricMatch(set)).toBe(true); | ||
}); | ||
test('returns false for object', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anySet)().asymmetricMatch({})).toBe(false); | ||
(0, globals_1.test)('returns false for object', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anySet)().asymmetricMatch({})).toBe(false); | ||
}); | ||
test('returns false for null', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anySet)().asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false for null', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anySet)().asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns false for undefined', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.anySet)().asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false for undefined', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.anySet)().asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('isA', () => { | ||
test('returns true when class is the same builtin', () => { | ||
expect((0, Matchers_1.isA)(Map).asymmetricMatch(new Map())).toBe(true); | ||
(0, globals_1.describe)('isA', () => { | ||
(0, globals_1.test)('returns true when class is the same builtin', () => { | ||
(0, globals_1.expect)((0, Matchers_1.isA)(Map).asymmetricMatch(new Map())).toBe(true); | ||
}); | ||
test('returns true for non empty', () => { | ||
expect((0, Matchers_1.isA)(Cls).asymmetricMatch(new Cls())).toBe(true); | ||
(0, globals_1.test)('returns true for non empty', () => { | ||
(0, globals_1.expect)((0, Matchers_1.isA)(Cls).asymmetricMatch(new Cls())).toBe(true); | ||
}); | ||
test('returns false for object', () => { | ||
expect((0, Matchers_1.isA)(Cls).asymmetricMatch({})).toBe(false); | ||
(0, globals_1.test)('returns false for object', () => { | ||
(0, globals_1.expect)((0, Matchers_1.isA)(Cls).asymmetricMatch({})).toBe(false); | ||
}); | ||
test('returns false for null', () => { | ||
expect((0, Matchers_1.isA)(Cls).asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false for null', () => { | ||
(0, globals_1.expect)((0, Matchers_1.isA)(Cls).asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns false for undefined', () => { | ||
expect((0, Matchers_1.isA)(Cls).asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false for undefined', () => { | ||
(0, globals_1.expect)((0, Matchers_1.isA)(Cls).asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('arrayIncludes', () => { | ||
test('returns true when array contains value', () => { | ||
expect((0, Matchers_1.arrayIncludes)('val').asymmetricMatch(['val', 'val2'])).toBe(true); | ||
(0, globals_1.describe)('arrayIncludes', () => { | ||
(0, globals_1.test)('returns true when array contains value', () => { | ||
(0, globals_1.expect)((0, Matchers_1.arrayIncludes)('val').asymmetricMatch(['val', 'val2'])).toBe(true); | ||
}); | ||
test('returns false when array does not contain value', () => { | ||
expect((0, Matchers_1.arrayIncludes)('val3').asymmetricMatch(['val', 'val2'])).toBe(false); | ||
(0, globals_1.test)('returns false when array does not contain value', () => { | ||
(0, globals_1.expect)((0, Matchers_1.arrayIncludes)('val3').asymmetricMatch(['val', 'val2'])).toBe(false); | ||
}); | ||
test('returns false when not a map', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.arrayIncludes)('val3').asymmetricMatch({})).toBe(false); | ||
(0, globals_1.test)('returns false when not a map', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.arrayIncludes)('val3').asymmetricMatch({})).toBe(false); | ||
}); | ||
test('returns false when for null', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.arrayIncludes)('val3').asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false when for null', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.arrayIncludes)('val3').asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns false when for undefined', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.arrayIncludes)('val3').asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false when for undefined', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.arrayIncludes)('val3').asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('mapHas', () => { | ||
test('returns true when map contains key', () => { | ||
expect((0, Matchers_1.mapHas)('key').asymmetricMatch(new Map([['key', 'val']]))).toBe(true); | ||
(0, globals_1.describe)('mapHas', () => { | ||
(0, globals_1.test)('returns true when map contains key', () => { | ||
(0, globals_1.expect)((0, Matchers_1.mapHas)('key').asymmetricMatch(new Map([['key', 'val']]))).toBe(true); | ||
}); | ||
test('returns false when map does not contain key', () => { | ||
expect((0, Matchers_1.mapHas)('key3').asymmetricMatch(new Map([['key', 'val']]))).toBe(false); | ||
(0, globals_1.test)('returns false when map does not contain key', () => { | ||
(0, globals_1.expect)((0, Matchers_1.mapHas)('key3').asymmetricMatch(new Map([['key', 'val']]))).toBe(false); | ||
}); | ||
test('returns false when not a map', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.mapHas)('val3').asymmetricMatch({})).toBe(false); | ||
(0, globals_1.test)('returns false when not a map', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.mapHas)('val3').asymmetricMatch({})).toBe(false); | ||
}); | ||
test('returns false when for null', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.mapHas)('val3').asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false when for null', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.mapHas)('val3').asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns false when for undefined', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.mapHas)('val3').asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false when for undefined', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.mapHas)('val3').asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('setHas', () => { | ||
test('returns true when set contains value', () => { | ||
expect((0, Matchers_1.setHas)('val').asymmetricMatch(new Set(['val']))).toBe(true); | ||
(0, globals_1.describe)('setHas', () => { | ||
(0, globals_1.test)('returns true when set contains value', () => { | ||
(0, globals_1.expect)((0, Matchers_1.setHas)('val').asymmetricMatch(new Set(['val']))).toBe(true); | ||
}); | ||
test('returns false when set does not contain value', () => { | ||
expect((0, Matchers_1.setHas)('val3').asymmetricMatch(new Set(['val', 'val2']))).toBe(false); | ||
(0, globals_1.test)('returns false when set does not contain value', () => { | ||
(0, globals_1.expect)((0, Matchers_1.setHas)('val3').asymmetricMatch(new Set(['val', 'val2']))).toBe(false); | ||
}); | ||
test('returns false when not a set', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.setHas)('val3').asymmetricMatch({})).toBe(false); | ||
(0, globals_1.test)('returns false when not a set', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.setHas)('val3').asymmetricMatch({})).toBe(false); | ||
}); | ||
test('returns false when for null', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.setHas)('val3').asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false when for null', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.setHas)('val3').asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns false when for undefined', () => { | ||
// @ts-ignore | ||
expect((0, Matchers_1.setHas)('val3').asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false when for undefined', () => { | ||
// @ts-expect-error | ||
(0, globals_1.expect)((0, Matchers_1.setHas)('val3').asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('objectContainsKey', () => { | ||
test('returns true when object contains key', () => { | ||
expect((0, Matchers_1.objectContainsKey)('key').asymmetricMatch({ key: 'val' })).toBe(true); | ||
(0, globals_1.describe)('objectContainsKey', () => { | ||
(0, globals_1.test)('returns true when object contains key', () => { | ||
(0, globals_1.expect)((0, Matchers_1.objectContainsKey)('key').asymmetricMatch({ key: 'val' })).toBe(true); | ||
}); | ||
test('returns false when object does not contain key', () => { | ||
expect((0, Matchers_1.objectContainsKey)('key3').asymmetricMatch({ key: 'val' })).toBe(false); | ||
(0, globals_1.test)('returns false when object does not contain key', () => { | ||
(0, globals_1.expect)((0, Matchers_1.objectContainsKey)('key3').asymmetricMatch({ key: 'val' })).toBe(false); | ||
}); | ||
test('returns false when not a object', () => { | ||
expect((0, Matchers_1.objectContainsKey)('val3').asymmetricMatch(213)).toBe(false); | ||
(0, globals_1.test)('returns false when not a object', () => { | ||
(0, globals_1.expect)((0, Matchers_1.objectContainsKey)('val3').asymmetricMatch(213)).toBe(false); | ||
}); | ||
test('returns false when for null', () => { | ||
expect((0, Matchers_1.objectContainsKey)('val3').asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false when for null', () => { | ||
(0, globals_1.expect)((0, Matchers_1.objectContainsKey)('val3').asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns false when for undefined', () => { | ||
expect((0, Matchers_1.objectContainsKey)('val3').asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false when for undefined', () => { | ||
(0, globals_1.expect)((0, Matchers_1.objectContainsKey)('val3').asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('objectContainsValue', () => { | ||
test('returns true when object contains value', () => { | ||
expect((0, Matchers_1.objectContainsValue)('val').asymmetricMatch({ key: 'val' })).toBe(true); | ||
(0, globals_1.describe)('objectContainsValue', () => { | ||
(0, globals_1.test)('returns true when object contains value', () => { | ||
(0, globals_1.expect)((0, Matchers_1.objectContainsValue)('val').asymmetricMatch({ key: 'val' })).toBe(true); | ||
}); | ||
test('returns false when object does not contain value', () => { | ||
expect((0, Matchers_1.objectContainsValue)('val3').asymmetricMatch({ key: 'val' })).toBe(false); | ||
(0, globals_1.test)('returns false when object does not contain value', () => { | ||
(0, globals_1.expect)((0, Matchers_1.objectContainsValue)('val3').asymmetricMatch({ key: 'val' })).toBe(false); | ||
}); | ||
test('returns false when not a object', () => { | ||
expect((0, Matchers_1.objectContainsValue)('val3').asymmetricMatch(213)).toBe(false); | ||
(0, globals_1.test)('returns false when not a object', () => { | ||
(0, globals_1.expect)((0, Matchers_1.objectContainsValue)('val3').asymmetricMatch(213)).toBe(false); | ||
}); | ||
test('returns false when for null', () => { | ||
expect((0, Matchers_1.objectContainsValue)('val3').asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false when for null', () => { | ||
(0, globals_1.expect)((0, Matchers_1.objectContainsValue)('val3').asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns false when for undefined', () => { | ||
expect((0, Matchers_1.objectContainsValue)('val3').asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false when for undefined', () => { | ||
(0, globals_1.expect)((0, Matchers_1.objectContainsValue)('val3').asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('notNull', () => { | ||
test('returns true when object', () => { | ||
expect((0, Matchers_1.notNull)().asymmetricMatch({ key: 'val' })).toBe(true); | ||
(0, globals_1.describe)('notNull', () => { | ||
(0, globals_1.test)('returns true when object', () => { | ||
(0, globals_1.expect)((0, Matchers_1.notNull)().asymmetricMatch({ key: 'val' })).toBe(true); | ||
}); | ||
test('returns true when undefined', () => { | ||
expect((0, Matchers_1.notNull)().asymmetricMatch(undefined)).toBe(true); | ||
(0, globals_1.test)('returns true when undefined', () => { | ||
(0, globals_1.expect)((0, Matchers_1.notNull)().asymmetricMatch(undefined)).toBe(true); | ||
}); | ||
test('returns true when empty string', () => { | ||
expect((0, Matchers_1.notNull)().asymmetricMatch('')).toBe(true); | ||
(0, globals_1.test)('returns true when empty string', () => { | ||
(0, globals_1.expect)((0, Matchers_1.notNull)().asymmetricMatch('')).toBe(true); | ||
}); | ||
test('returns false when for null', () => { | ||
expect((0, Matchers_1.notNull)().asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns false when for null', () => { | ||
(0, globals_1.expect)((0, Matchers_1.notNull)().asymmetricMatch(null)).toBe(false); | ||
}); | ||
}); | ||
describe('notUndefined', () => { | ||
test('returns true when object', () => { | ||
expect((0, Matchers_1.notUndefined)().asymmetricMatch({ key: 'val' })).toBe(true); | ||
(0, globals_1.describe)('notUndefined', () => { | ||
(0, globals_1.test)('returns true when object', () => { | ||
(0, globals_1.expect)((0, Matchers_1.notUndefined)().asymmetricMatch({ key: 'val' })).toBe(true); | ||
}); | ||
test('returns true when null', () => { | ||
expect((0, Matchers_1.notUndefined)().asymmetricMatch(null)).toBe(true); | ||
(0, globals_1.test)('returns true when null', () => { | ||
(0, globals_1.expect)((0, Matchers_1.notUndefined)().asymmetricMatch(null)).toBe(true); | ||
}); | ||
test('returns true when empty string', () => { | ||
expect((0, Matchers_1.notUndefined)().asymmetricMatch('')).toBe(true); | ||
(0, globals_1.test)('returns true when empty string', () => { | ||
(0, globals_1.expect)((0, Matchers_1.notUndefined)().asymmetricMatch('')).toBe(true); | ||
}); | ||
test('returns false when for undefined', () => { | ||
expect((0, Matchers_1.notUndefined)().asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false when for undefined', () => { | ||
(0, globals_1.expect)((0, Matchers_1.notUndefined)().asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('notEmpty', () => { | ||
test('returns true when object', () => { | ||
expect((0, Matchers_1.notEmpty)().asymmetricMatch({ key: 'val' })).toBe(true); | ||
(0, globals_1.describe)('notEmpty', () => { | ||
(0, globals_1.test)('returns true when object', () => { | ||
(0, globals_1.expect)((0, Matchers_1.notEmpty)().asymmetricMatch({ key: 'val' })).toBe(true); | ||
}); | ||
test('returns true when null', () => { | ||
expect((0, Matchers_1.notEmpty)().asymmetricMatch(null)).toBe(false); | ||
(0, globals_1.test)('returns true when null', () => { | ||
(0, globals_1.expect)((0, Matchers_1.notEmpty)().asymmetricMatch(null)).toBe(false); | ||
}); | ||
test('returns true when empty string', () => { | ||
expect((0, Matchers_1.notEmpty)().asymmetricMatch('')).toBe(false); | ||
(0, globals_1.test)('returns true when empty string', () => { | ||
(0, globals_1.expect)((0, Matchers_1.notEmpty)().asymmetricMatch('')).toBe(false); | ||
}); | ||
test('returns false when for undefined', () => { | ||
expect((0, Matchers_1.notEmpty)().asymmetricMatch(undefined)).toBe(false); | ||
(0, globals_1.test)('returns false when for undefined', () => { | ||
(0, globals_1.expect)((0, Matchers_1.notEmpty)().asymmetricMatch(undefined)).toBe(false); | ||
}); | ||
}); | ||
describe('captor', () => { | ||
(0, globals_1.describe)('captor', () => { | ||
let fn; | ||
let doSomething; | ||
beforeEach(() => { | ||
fn = jest.fn(); | ||
(0, globals_1.beforeEach)(() => { | ||
fn = globals_1.jest.fn(); | ||
doSomething = (fn, count) => { | ||
@@ -372,9 +373,9 @@ fn(String(count), count, { 1: 2 }); | ||
}); | ||
test('can capture arg with other matchers', () => { | ||
(0, globals_1.test)('can capture arg with other matchers', () => { | ||
doSomething(fn, 1); | ||
const argCaptor = (0, Matchers_1.captor)(); | ||
expect(fn).toHaveBeenCalledWith(argCaptor, (0, Matchers_1.any)(), (0, Matchers_1.any)()); | ||
expect(argCaptor.value).toBe('1'); | ||
(0, globals_1.expect)(fn).toHaveBeenCalledWith(argCaptor, (0, Matchers_1.any)(), (0, Matchers_1.any)()); | ||
(0, globals_1.expect)(argCaptor.value).toBe('1'); | ||
}); | ||
test('stores all values', () => { | ||
(0, globals_1.test)('stores all values', () => { | ||
doSomething(fn, 1); | ||
@@ -384,21 +385,21 @@ doSomething(fn, 2); | ||
const argCaptor = (0, Matchers_1.captor)(); | ||
expect(fn).toHaveBeenNthCalledWith(1, argCaptor, (0, Matchers_1.any)(), (0, Matchers_1.any)()); | ||
expect(fn).toHaveBeenNthCalledWith(2, argCaptor, (0, Matchers_1.any)(), (0, Matchers_1.any)()); | ||
expect(fn).toHaveBeenNthCalledWith(3, argCaptor, (0, Matchers_1.any)(), (0, Matchers_1.any)()); | ||
expect(argCaptor.value).toBe('3'); | ||
expect(argCaptor.values).toEqual(['1', '2', '3']); | ||
(0, globals_1.expect)(fn).toHaveBeenNthCalledWith(1, argCaptor, (0, Matchers_1.any)(), (0, Matchers_1.any)()); | ||
(0, globals_1.expect)(fn).toHaveBeenNthCalledWith(2, argCaptor, (0, Matchers_1.any)(), (0, Matchers_1.any)()); | ||
(0, globals_1.expect)(fn).toHaveBeenNthCalledWith(3, argCaptor, (0, Matchers_1.any)(), (0, Matchers_1.any)()); | ||
(0, globals_1.expect)(argCaptor.value).toBe('3'); | ||
(0, globals_1.expect)(argCaptor.values).toEqual(['1', '2', '3']); | ||
}); | ||
}); | ||
describe('matches function', () => { | ||
test('expects passes for when it returns true', () => { | ||
const fn = jest.fn(); | ||
(0, globals_1.describe)('matches function', () => { | ||
(0, globals_1.test)('expects passes for when it returns true', () => { | ||
const fn = globals_1.jest.fn(); | ||
fn(1); | ||
expect(fn).toHaveBeenCalledWith((0, Matchers_1.matches)((val) => val === 1)); | ||
(0, globals_1.expect)(fn).toHaveBeenCalledWith((0, Matchers_1.matches)((val) => val === 1)); | ||
}); | ||
test('expects with not passes for when it returns false', () => { | ||
const fn = jest.fn(); | ||
(0, globals_1.test)('expects with not passes for when it returns false', () => { | ||
const fn = globals_1.jest.fn(); | ||
fn(1); | ||
expect(fn).not.toHaveBeenCalledWith((0, Matchers_1.matches)((val) => val === 2)); | ||
(0, globals_1.expect)(fn).not.toHaveBeenCalledWith((0, Matchers_1.matches)((val) => val === 2)); | ||
}); | ||
}); | ||
}); |
@@ -1,4 +0,5 @@ | ||
/// <reference types="jest" /> | ||
import { MatchersOrLiterals } from './Matchers'; | ||
import { DeepPartial } from 'ts-essentials'; | ||
import { jest } from '@jest/globals'; | ||
import { FunctionLike } from 'jest-mock'; | ||
type ProxiedProperty = string | number | symbol; | ||
@@ -13,15 +14,15 @@ export interface GlobalConfig { | ||
}; | ||
export interface CalledWithMock<T, Y extends any[]> extends jest.Mock<T, Y> { | ||
calledWith: (...args: Y | MatchersOrLiterals<Y>) => jest.Mock<T, Y>; | ||
export interface CalledWithMock<T extends FunctionLike> extends jest.Mock<T> { | ||
calledWith: (...args: [...MatchersOrLiterals<Parameters<T>>]) => jest.Mock<T>; | ||
} | ||
export type _MockProxy<T> = { | ||
[K in keyof T]: T[K] extends (...args: infer A) => infer B ? T[K] & CalledWithMock<B, A> : T[K]; | ||
[K in keyof T]: T[K] extends FunctionLike ? T[K] & CalledWithMock<T[K]> : T[K]; | ||
}; | ||
export type MockProxy<T> = _MockProxy<T> & T; | ||
export type _DeepMockProxy<T> = { | ||
[K in keyof T]: T[K] extends (...args: infer A) => infer B ? T[K] & CalledWithMock<B, A> : T[K] & _DeepMockProxy<T[K]>; | ||
[K in keyof T]: T[K] extends FunctionLike ? T[K] & CalledWithMock<T[K]> : T[K] & _DeepMockProxy<T[K]>; | ||
}; | ||
export type DeepMockProxy<T> = _DeepMockProxy<T> & T; | ||
export type _DeepMockProxyWithFuncPropSupport<T> = { | ||
[K in keyof T]: T[K] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & DeepMockProxy<T[K]> : DeepMockProxy<T[K]>; | ||
[K in keyof T]: T[K] extends FunctionLike ? CalledWithMock<T[K]> & DeepMockProxy<T[K]> : DeepMockProxy<T[K]>; | ||
}; | ||
@@ -41,4 +42,4 @@ export type DeepMockProxyWithFuncPropSupport<T> = _DeepMockProxyWithFuncPropSupport<T> & T; | ||
declare const mock: <T, MockedReturn extends _MockProxy<T> & T = _MockProxy<T> & T>(mockImplementation?: DeepPartial<T>, opts?: MockOpts) => MockedReturn; | ||
export declare const mockFn: <T extends Function, A extends any[] = T extends (...args: infer AReal) => any ? AReal : any[], R = T extends (...args: any) => infer RReal ? RReal : any>() => CalledWithMock<R, A> & T; | ||
export declare const mockFn: <T extends FunctionLike>() => CalledWithMock<T> & T; | ||
export declare const stub: <T extends object>() => T; | ||
export default mock; |
@@ -8,2 +8,3 @@ "use strict"; | ||
const CalledWithFn_1 = __importDefault(require("./CalledWithFn")); | ||
const globals_1 = require("@jest/globals"); | ||
const DEFAULT_CONFIG = { | ||
@@ -85,3 +86,2 @@ ignoreProps: ['then'], | ||
set: (obj, property, value) => { | ||
// @ts-ignore All of these ignores are due to https://github.com/microsoft/TypeScript/issues/1863 | ||
obj[property] = value; | ||
@@ -93,3 +93,2 @@ return true; | ||
let fn = (0, CalledWithFn_1.default)({ fallbackMockImplementation: opts === null || opts === void 0 ? void 0 : opts.fallbackMockImplementation }); | ||
// @ts-ignore | ||
if (!(property in obj)) { | ||
@@ -101,3 +100,2 @@ if ((_a = GLOBAL_CONFIG.ignoreProps) === null || _a === void 0 ? void 0 : _a.includes(property)) { | ||
if (property === Symbol.iterator) { | ||
// @ts-ignore | ||
return obj[property]; | ||
@@ -110,18 +108,14 @@ } | ||
if ((opts === null || opts === void 0 ? void 0 : opts.deep) && property !== 'calls') { | ||
// @ts-ignore | ||
obj[property] = new Proxy(fn, handler(opts)); | ||
// @ts-ignore | ||
obj[property]._isMockObject = true; | ||
} | ||
else { | ||
// @ts-ignore | ||
obj[property] = (0, CalledWithFn_1.default)({ fallbackMockImplementation: opts === null || opts === void 0 ? void 0 : opts.fallbackMockImplementation }); | ||
} | ||
} | ||
// @ts-ignore | ||
// @ts-expect-error | ||
if (obj instanceof Date && typeof obj[property] === 'function') { | ||
// @ts-ignore | ||
// @ts-expect-error | ||
return obj[property].bind(obj); | ||
} | ||
// @ts-ignore | ||
return obj[property]; | ||
@@ -131,3 +125,3 @@ }, | ||
const mock = (mockImplementation = {}, opts) => { | ||
// @ts-ignore private | ||
// @ts-expect-error private | ||
mockImplementation._isMockObject = true; | ||
@@ -137,3 +131,3 @@ return overrideMockImp(mockImplementation, opts); | ||
const mockFn = () => { | ||
// @ts-ignore | ||
// @ts-expect-error | ||
return (0, CalledWithFn_1.default)(); | ||
@@ -146,6 +140,6 @@ }; | ||
if (property in obj) { | ||
// @ts-ignore | ||
// @ts-expect-error | ||
return obj[property]; | ||
} | ||
return jest.fn(); | ||
return globals_1.jest.fn(); | ||
}, | ||
@@ -152,0 +146,0 @@ }); |
@@ -33,2 +33,4 @@ "use strict"; | ||
const CalledWithFn_1 = __importDefault(require("./CalledWithFn")); | ||
const globals_1 = require("@jest/globals"); | ||
const assert_1 = require("assert"); | ||
class Test1 { | ||
@@ -82,4 +84,4 @@ constructor(id) { | ||
exports.Test6 = Test6; | ||
describe('jest-mock-extended', () => { | ||
test('Can be assigned back to itself even when there are private parts', () => { | ||
(0, globals_1.describe)('jest-mock-extended', () => { | ||
(0, globals_1.test)('Can be assigned back to itself even when there are private parts', () => { | ||
// No TS errors here | ||
@@ -89,30 +91,37 @@ const mockObj = (0, Mock_1.default)(); | ||
new Test1(1).ofAnother(mockObj); | ||
expect(mockObj.getNumber).toHaveBeenCalledTimes(1); | ||
(0, globals_1.expect)(mockObj.getNumber).toHaveBeenCalledTimes(1); | ||
}); | ||
test('Check that a jest.fn() is created without any invocation to the mock method', () => { | ||
(0, globals_1.test)('Check that a jest.fn() is created without any invocation to the mock method', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
expect(mockObj.getNumber).toHaveBeenCalledTimes(0); | ||
(0, globals_1.expect)(mockObj.getNumber).toHaveBeenCalledTimes(0); | ||
}); | ||
test('Check that invocations are registered', () => { | ||
(0, globals_1.test)('Check that invocations are registered', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getNumber(); | ||
mockObj.getNumber(); | ||
expect(mockObj.getNumber).toHaveBeenCalledTimes(2); | ||
(0, globals_1.expect)(mockObj.getNumber).toHaveBeenCalledTimes(2); | ||
}); | ||
test('Can mock a return value', () => { | ||
(0, globals_1.test)('Can mock a return value', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getNumber.mockReturnValue(12); | ||
expect(mockObj.getNumber()).toBe(12); | ||
(0, globals_1.expect)(mockObj.getNumber()).toBe(12); | ||
}); | ||
test('Can specify args', () => { | ||
(0, globals_1.test)('Can specify args', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithArgs(1, 2); | ||
expect(mockObj.getSomethingWithArgs).toBeCalledWith(1, 2); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs).toBeCalledWith(1, 2); | ||
}); | ||
test('Can specify calledWith', () => { | ||
(0, globals_1.test)('Can mock implementation of method', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithArgs.mockImplementation((arg1, arg2) => { | ||
return arg1 + arg2; | ||
}); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
}); | ||
(0, globals_1.test)('Can specify calledWith', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithArgs.calledWith(1, 2).mockReturnValue(1); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(1); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(1); | ||
}); | ||
test('Can specify fallbackMockImplementation', () => { | ||
(0, globals_1.test)('Can specify fallbackMockImplementation', () => { | ||
const mockObj = (0, Mock_1.default)({}, { | ||
@@ -123,17 +132,17 @@ fallbackMockImplementation: () => { | ||
}); | ||
expect(() => mockObj.getSomethingWithArgs(1, 2)).toThrowError('not mocked'); | ||
(0, globals_1.expect)(() => mockObj.getSomethingWithArgs(1, 2)).toThrowError('not mocked'); | ||
}); | ||
test('Can specify multiple calledWith', () => { | ||
(0, globals_1.test)('Can specify multiple calledWith', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithArgs.calledWith(1, 2).mockReturnValue(3); | ||
mockObj.getSomethingWithArgs.calledWith(6, 7).mockReturnValue(13); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
expect(mockObj.getSomethingWithArgs(6, 7)).toBe(13); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(6, 7)).toBe(13); | ||
}); | ||
test('Can set props', () => { | ||
(0, globals_1.test)('Can set props', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.id = 17; | ||
expect(mockObj.id).toBe(17); | ||
(0, globals_1.expect)(mockObj.id).toBe(17); | ||
}); | ||
test('Can set false and null boolean props', () => { | ||
(0, globals_1.test)('Can set false and null boolean props', () => { | ||
const mockObj = (0, Mock_1.default)({ | ||
@@ -145,21 +154,21 @@ someValue: false, | ||
}); | ||
expect(mockObj.someValue).toBe(false); | ||
expect(mockObj2.someValue).toBe(null); | ||
(0, globals_1.expect)(mockObj.someValue).toBe(false); | ||
(0, globals_1.expect)(mockObj2.someValue).toBe(null); | ||
}); | ||
test('can set undefined explicitly', () => { | ||
(0, globals_1.test)('can set undefined explicitly', () => { | ||
const mockObj = (0, Mock_1.default)({ | ||
someValue: undefined, // this is intentionally set to undefined | ||
}); | ||
expect(mockObj.someValue).toBe(undefined); | ||
(0, globals_1.expect)(mockObj.someValue).toBe(undefined); | ||
}); | ||
test('Equals self', () => { | ||
(0, globals_1.test)('Equals self', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
expect(mockObj).toBe(mockObj); | ||
expect(mockObj).toEqual(mockObj); | ||
const spy = jest.fn(); | ||
(0, globals_1.expect)(mockObj).toBe(mockObj); | ||
(0, globals_1.expect)(mockObj).toEqual(mockObj); | ||
const spy = globals_1.jest.fn(); | ||
spy(mockObj); | ||
expect(spy).toHaveBeenCalledWith(mockObj); | ||
(0, globals_1.expect)(spy).toHaveBeenCalledWith(mockObj); | ||
}); | ||
describe('Mimic Type', () => { | ||
test('can use MockProxy in place of Mock Type', () => { | ||
(0, globals_1.describe)('Mimic Type', () => { | ||
(0, globals_1.test)('can use MockProxy in place of Mock Type', () => { | ||
const t1 = (0, Mock_1.default)(); | ||
@@ -171,30 +180,30 @@ const i1 = (0, Mock_1.default)(); | ||
}); | ||
describe('calledWith', () => { | ||
test('can use calledWith without mock', () => { | ||
(0, globals_1.describe)('calledWith', () => { | ||
(0, globals_1.test)('can use calledWith without mock', () => { | ||
const mockFunc = (0, CalledWithFn_1.default)(); | ||
mockFunc.calledWith((0, Matchers_1.anyNumber)(), (0, Matchers_1.anyNumber)()).mockReturnValue(3); | ||
expect(mockFunc(1, 2)).toBe(3); | ||
(0, globals_1.expect)(mockFunc(1, 2)).toBe(3); | ||
}); | ||
test('Can specify matchers', () => { | ||
(0, globals_1.test)('Can specify matchers', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithArgs.calledWith((0, Matchers_1.anyNumber)(), (0, Matchers_1.anyNumber)()).mockReturnValue(3); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
}); | ||
test('does not match when one arg does not match Matcher', () => { | ||
(0, globals_1.test)('does not match when one arg does not match Matcher', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithArgs.calledWith((0, Matchers_1.anyNumber)(), (0, Matchers_1.anyNumber)()).mockReturnValue(3); | ||
// @ts-ignore | ||
expect(mockObj.getSomethingWithArgs('1', 2)).toBe(undefined); | ||
// @ts-expect-error | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs('1', 2)).toBe(undefined); | ||
}); | ||
test('can use literals', () => { | ||
(0, globals_1.test)('can use literals', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithArgs.calledWith(1, 2).mockReturnValue(3); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
}); | ||
test('can mix Matchers with literals', () => { | ||
(0, globals_1.test)('can mix Matchers with literals', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithArgs.calledWith(1, (0, Matchers_1.anyNumber)()).mockReturnValue(3); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
}); | ||
test('supports multiple calledWith', () => { | ||
(0, globals_1.test)('supports multiple calledWith', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
@@ -204,75 +213,79 @@ mockObj.getSomethingWithArgs.calledWith(2, (0, Matchers_1.anyNumber)()).mockReturnValue(4); | ||
mockObj.getSomethingWithArgs.calledWith(6, (0, Matchers_1.anyNumber)()).mockReturnValue(7); | ||
expect(mockObj.getSomethingWithArgs(2, 2)).toBe(4); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
expect(mockObj.getSomethingWithArgs(6, 2)).toBe(7); | ||
expect(mockObj.getSomethingWithArgs(7, 2)).toBe(undefined); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(2, 2)).toBe(4); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(6, 2)).toBe(7); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(7, 2)).toBe(undefined); | ||
}); | ||
test('supports overriding with same args', () => { | ||
(0, globals_1.test)('supports overriding with same args', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithArgs.calledWith(1, 2).mockReturnValue(4); | ||
mockObj.getSomethingWithArgs.calledWith(1, 2).mockReturnValue(3); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
}); | ||
test('Support jest matcher', () => { | ||
(0, globals_1.test)('Support jest matcher', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithArgs.calledWith(expect.anything(), expect.anything()).mockReturnValue(3); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
mockObj.getSomethingWithArgs | ||
.calledWith(globals_1.expect.anything(), globals_1.expect.anything()) | ||
.mockReturnValue(3); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
}); | ||
test('Suport mix Matchers with literals and with jest matcher', () => { | ||
(0, globals_1.test)('Suport mix Matchers with literals and with jest matcher', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithMoreArgs.calledWith((0, Matchers_1.anyNumber)(), expect.anything(), 3).mockReturnValue(4); | ||
expect(mockObj.getSomethingWithMoreArgs(1, 2, 3)).toBe(4); | ||
expect(mockObj.getSomethingWithMoreArgs(1, 2, 4)).toBeUndefined; | ||
mockObj.getSomethingWithMoreArgs | ||
.calledWith((0, Matchers_1.anyNumber)(), globals_1.expect.anything(), 3) | ||
.mockReturnValue(4); | ||
(0, globals_1.expect)(mockObj.getSomethingWithMoreArgs(1, 2, 3)).toBe(4); | ||
(0, globals_1.expect)(mockObj.getSomethingWithMoreArgs(1, 2, 4)).toBeUndefined; | ||
}); | ||
test('Can use calledWith with an other mock', () => { | ||
(0, globals_1.test)('Can use calledWith with an other mock', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
const mockArg = (0, Mock_1.default)(); | ||
mockObj.getNumberWithMockArg.calledWith(mockArg).mockReturnValue(4); | ||
expect(mockObj.getNumberWithMockArg(mockArg)).toBe(4); | ||
(0, globals_1.expect)(mockObj.getNumberWithMockArg(mockArg)).toBe(4); | ||
}); | ||
}); | ||
describe('Matchers with toHaveBeenCalledWith', () => { | ||
test('matchers allow all args to be Matcher based', () => { | ||
(0, globals_1.describe)('Matchers with toHaveBeenCalledWith', () => { | ||
(0, globals_1.test)('matchers allow all args to be Matcher based', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithArgs(2, 4); | ||
expect(mockObj.getSomethingWithArgs).toHaveBeenCalledWith((0, Matchers_1.anyNumber)(), (0, Matchers_1.anyNumber)()); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs).toHaveBeenCalledWith((0, Matchers_1.anyNumber)(), (0, Matchers_1.anyNumber)()); | ||
}); | ||
test('matchers allow for a mix of Matcher and literal', () => { | ||
(0, globals_1.test)('matchers allow for a mix of Matcher and literal', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithArgs(2, 4); | ||
expect(mockObj.getSomethingWithArgs).toHaveBeenCalledWith((0, Matchers_1.anyNumber)(), 4); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs).toHaveBeenCalledWith((0, Matchers_1.anyNumber)(), 4); | ||
}); | ||
test('matchers allow for not.toHaveBeenCalledWith', () => { | ||
(0, globals_1.test)('matchers allow for not.toHaveBeenCalledWith', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithArgs(2, 4); | ||
expect(mockObj.getSomethingWithArgs).not.toHaveBeenCalledWith((0, Matchers_1.anyNumber)(), 5); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs).not.toHaveBeenCalledWith((0, Matchers_1.anyNumber)(), 5); | ||
}); | ||
}); | ||
describe('Deep mock support', () => { | ||
test('can deep mock members', () => { | ||
(0, globals_1.describe)('Deep mock support', () => { | ||
(0, globals_1.test)('can deep mock members', () => { | ||
const mockObj = (0, Mock_1.mockDeep)(); | ||
mockObj.deepProp.getNumber.calledWith(1).mockReturnValue(4); | ||
expect(mockObj.deepProp.getNumber(1)).toBe(4); | ||
(0, globals_1.expect)(mockObj.deepProp.getNumber(1)).toBe(4); | ||
}); | ||
test('three level deep mock', () => { | ||
(0, globals_1.test)('three level deep mock', () => { | ||
const mockObj = (0, Mock_1.mockDeep)(); | ||
mockObj.deepProp.deeperProp.getNumber.calledWith(1).mockReturnValue(4); | ||
expect(mockObj.deepProp.deeperProp.getNumber(1)).toBe(4); | ||
(0, globals_1.expect)(mockObj.deepProp.deeperProp.getNumber(1)).toBe(4); | ||
}); | ||
test('maintains API for deep mocks', () => { | ||
(0, globals_1.test)('maintains API for deep mocks', () => { | ||
const mockObj = (0, Mock_1.mockDeep)(); | ||
mockObj.deepProp.getNumber(100); | ||
expect(mockObj.deepProp.getNumber.mock.calls[0][0]).toBe(100); | ||
(0, globals_1.expect)(mockObj.deepProp.getNumber.mock.calls[0][0]).toBe(100); | ||
}); | ||
test('non deep expectation work as expected', () => { | ||
(0, globals_1.test)('non deep expectation work as expected', () => { | ||
const mockObj = (0, Mock_1.mockDeep)(); | ||
new Test1(1).ofAnother(mockObj); | ||
expect(mockObj.getNumber).toHaveBeenCalledTimes(1); | ||
(0, globals_1.expect)(mockObj.getNumber).toHaveBeenCalledTimes(1); | ||
}); | ||
test('deep expectation work as expected', () => { | ||
(0, globals_1.test)('deep expectation work as expected', () => { | ||
const mockObj = (0, Mock_1.mockDeep)(); | ||
mockObj.deepProp.getNumber(2); | ||
expect(mockObj.deepProp.getNumber).toHaveBeenCalledTimes(1); | ||
(0, globals_1.expect)(mockObj.deepProp.getNumber).toHaveBeenCalledTimes(1); | ||
}); | ||
test('fallback mock implementation can be overridden', () => { | ||
(0, globals_1.test)('fallback mock implementation can be overridden', () => { | ||
const mockObj = (0, Mock_1.mockDeep)({ | ||
@@ -284,6 +297,6 @@ fallbackMockImplementation: () => { | ||
mockObj.deepProp.getAnotherString.calledWith('foo'); // no mock implementation | ||
expect(() => mockObj.getNumber()).toThrowError('not mocked'); | ||
expect(() => mockObj.deepProp.getAnotherString('foo')).toThrowError('not mocked'); | ||
(0, globals_1.expect)(() => mockObj.getNumber()).toThrowError('not mocked'); | ||
(0, globals_1.expect)(() => mockObj.deepProp.getAnotherString('foo')).toThrowError('not mocked'); | ||
}); | ||
test('fallback mock implementation can be overridden while also providing a mock implementation', () => { | ||
(0, globals_1.test)('fallback mock implementation can be overridden while also providing a mock implementation', () => { | ||
const mockObj = (0, Mock_1.mockDeep)({ | ||
@@ -299,49 +312,49 @@ fallbackMockImplementation: () => { | ||
mockObj.deepProp.getAnotherString.calledWith('?').mockReturnValue('mocked'); | ||
expect(mockObj.getNumber()).toBe(150); | ||
expect(mockObj.deepProp.getAnotherString('?')).toBe('mocked'); | ||
expect(() => mockObj.deepProp.getNumber(1)).toThrowError('not mocked'); | ||
expect(() => mockObj.deepProp.getAnotherString('!')).toThrowError('not mocked'); | ||
(0, globals_1.expect)(mockObj.getNumber()).toBe(150); | ||
(0, globals_1.expect)(mockObj.deepProp.getAnotherString('?')).toBe('mocked'); | ||
(0, globals_1.expect)(() => mockObj.deepProp.getNumber(1)).toThrowError('not mocked'); | ||
(0, globals_1.expect)(() => mockObj.deepProp.getAnotherString('!')).toThrowError('not mocked'); | ||
}); | ||
}); | ||
describe('Deep mock support for class variables which are functions but also have nested properties and functions', () => { | ||
test('can deep mock members', () => { | ||
(0, globals_1.describe)('Deep mock support for class variables which are functions but also have nested properties and functions', () => { | ||
(0, globals_1.test)('can deep mock members', () => { | ||
const mockObj = (0, Mock_1.mockDeep)({ funcPropSupport: true }); | ||
const input = new Test1(1); | ||
mockObj.funcValueProp.nonDeepProp.calledWith(input).mockReturnValue(4); | ||
expect(mockObj.funcValueProp.nonDeepProp(input)).toBe(4); | ||
(0, globals_1.expect)(mockObj.funcValueProp.nonDeepProp(input)).toBe(4); | ||
}); | ||
test('three or more level deep mock', () => { | ||
(0, globals_1.test)('three or more level deep mock', () => { | ||
const mockObj = (0, Mock_1.mockDeep)({ funcPropSupport: true }); | ||
mockObj.funcValueProp.deepProp.deeperProp.getNumber.calledWith(1).mockReturnValue(4); | ||
expect(mockObj.funcValueProp.deepProp.deeperProp.getNumber(1)).toBe(4); | ||
(0, globals_1.expect)(mockObj.funcValueProp.deepProp.deeperProp.getNumber(1)).toBe(4); | ||
}); | ||
test('maintains API for deep mocks', () => { | ||
(0, globals_1.test)('maintains API for deep mocks', () => { | ||
const mockObj = (0, Mock_1.mockDeep)({ funcPropSupport: true }); | ||
mockObj.funcValueProp.deepProp.getNumber(100); | ||
expect(mockObj.funcValueProp.deepProp.getNumber.mock.calls[0][0]).toBe(100); | ||
(0, globals_1.expect)(mockObj.funcValueProp.deepProp.getNumber.mock.calls[0][0]).toBe(100); | ||
}); | ||
test('deep expectation work as expected', () => { | ||
(0, globals_1.test)('deep expectation work as expected', () => { | ||
const mockObj = (0, Mock_1.mockDeep)(); | ||
mockObj.funcValueProp.deepProp.getNumber(2); | ||
expect(mockObj.funcValueProp.deepProp.getNumber).toHaveBeenCalledTimes(1); | ||
(0, globals_1.expect)(mockObj.funcValueProp.deepProp.getNumber).toHaveBeenCalledTimes(1); | ||
}); | ||
test('can mock base function which have properties', () => { | ||
(0, globals_1.test)('can mock base function which have properties', () => { | ||
const mockObj = (0, Mock_1.mockDeep)(); | ||
mockObj.funcValueProp.calledWith(1).mockReturnValue(2); | ||
expect(mockObj.funcValueProp(1)).toBe(2); | ||
(0, globals_1.expect)(mockObj.funcValueProp(1)).toBe(2); | ||
}); | ||
test('base function expectation work as expected', () => { | ||
(0, globals_1.test)('base function expectation work as expected', () => { | ||
const mockObj = (0, Mock_1.mockDeep)(); | ||
mockObj.funcValueProp(1); | ||
expect(mockObj.funcValueProp).toHaveBeenCalledTimes(1); | ||
(0, globals_1.expect)(mockObj.funcValueProp).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
describe('mock implementation support', () => { | ||
test('can provide mock implementation for props', () => { | ||
(0, globals_1.describe)('mock implementation support', () => { | ||
(0, globals_1.test)('can provide mock implementation for props', () => { | ||
const mockObj = (0, Mock_1.default)({ | ||
id: 61, | ||
}); | ||
expect(mockObj.id).toBe(61); | ||
(0, globals_1.expect)(mockObj.id).toBe(61); | ||
}); | ||
test('can provide mock implementation for functions', () => { | ||
(0, globals_1.test)('can provide mock implementation for functions', () => { | ||
const mockObj = (0, Mock_1.default)({ | ||
@@ -352,5 +365,5 @@ getNumber: () => { | ||
}); | ||
expect(mockObj.getNumber()).toBe(150); | ||
(0, globals_1.expect)(mockObj.getNumber()).toBe(150); | ||
}); | ||
test('Partially mocked implementations can have non-mocked function expectations', () => { | ||
(0, globals_1.test)('Partially mocked implementations can have non-mocked function expectations', () => { | ||
const mockObj = (0, Mock_1.default)({ | ||
@@ -362,5 +375,5 @@ getNumber: () => { | ||
mockObj.getSomethingWithArgs.calledWith(1, 2).mockReturnValue(3); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
}); | ||
test('can provide deep mock implementations', () => { | ||
(0, globals_1.test)('can provide deep mock implementations', () => { | ||
const mockObj = (0, Mock_1.mockDeep)({ | ||
@@ -373,5 +386,5 @@ deepProp: { | ||
}); | ||
expect(mockObj.deepProp.getNumber(123)).toBe(76); | ||
(0, globals_1.expect)(mockObj.deepProp.getNumber(123)).toBe(76); | ||
}); | ||
test('Partially mocked implementations of deep mocks can have non-mocked function expectations', () => { | ||
(0, globals_1.test)('Partially mocked implementations of deep mocks can have non-mocked function expectations', () => { | ||
const mockObj = (0, Mock_1.mockDeep)({ | ||
@@ -385,14 +398,14 @@ deepProp: { | ||
mockObj.deepProp.getAnotherString.calledWith('abc').mockReturnValue('this string'); | ||
expect(mockObj.deepProp.getAnotherString('abc')).toBe('this string'); | ||
(0, globals_1.expect)(mockObj.deepProp.getAnotherString('abc')).toBe('this string'); | ||
}); | ||
}); | ||
describe('Promise', () => { | ||
test('Can return as Promise.resolve', async () => { | ||
(0, globals_1.describe)('Promise', () => { | ||
(0, globals_1.test)('Can return as Promise.resolve', async () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.id = 17; | ||
const promiseMockObj = Promise.resolve(mockObj); | ||
await expect(promiseMockObj).resolves.toBeDefined(); | ||
await expect(promiseMockObj).resolves.toMatchObject({ id: 17 }); | ||
await (0, globals_1.expect)(promiseMockObj).resolves.toBeDefined(); | ||
await (0, globals_1.expect)(promiseMockObj).resolves.toMatchObject({ id: 17 }); | ||
}); | ||
test('Can return as Promise.reject', async () => { | ||
(0, globals_1.test)('Can return as Promise.reject', async () => { | ||
const mockError = (0, Mock_1.default)(); | ||
@@ -403,76 +416,76 @@ mockError.message = '17'; | ||
await promiseMockObj; | ||
fail('Promise must be rejected'); | ||
(0, assert_1.fail)('Promise must be rejected'); | ||
} | ||
catch (e) { | ||
await expect(e).toBeDefined(); | ||
await expect(e).toBe(mockError); | ||
await expect(e).toHaveProperty('message', '17'); | ||
await (0, globals_1.expect)(e).toBeDefined(); | ||
await (0, globals_1.expect)(e).toBe(mockError); | ||
await (0, globals_1.expect)(e).toHaveProperty('message', '17'); | ||
} | ||
await expect(promiseMockObj).rejects.toBeDefined(); | ||
await expect(promiseMockObj).rejects.toBe(mockError); | ||
await expect(promiseMockObj).rejects.toHaveProperty('message', '17'); | ||
await (0, globals_1.expect)(promiseMockObj).rejects.toBeDefined(); | ||
await (0, globals_1.expect)(promiseMockObj).rejects.toBe(mockError); | ||
await (0, globals_1.expect)(promiseMockObj).rejects.toHaveProperty('message', '17'); | ||
}); | ||
test('Can mock a then function', async () => { | ||
(0, globals_1.test)('Can mock a then function', async () => { | ||
const mockPromiseObj = Promise.resolve(42); | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.id = 17; | ||
// @ts-ignore | ||
// @ts-expect-error | ||
mockObj.then = mockPromiseObj.then.bind(mockPromiseObj); | ||
const promiseMockObj = Promise.resolve(mockObj); | ||
await promiseMockObj; | ||
await expect(promiseMockObj).resolves.toBeDefined(); | ||
await expect(promiseMockObj).resolves.toEqual(42); | ||
await (0, globals_1.expect)(promiseMockObj).resolves.toBeDefined(); | ||
await (0, globals_1.expect)(promiseMockObj).resolves.toEqual(42); | ||
}); | ||
}); | ||
describe('clearing / resetting', () => { | ||
test('mockReset supports jest.fn()', () => { | ||
const fn = jest.fn().mockImplementation(() => true); | ||
expect(fn()).toBe(true); | ||
(0, globals_1.describe)('clearing / resetting', () => { | ||
(0, globals_1.test)('mockReset supports jest.fn()', () => { | ||
const fn = globals_1.jest.fn().mockImplementation(() => true); | ||
(0, globals_1.expect)(fn()).toBe(true); | ||
(0, Mock_1.mockReset)(fn); | ||
expect(fn()).toBe(undefined); | ||
(0, globals_1.expect)(fn()).toBe(undefined); | ||
}); | ||
test('mockClear supports jest.fn()', () => { | ||
const fn = jest.fn().mockImplementation(() => true); | ||
(0, globals_1.test)('mockClear supports jest.fn()', () => { | ||
const fn = globals_1.jest.fn().mockImplementation(() => true); | ||
fn(); | ||
expect(fn.mock.calls.length).toBe(1); | ||
(0, globals_1.expect)(fn.mock.calls.length).toBe(1); | ||
(0, Mock_1.mockClear)(fn); | ||
expect(fn.mock.calls.length).toBe(0); | ||
(0, globals_1.expect)(fn.mock.calls.length).toBe(0); | ||
}); | ||
test('mockReset object', () => { | ||
(0, globals_1.test)('mockReset object', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithArgs.calledWith(1, (0, Matchers_1.anyNumber)()).mockReturnValue(3); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
(0, Mock_1.mockReset)(mockObj); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(undefined); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(undefined); | ||
mockObj.getSomethingWithArgs.calledWith(1, (0, Matchers_1.anyNumber)()).mockReturnValue(3); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
}); | ||
test('mockClear object', () => { | ||
(0, globals_1.test)('mockClear object', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.getSomethingWithArgs.calledWith(1, (0, Matchers_1.anyNumber)()).mockReturnValue(3); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
expect(mockObj.getSomethingWithArgs.mock.calls.length).toBe(1); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs.mock.calls.length).toBe(1); | ||
(0, Mock_1.mockClear)(mockObj); | ||
expect(mockObj.getSomethingWithArgs.mock.calls.length).toBe(0); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs.mock.calls.length).toBe(0); | ||
// Does not clear mock implementations of calledWith | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
}); | ||
test('mockReset deep', () => { | ||
(0, globals_1.test)('mockReset deep', () => { | ||
const mockObj = (0, Mock_1.mockDeep)(); | ||
mockObj.deepProp.getNumber.calledWith(1).mockReturnValue(4); | ||
expect(mockObj.deepProp.getNumber(1)).toBe(4); | ||
(0, globals_1.expect)(mockObj.deepProp.getNumber(1)).toBe(4); | ||
(0, Mock_1.mockReset)(mockObj); | ||
expect(mockObj.deepProp.getNumber(1)).toBe(undefined); | ||
(0, globals_1.expect)(mockObj.deepProp.getNumber(1)).toBe(undefined); | ||
}); | ||
test('mockClear deep', () => { | ||
(0, globals_1.test)('mockClear deep', () => { | ||
const mockObj = (0, Mock_1.mockDeep)(); | ||
mockObj.deepProp.getNumber.calledWith(1).mockReturnValue(4); | ||
expect(mockObj.deepProp.getNumber(1)).toBe(4); | ||
expect(mockObj.deepProp.getNumber.mock.calls.length).toBe(1); | ||
(0, globals_1.expect)(mockObj.deepProp.getNumber(1)).toBe(4); | ||
(0, globals_1.expect)(mockObj.deepProp.getNumber.mock.calls.length).toBe(1); | ||
(0, Mock_1.mockClear)(mockObj); | ||
expect(mockObj.deepProp.getNumber.mock.calls.length).toBe(0); | ||
(0, globals_1.expect)(mockObj.deepProp.getNumber.mock.calls.length).toBe(0); | ||
// Does not clear mock implementations of calledWith | ||
expect(mockObj.deepProp.getNumber(1)).toBe(4); | ||
(0, globals_1.expect)(mockObj.deepProp.getNumber(1)).toBe(4); | ||
}); | ||
test('mockReset ignores undefined properties', () => { | ||
(0, globals_1.test)('mockReset ignores undefined properties', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
@@ -482,5 +495,5 @@ mockObj.someValue = undefined; | ||
(0, Mock_1.mockReset)(mockObj); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(undefined); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(undefined); | ||
}); | ||
test('mockReset ignores null properties', () => { | ||
(0, globals_1.test)('mockReset ignores null properties', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
@@ -490,67 +503,67 @@ mockObj.someValue = null; | ||
(0, Mock_1.mockReset)(mockObj); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(undefined); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(undefined); | ||
}); | ||
test('mockClear ignores undefined properties', () => { | ||
(0, globals_1.test)('mockClear ignores undefined properties', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.someValue = undefined; | ||
mockObj.getSomethingWithArgs.calledWith(1, (0, Matchers_1.anyNumber)()).mockReturnValue(3); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
expect(mockObj.getSomethingWithArgs.mock.calls.length).toBe(1); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs.mock.calls.length).toBe(1); | ||
(0, Mock_1.mockClear)(mockObj); | ||
expect(mockObj.getSomethingWithArgs.mock.calls.length).toBe(0); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs.mock.calls.length).toBe(0); | ||
}); | ||
test('mockClear ignores null properties', () => { | ||
(0, globals_1.test)('mockClear ignores null properties', () => { | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.someValue = null; | ||
mockObj.getSomethingWithArgs.calledWith(1, (0, Matchers_1.anyNumber)()).mockReturnValue(3); | ||
expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
expect(mockObj.getSomethingWithArgs.mock.calls.length).toBe(1); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs(1, 2)).toBe(3); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs.mock.calls.length).toBe(1); | ||
(0, Mock_1.mockClear)(mockObj); | ||
expect(mockObj.getSomethingWithArgs.mock.calls.length).toBe(0); | ||
(0, globals_1.expect)(mockObj.getSomethingWithArgs.mock.calls.length).toBe(0); | ||
}); | ||
}); | ||
describe('function mock', () => { | ||
test('should mock function', async () => { | ||
(0, globals_1.describe)('function mock', () => { | ||
(0, globals_1.test)('should mock function', async () => { | ||
const mockFunc = (0, Mock_1.mockFn)(); | ||
mockFunc.mockResolvedValue(`str`); | ||
const result = await mockFunc(1, 2); | ||
expect(result).toBe(`str`); | ||
(0, globals_1.expect)(result).toBe(`str`); | ||
}); | ||
test('should mock function and use calledWith', async () => { | ||
(0, globals_1.test)('should mock function and use calledWith', async () => { | ||
const mockFunc = (0, Mock_1.mockFn)(); | ||
mockFunc.calledWith(1, 2).mockResolvedValue(`str`); | ||
const result = await mockFunc(1, 2); | ||
expect(result).toBe(`str`); | ||
(0, globals_1.expect)(result).toBe(`str`); | ||
}); | ||
}); | ||
describe('ignoreProps', () => { | ||
test('can configure ignoreProps', async () => { | ||
(0, globals_1.describe)('ignoreProps', () => { | ||
(0, globals_1.test)('can configure ignoreProps', async () => { | ||
Mock_1.JestMockExtended.configure({ ignoreProps: ['ignoreMe'] }); | ||
const mockObj = (0, Mock_1.default)(); | ||
expect(mockObj.ignoreMe).toBeUndefined(); | ||
expect(mockObj.dontIgnoreMe).toBeDefined(); | ||
(0, globals_1.expect)(mockObj.ignoreMe).toBeUndefined(); | ||
(0, globals_1.expect)(mockObj.dontIgnoreMe).toBeDefined(); | ||
}); | ||
}); | ||
describe('JestMockExtended config', () => { | ||
test('can mock then', async () => { | ||
(0, globals_1.describe)('JestMockExtended config', () => { | ||
(0, globals_1.test)('can mock then', async () => { | ||
Mock_1.JestMockExtended.configure({ ignoreProps: [] }); | ||
const mockObj = (0, Mock_1.default)(); | ||
mockObj.then(); | ||
expect(mockObj.then).toHaveBeenCalled(); | ||
(0, globals_1.expect)(mockObj.then).toHaveBeenCalled(); | ||
}); | ||
test('can reset config', async () => { | ||
(0, globals_1.test)('can reset config', async () => { | ||
Mock_1.JestMockExtended.configure({ ignoreProps: [] }); | ||
Mock_1.JestMockExtended.resetConfig(); | ||
const mockObj = (0, Mock_1.default)(); | ||
expect(mockObj.then).toBeUndefined(); | ||
(0, globals_1.expect)(mockObj.then).toBeUndefined(); | ||
}); | ||
}); | ||
describe('mock Date', () => { | ||
test('should call built-in date functions', () => { | ||
(0, globals_1.describe)('mock Date', () => { | ||
(0, globals_1.test)('should call built-in date functions', () => { | ||
const mockObj = (0, Mock_1.default)({ date: new Date('2000-01-15') }); | ||
expect(mockObj.date.getFullYear()).toBe(2000); | ||
expect(mockObj.date.getMonth()).toBe(0); | ||
expect(mockObj.date.getDate()).toBe(15); | ||
(0, globals_1.expect)(mockObj.date.getFullYear()).toBe(2000); | ||
(0, globals_1.expect)(mockObj.date.getMonth()).toBe(0); | ||
(0, globals_1.expect)(mockObj.date.getDate()).toBe(15); | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "jest-mock-extended", | ||
"version": "3.0.7", | ||
"version": "4.0.0-beta1", | ||
"homepage": "https://github.com/marchaos/jest-mock-extended", | ||
@@ -23,6 +23,5 @@ "description": "Type safe mocking extensions for jest", | ||
"dependencies": { | ||
"ts-essentials": "^10.0.0" | ||
"ts-essentials": "^10.0.2" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^27.5.0", | ||
"coveralls": "^3.1.1", | ||
@@ -36,2 +35,3 @@ "jest": "^29.5.0", | ||
"peerDependencies": { | ||
"@jest/globals": "^28.0.0 || ^29.0.0", | ||
"jest": "^24.0.0 || ^25.0.0 || ^26.0.0 || ^27.0.0 || ^28.0.0 || ^29.0.0", | ||
@@ -38,0 +38,0 @@ "typescript": "^3.0.0 || ^4.0.0 || ^5.0.0" |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
79897
6
1407
4
1
Updatedts-essentials@^10.0.2