diff --git a/package-lock.json b/package-lock.json index 222f4fd..782ba0d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@typestrong/ts-mockito", - "version": "2.6.7", + "version": "2.6.10", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@typestrong/ts-mockito", - "version": "2.6.7", + "version": "2.6.10", "license": "MIT", "dependencies": { "@babel/parser": "^7.24.7", diff --git a/package.json b/package.json index 7ebfc80..8ad8028 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@typestrong/ts-mockito", - "version": "2.6.7", + "version": "2.6.10", "description": "Mocking library for TypeScript", "main": "lib/ts-mockito.js", "typings": "lib/ts-mockito", diff --git a/src/matcher/type/AnyFunctionMatcher.ts b/src/matcher/type/AnyFunctionMatcher.ts index bf9a1b9..30fe2dd 100644 --- a/src/matcher/type/AnyFunctionMatcher.ts +++ b/src/matcher/type/AnyFunctionMatcher.ts @@ -1,12 +1,12 @@ import * as _ from "lodash"; import {Matcher} from "./Matcher"; -export class AnyFunctionMatcher extends Matcher { +export class AnyFunctionMatcher extends Matcher { constructor() { super(); } - public match(value: any): boolean { + public match(value: T): boolean { return _.isFunction(value); } diff --git a/src/matcher/type/AnyNumberMatcher.ts b/src/matcher/type/AnyNumberMatcher.ts index 9a69149..11efb4f 100644 --- a/src/matcher/type/AnyNumberMatcher.ts +++ b/src/matcher/type/AnyNumberMatcher.ts @@ -1,12 +1,12 @@ import * as _ from "lodash"; import {Matcher} from "./Matcher"; -export class AnyNumberMatcher extends Matcher { +export class AnyNumberMatcher extends Matcher { constructor() { super(); } - public match(value: any): boolean { + public match(value: number): boolean { return _.isNumber(value); } diff --git a/src/matcher/type/AnyOfClassMatcher.ts b/src/matcher/type/AnyOfClassMatcher.ts index 07246e5..111ee76 100644 --- a/src/matcher/type/AnyOfClassMatcher.ts +++ b/src/matcher/type/AnyOfClassMatcher.ts @@ -1,6 +1,6 @@ import {Matcher} from "./Matcher"; -export class AnyOfClassMatcher extends Matcher { +export class AnyOfClassMatcher extends Matcher { constructor(private expectedClass: new (...args: any[]) => T) { super(); if (expectedClass === null) { @@ -8,7 +8,7 @@ export class AnyOfClassMatcher extends Matcher { } } - public match(value: any): boolean { + public match(value: T): boolean { return value instanceof this.expectedClass; } diff --git a/src/matcher/type/AnyStringMatcher.ts b/src/matcher/type/AnyStringMatcher.ts index efbab75..adab133 100644 --- a/src/matcher/type/AnyStringMatcher.ts +++ b/src/matcher/type/AnyStringMatcher.ts @@ -1,12 +1,12 @@ import * as _ from "lodash"; import {Matcher} from "./Matcher"; -export class AnyStringMatcher extends Matcher { +export class AnyStringMatcher extends Matcher { constructor() { super(); } - public match(value: any): boolean { + public match(value: string): boolean { return _.isString(value); } diff --git a/src/matcher/type/BetweenMatcher.ts b/src/matcher/type/BetweenMatcher.ts index 5a8bc56..017f75d 100644 --- a/src/matcher/type/BetweenMatcher.ts +++ b/src/matcher/type/BetweenMatcher.ts @@ -1,6 +1,6 @@ import {Matcher} from "./Matcher"; -export class BetweenMatcher extends Matcher { +export class BetweenMatcher extends Matcher { constructor(private min: number, private max: number) { super(); @@ -9,7 +9,7 @@ export class BetweenMatcher extends Matcher { } } - public match(value: any): boolean { + public match(value: number): boolean { return value >= this.min && value <= this.max; } diff --git a/src/matcher/type/DeepEqualMatcher.ts b/src/matcher/type/DeepEqualMatcher.ts index 2fe8afe..3e889c1 100644 --- a/src/matcher/type/DeepEqualMatcher.ts +++ b/src/matcher/type/DeepEqualMatcher.ts @@ -1,14 +1,14 @@ import * as _ from "lodash"; import {Matcher} from "./Matcher"; import * as safeJsonStringify from "safe-json-stringify"; -export class DeepEqualMatcher extends Matcher { +export class DeepEqualMatcher extends Matcher { constructor(private expectedValue: T) { super(); } - public match(value: any): boolean { + public match(value: T): boolean { return _.isEqualWith(this.expectedValue, value, - (expected: any, actual: any): boolean => { + (expected: any, actual: any) => { if (expected instanceof Matcher) { return expected.match(actual); } diff --git a/src/matcher/type/Matcher.ts b/src/matcher/type/Matcher.ts index 87bbc83..959a39e 100644 --- a/src/matcher/type/Matcher.ts +++ b/src/matcher/type/Matcher.ts @@ -1,5 +1,5 @@ -export class Matcher { - public match(value: any): boolean { +export class Matcher { + public match(value: T): boolean { return false; } diff --git a/src/matcher/type/MatchingStringMatcher.ts b/src/matcher/type/MatchingStringMatcher.ts index 2806500..bfb3654 100644 --- a/src/matcher/type/MatchingStringMatcher.ts +++ b/src/matcher/type/MatchingStringMatcher.ts @@ -1,12 +1,12 @@ import {Matcher} from "./Matcher"; -export class MatchingStringMatcher extends Matcher { - constructor(private expectedValue: any) { +export class MatchingStringMatcher extends Matcher { + constructor(private expectedValue: RegExp | string) { super(); } - public match(value: any): boolean { - return value.match(this.expectedValue); + public match(value: string): boolean { + return Boolean(value.match(this.expectedValue)); } public toString(): string { diff --git a/src/matcher/type/ObjectContainingMatcher.ts b/src/matcher/type/ObjectContainingMatcher.ts index 1f157e5..81ccdcd 100644 --- a/src/matcher/type/ObjectContainingMatcher.ts +++ b/src/matcher/type/ObjectContainingMatcher.ts @@ -1,12 +1,12 @@ import * as _ from "lodash"; import {Matcher} from "./Matcher"; -export class ObjectContainingMatcher extends Matcher { +export class ObjectContainingMatcher extends Matcher { constructor(private expectedValue: any) { super(); } - public match(value: Object): boolean { + public match(value: T): boolean { return _.isMatch(value, this.expectedValue); } diff --git a/src/matcher/type/StrictEqualMatcher.ts b/src/matcher/type/StrictEqualMatcher.ts index 593c2de..770d4d9 100644 --- a/src/matcher/type/StrictEqualMatcher.ts +++ b/src/matcher/type/StrictEqualMatcher.ts @@ -1,11 +1,11 @@ import {Matcher} from "./Matcher"; -export class StrictEqualMatcher extends Matcher { +export class StrictEqualMatcher extends Matcher { constructor(private expectedValue: any) { super(); } - public match(value: any): boolean { + public match(value: T): boolean { return this.expectedValue === value; } diff --git a/src/ts-mockito.ts b/src/ts-mockito.ts index b4951a4..87d8fbb 100644 --- a/src/ts-mockito.ts +++ b/src/ts-mockito.ts @@ -27,43 +27,44 @@ import {MethodStubVerificator} from "./MethodStubVerificator"; import {MethodToStub} from "./MethodToStub"; import {Mocker} from "./Mock"; import {Spy} from "./Spy"; +import {Mocked, MockedProp} from "./utils/types"; -export function spy(instanceToSpy: T): T { +export function spy(instanceToSpy: T): Mocked { return new Spy(instanceToSpy).getMock(); } -export function mock(clazz: (new(...args: any[]) => T) | (Function & { prototype: T }) ): T; -export function mock(clazz?: any): T; -export function mock(clazz?: any): T { +export function mock(clazz: (new(...args: any[]) => T) | (Function & { prototype: T }) ): Mocked; +export function mock(clazz?: any): Mocked; +export function mock(clazz?: any): Mocked { return new Mocker(clazz).getMock(); } -export function verify(method: T): MethodStubVerificator { +export function verify(method: MockedProp): MethodStubVerificator { return new MethodStubVerificator(method as any); } -export function when(method: Promise): MethodStubSetter, T, Error>; -export function when(method: T): MethodStubSetter; +export function when(method: Promise>): MethodStubSetter, T, Error>; +export function when(method: MockedProp): MethodStubSetter; export function when(method: any): any { return new MethodStubSetter(method); } -export function instance(mockedValue: T): T { +export function instance(mockedValue: Mocked): T { const tsmockitoInstance = (mockedValue as any).__tsmockitoInstance as T; return tsmockitoInstance; } -export function capture(method: (a: T0, b: T1, c: T2, d: T3, e: T4, f: T5, g: T6, h: T7, i: T8, j: T9) => any): ArgCaptor10; -export function capture(method: (a: T0, b: T1, c: T2, d: T3, e: T4, f: T5, g: T6, h: T7, i: T8) => any): ArgCaptor9; -export function capture(method: (a: T0, b: T1, c: T2, d: T3, e: T4, f: T5, g: T6, h: T7) => any): ArgCaptor8; -export function capture(method: (a: T0, b: T1, c: T2, d: T3, e: T4, f: T5, g: T6) => any): ArgCaptor7; -export function capture(method: (a: T0, b: T1, c: T2, d: T3, e: T4, f: T5) => any): ArgCaptor6; -export function capture(method: (a: T0, b: T1, c: T2, d: T3, e: T4) => any): ArgCaptor5; -export function capture(method: (a: T0, b: T1, c: T2, d: T3) => any): ArgCaptor4; -export function capture(method: (a: T0, b: T1, c: T2) => any): ArgCaptor3; -export function capture(method: (a: T0, b: T1) => any): ArgCaptor2; -export function capture(method: (a: T0) => any): ArgCaptor1; -export function capture(method: (...args: any[]) => any): ArgCaptor { +export function capture(method: MockedProp<(a: T0, b: T1, c: T2, d: T3, e: T4, f: T5, g: T6, h: T7, i: T8, j: T9) => any>): ArgCaptor10; +export function capture(method: MockedProp<(a: T0, b: T1, c: T2, d: T3, e: T4, f: T5, g: T6, h: T7, i: T8) => any>): ArgCaptor9; +export function capture(method: MockedProp<(a: T0, b: T1, c: T2, d: T3, e: T4, f: T5, g: T6, h: T7) => any>): ArgCaptor8; +export function capture(method: MockedProp<(a: T0, b: T1, c: T2, d: T3, e: T4, f: T5, g: T6) => any>): ArgCaptor7; +export function capture(method: MockedProp<(a: T0, b: T1, c: T2, d: T3, e: T4, f: T5) => any>): ArgCaptor6; +export function capture(method: MockedProp<(a: T0, b: T1, c: T2, d: T3, e: T4) => any>): ArgCaptor5; +export function capture(method: MockedProp<(a: T0, b: T1, c: T2, d: T3) => any>): ArgCaptor4; +export function capture(method: MockedProp<(a: T0, b: T1, c: T2) => any>): ArgCaptor3; +export function capture(method: MockedProp<(a: T0, b: T1) => any>): ArgCaptor2; +export function capture(method: MockedProp<(a: T0) => any>): ArgCaptor1; +export function capture(method: MockedProp<(...args: any[]) => any>): ArgCaptor { const methodStub: MethodToStub = method(); if (methodStub instanceof MethodToStub) { const actions = methodStub.mocker.getActionsByName(methodStub.name); @@ -73,56 +74,56 @@ export function capture(method: (...args: any[]) => any): ArgCaptor { } } -export function reset(...mockedValues: T[]): void { +export function reset(...mockedValues: Mocked[]): void { mockedValues.forEach(mockedValue => (mockedValue as any).__tsmockitoMocker.reset()); } -export function resetCalls(...mockedValues: T[]): void { +export function resetCalls(...mockedValues: Mocked[]): void { mockedValues.forEach(mockedValue => (mockedValue as any).__tsmockitoMocker.resetCalls()); } -export function anyOfClass(expectedClass: new (...args: any[]) => T): any { - return new AnyOfClassMatcher(expectedClass) as any; +export function anyOfClass(expectedClass: new (...args: any[]) => T) { + return new AnyOfClassMatcher(expectedClass); } -export function anyFunction(): any { - return new AnyFunctionMatcher() as any; +export function anyFunction() { + return new AnyFunctionMatcher(); } -export function anyNumber(): any { - return new AnyNumberMatcher() as any; +export function anyNumber() { + return new AnyNumberMatcher(); } -export function anyString(): any { - return new AnyStringMatcher() as any; +export function anyString() { + return new AnyStringMatcher(); } -export function anything(): any { - return new AnythingMatcher() as any; +export function anything() { + return new AnythingMatcher(); } -export function between(min: number, max: number): any { - return new BetweenMatcher(min, max) as any; +export function between(min: number, max: number) { + return new BetweenMatcher(min, max) ; } -export function deepEqual(expectedValue: T): T { - return new DeepEqualMatcher(expectedValue) as any; +export function deepEqual(expectedValue: T) { + return new DeepEqualMatcher(expectedValue); } -export function notNull(): any { - return new NotNullMatcher() as any; +export function notNull() { + return new NotNullMatcher(); } -export function strictEqual(expectedValue: any): any { - return new StrictEqualMatcher(expectedValue) as any; +export function strictEqual(expectedValue: T) { + return new StrictEqualMatcher(expectedValue); } -export function match(expectedValue: RegExp | string): any { - return new MatchingStringMatcher(expectedValue) as any; +export function match(expectedValue: RegExp | string) { + return new MatchingStringMatcher(expectedValue); } -export function objectContaining(expectedValue: T): any { - return new ObjectContainingMatcher(expectedValue) as any; +export function objectContaining(expectedValue: T) { + return new ObjectContainingMatcher(expectedValue); } // Export default object with all members (ember-browserify doesn't support named exports). diff --git a/src/utils/types.ts b/src/utils/types.ts index 6f8b6d2..1a6a631 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -1 +1,14 @@ -export type TODO = any; \ No newline at end of file +import {Matcher} from "../matcher/type/Matcher"; + +type MockedArgs> = { + [I in keyof T]: Matcher | T[I] +}; + +export type MockedProp = + T extends (...args:infer Args) => infer R ? + (...args:MockedArgs) => R : + T; + +export type Mocked = { + [prop in keyof T]: MockedProp +}; diff --git a/test/capturing.spec.ts b/test/capturing.spec.ts index 93ad07a..d050404 100644 --- a/test/capturing.spec.ts +++ b/test/capturing.spec.ts @@ -1,8 +1,9 @@ import {capture, instance, mock} from "../src/ts-mockito"; import {Foo} from "./utils/Foo"; +import {Mocked} from "../src/utils/types"; describe("capturing method arguments", () => { - let mockedFoo: Foo; + let mockedFoo: Mocked; let foo: Foo; beforeEach(() => { diff --git a/test/mocking.getter.spec.ts b/test/mocking.getter.spec.ts index c5d946f..b3c1b1a 100644 --- a/test/mocking.getter.spec.ts +++ b/test/mocking.getter.spec.ts @@ -1,8 +1,9 @@ import {instance, mock, when} from "../src/ts-mockito"; import {Bar} from "./utils/Bar"; +import {Mocked} from "../src/utils/types"; describe("mocking", () => { - let mockedFoo: FooWithGetterAndSetter; + let mockedFoo: Mocked; let foo: FooWithGetterAndSetter; describe("mocking object with getters and setters", () => { diff --git a/test/recording.multiple.behaviors.spec.ts b/test/recording.multiple.behaviors.spec.ts index e3f15e6..3340cde 100644 --- a/test/recording.multiple.behaviors.spec.ts +++ b/test/recording.multiple.behaviors.spec.ts @@ -1,8 +1,9 @@ import {instance, mock, when} from "../src/ts-mockito"; import {Foo} from "./utils/Foo"; +import {Mocked} from "../src/utils/types"; describe("recording multiple behaviors", () => { - let mockedFoo: Foo; + let mockedFoo: Mocked; let foo: Foo; beforeEach(() => { diff --git a/test/reseting.calls.spec.ts b/test/reseting.calls.spec.ts index 4dda5f7..e9fa7ab 100644 --- a/test/reseting.calls.spec.ts +++ b/test/reseting.calls.spec.ts @@ -1,9 +1,10 @@ import { Mocker } from "../src/Mock"; import { anything, instance, mock, resetCalls, verify } from "../src/ts-mockito"; import {Foo} from "./utils/Foo"; +import {Mocked} from "../src/utils/types"; describe("resetting mocked object", () => { - let mockedFoo: Foo; + let mockedFoo: Mocked; let foo: Foo; beforeEach(() => { diff --git a/test/reseting.spec.ts b/test/reseting.spec.ts index df55ad3..6756011 100644 --- a/test/reseting.spec.ts +++ b/test/reseting.spec.ts @@ -1,9 +1,10 @@ import { Mocker } from "../src/Mock"; import {anything, instance, mock, reset, verify, when} from "../src/ts-mockito"; import {Foo} from "./utils/Foo"; +import {Mocked} from "../src/utils/types"; describe("resetting mocked object", () => { - let mockedFoo: Foo; + let mockedFoo: Mocked; let foo: Foo; beforeEach(() => { diff --git a/test/stubbing.method.spec.ts b/test/stubbing.method.spec.ts index 038ad59..f869bc7 100644 --- a/test/stubbing.method.spec.ts +++ b/test/stubbing.method.spec.ts @@ -1,8 +1,9 @@ import {anything, instance, mock, when} from "../src/ts-mockito"; import {Foo} from "./utils/Foo"; +import {Mocked} from "../src/utils/types"; describe("mocking", () => { - let mockedFoo: Foo; + let mockedFoo: Mocked; let foo: Foo; beforeEach(() => {