From 07ef837bf86dbf7198e488d463e4157e42d36605 Mon Sep 17 00:00:00 2001 From: rezoled Date: Sun, 14 Jul 2024 17:19:35 +0300 Subject: [PATCH 1/3] 2.6.8 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 222f4fd..f01799c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@typestrong/ts-mockito", - "version": "2.6.7", + "version": "2.6.8", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@typestrong/ts-mockito", - "version": "2.6.7", + "version": "2.6.8", "license": "MIT", "dependencies": { "@babel/parser": "^7.24.7", diff --git a/package.json b/package.json index 7ebfc80..09f353b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@typestrong/ts-mockito", - "version": "2.6.7", + "version": "2.6.8", "description": "Mocking library for TypeScript", "main": "lib/ts-mockito.js", "typings": "lib/ts-mockito", From 65ffd91ec9bf372bdf2299f00667cedde04d051a Mon Sep 17 00:00:00 2001 From: rezoled Date: Sun, 14 Jul 2024 17:22:05 +0300 Subject: [PATCH 2/3] support custom error messages in verify --- src/MethodStubVerificator.ts | 28 ++++++++++++++++------------ src/ts-mockito.ts | 4 ++-- test/verification.spec.ts | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/MethodStubVerificator.ts b/src/MethodStubVerificator.ts index 6d41b9c..7a46d01 100644 --- a/src/MethodStubVerificator.ts +++ b/src/MethodStubVerificator.ts @@ -4,7 +4,7 @@ import {MethodCallToStringConverter} from "./utils/MethodCallToStringConverter"; export class MethodStubVerificator { private methodCallToStringConverter: MethodCallToStringConverter = new MethodCallToStringConverter(); - constructor(private methodToVerify: MethodToStub) { + constructor(private methodToVerify: MethodToStub, private errorMessage?: string) { } @@ -33,7 +33,7 @@ export class MethodStubVerificator { if (value !== allMatchingActions.length) { const methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify); const msg = `Expected "${methodToVerifyAsString}to be called ${value} time(s). But has been called ${allMatchingActions.length} time(s).`; - throw new Error(`${msg} + this.throw(`${msg} ${this.actualCalls()}`); } } @@ -42,7 +42,7 @@ ${this.actualCalls()}`); const allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers); if (value > allMatchingActions.length) { const methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify); - throw new Error(`Expected "${methodToVerifyAsString}to be called at least ${value} time(s). But has been called ${allMatchingActions.length} time(s).`); + this.throw(`Expected "${methodToVerifyAsString}to be called at least ${value} time(s). But has been called ${allMatchingActions.length} time(s).`); } } @@ -50,7 +50,7 @@ ${this.actualCalls()}`); const allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers); if (value < allMatchingActions.length) { const methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify); - throw new Error(`Expected "${methodToVerifyAsString}to be called at least ${value} time(s). But has been called ${allMatchingActions.length} time(s).`); + this.throw(`Expected "${methodToVerifyAsString}to be called at least ${value} time(s). But has been called ${allMatchingActions.length} time(s).`); } } @@ -63,14 +63,14 @@ ${this.actualCalls()}`); if (firstMethodAction && secondMethodAction) { if (!firstMethodAction.hasBeenCalledBefore(secondMethodAction)) { - throw new Error(`${errorBeginning}but has been called after.`); + this.throw(`${errorBeginning}but has been called after.`); } } else if (firstMethodAction && !secondMethodAction) { - throw new Error(`${errorBeginning}but ${secondMethodAsString}has never been called.`); + this.throw(`${errorBeginning}but ${secondMethodAsString}has never been called.`); } else if (!firstMethodAction && secondMethodAction) { - throw new Error(`${errorBeginning}but ${mainMethodToVerifyAsString}has never been called.`); + this.throw(`${errorBeginning}but ${mainMethodToVerifyAsString}has never been called.`); } else { - throw new Error(`${errorBeginning}but none of them has been called.`); + this.throw(`${errorBeginning}but none of them has been called.`); } } @@ -83,14 +83,14 @@ ${this.actualCalls()}`); if (firstMethodAction && secondMethodAction) { if (firstMethodAction.hasBeenCalledBefore(secondMethodAction)) { - throw new Error(`${errorBeginning}but has been called before.`); + this.throw(`${errorBeginning}but has been called before.`); } } else if (firstMethodAction && !secondMethodAction) { - throw new Error(`${errorBeginning}but ${secondMethodAsString}has never been called.`); + this.throw(`${errorBeginning}but ${secondMethodAsString}has never been called.`); } else if (!firstMethodAction && secondMethodAction) { - throw new Error(`${errorBeginning}but ${mainMethodToVerifyAsString}has never been called.`); + this.throw(`${errorBeginning}but ${mainMethodToVerifyAsString}has never been called.`); } else { - throw new Error(`${errorBeginning}but none of them has been called.`); + this.throw(`${errorBeginning}but none of them has been called.`); } } @@ -99,4 +99,8 @@ ${this.actualCalls()}`); return `Actual calls: ${this.methodCallToStringConverter.convertActualCalls(calls).join("\n ")}`; } + + private throw(message: string) { + throw new Error(this.errorMessage ?? message); + } } diff --git a/src/ts-mockito.ts b/src/ts-mockito.ts index b4951a4..62b4898 100644 --- a/src/ts-mockito.ts +++ b/src/ts-mockito.ts @@ -38,8 +38,8 @@ export function mock(clazz?: any): T { return new Mocker(clazz).getMock(); } -export function verify(method: T): MethodStubVerificator { - return new MethodStubVerificator(method as any); +export function verify(method: T, errorMessage?:string): MethodStubVerificator { + return new MethodStubVerificator(method as any, errorMessage); } export function when(method: Promise): MethodStubSetter, T, Error>; diff --git a/test/verification.spec.ts b/test/verification.spec.ts index 83264e1..7ce2020 100644 --- a/test/verification.spec.ts +++ b/test/verification.spec.ts @@ -817,6 +817,23 @@ cases.forEach(testData => { expect(e.message).toContain(`sampleMethodWithObjectArguments({\"foo\":\"baz\"})`); } }); + + it("should describe error with the supplied error message", () => { + instance(mockedFoo).sampleMethodWithObjectArguments({foo: 'baz'}); + + try { + // when + verify( + mockedFoo.sampleMethodWithObjectArguments(deepEqual({foo: 'bar'})), + 'sampleMethodWithObjectArguments should return baz!', + ).once(); + + expect(true).toBe(false); // Above call should throw an exception + } catch (e) { + // then + expect(e.message).toContain('sampleMethodWithObjectArguments should return baz!'); + } + }); }); }); }); From 65d902d605c518cc98319d9524e14f67df0ad13a Mon Sep 17 00:00:00 2001 From: rezoled Date: Sun, 14 Jul 2024 18:57:28 +0300 Subject: [PATCH 3/3] added readme --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 637f5c6..58dc7db 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,13 @@ verify(mockedFoo.getBar(3)).called(); verify(mockedFoo.getBar(anything())).called(); ``` +#### Throwing custom test error message +The verify assertion will throw a test error with predefined error template. +If you need to throw a different error message in the test you can supply the ```verify``` command with an optional error message parameter: +``` typescript +verify(mockedFoo.getBar(), 'getBar should have been called, but didnt!').called(); +``` + ### Stubbing method calls ``` typescript