Skip to content

Commit ec505ac

Browse files
Adding support for verify(...).timeout(ms)
1 parent 93d6625 commit ec505ac

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

src/MethodStubVerificator.ts

+21
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,25 @@ export class MethodStubVerificator<T> {
9191
throw new Error(`${errorBeginning}but none of them has been called.`);
9292
}
9393
}
94+
95+
public timeout(ms: number): Promise<void> {
96+
return new Promise((resolve, reject) => {
97+
const expired = Date.now() + ms;
98+
99+
const check = () => {
100+
const allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
101+
102+
if (allMatchingActions.length > 0) {
103+
resolve();
104+
} else if (Date.now() >= expired) {
105+
const methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
106+
reject(new Error(`Expected "${methodToVerifyAsString}to be called within ${ms} ms.`));
107+
} else {
108+
setTimeout(check, 1);
109+
}
110+
};
111+
112+
check();
113+
});
114+
}
94115
}

test/verification.spec.ts

+54-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {instance, mock, verify} from "../src/ts-mockito";
1+
import {instance, mock, verify, when} from "../src/ts-mockito";
22
import {MethodCallToStringConverter} from "../src/utils/MethodCallToStringConverter";
33
import {Bar} from "./utils/Bar";
44
import {Foo} from "./utils/Foo";
@@ -774,6 +774,59 @@ describe("verifying mocked object", () => {
774774
});
775775
});
776776
});
777+
778+
describe("with timeout", () => {
779+
it("should succeed if call already happend", async () => {
780+
// given
781+
foo.getBar();
782+
783+
// when
784+
await verify(mockedFoo.getBar()).timeout(10000);
785+
786+
// then
787+
verify(mockedFoo.getBar()).once();
788+
});
789+
790+
it("should wait for call to happen", async () => {
791+
// given
792+
setTimeout(() => foo.getBar(), 10);
793+
794+
// when
795+
await verify(mockedFoo.getBar()).timeout(10000);
796+
797+
// then
798+
verify(mockedFoo.getBar()).once();
799+
});
800+
801+
it("should fail if call does not happen", async () => {
802+
// given
803+
804+
// when
805+
let error;
806+
try {
807+
await verify(mockedFoo.getBar()).timeout(10);
808+
} catch (e) {
809+
error = e;
810+
}
811+
812+
// then
813+
expect(error.message).toContain("to be called within");
814+
});
815+
816+
it("should not alter call expectations", async () => {
817+
// given
818+
let result: string;
819+
when(mockedFoo.getBar()).thenReturn("abc");
820+
setTimeout(() => result = foo.getBar(), 10);
821+
822+
// when
823+
await verify(mockedFoo.getBar()).timeout(10000);
824+
825+
// then
826+
expect(result).toEqual("abc");
827+
verify(mockedFoo.getBar()).once();
828+
});
829+
});
777830
});
778831

779832
function verifyCallCountErrorMessage(error, expectedCallCount, receivedCallCount): void {

0 commit comments

Comments
 (0)