Skip to content

Commit 10799ee

Browse files
Adding support for verify(...).timeout(ms)
1 parent c1c7f69 commit 10799ee

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

src/MethodStubVerificator.ts

+24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import {MethodToStub} from "./MethodToStub";
22
import {MethodCallToStringConverter} from "./utils/MethodCallToStringConverter";
33

4+
// Save reference to setTimeout, in case tests are mocking time functions
5+
const localSetTimeout = setTimeout;
6+
47
export class MethodStubVerificator<T> {
58
private methodCallToStringConverter: MethodCallToStringConverter = new MethodCallToStringConverter();
69

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

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)