Skip to content

Commit

Permalink
Adding support for verify(...).timeout(ms)
Browse files Browse the repository at this point in the history
  • Loading branch information
johanblumenberg committed Mar 29, 2018
1 parent 93d6625 commit d81029e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/MethodStubVerificator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {MethodToStub} from "./MethodToStub";
import {MethodCallToStringConverter} from "./utils/MethodCallToStringConverter";

// Save reference to setTimeout, in case tests are mocking time functions
const localSetTimeout = setTimeout;

export class MethodStubVerificator<T> {
private methodCallToStringConverter: MethodCallToStringConverter = new MethodCallToStringConverter();

Expand Down Expand Up @@ -91,4 +94,25 @@ export class MethodStubVerificator<T> {
throw new Error(`${errorBeginning}but none of them has been called.`);
}
}

public timeout(ms: number): Promise<void> {
return new Promise((resolve, reject) => {
const expired = Date.now() + ms;

const check = () => {
const allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);

if (allMatchingActions.length > 0) {
resolve();
} else if (Date.now() >= expired) {
const methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
reject(new Error(`Expected "${methodToVerifyAsString}to be called within ${ms} ms.`));
} else {
localSetTimeout(check, 1);
}
};

check();
});
}
}
55 changes: 54 additions & 1 deletion test/verification.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {instance, mock, verify} from "../src/ts-mockito";
import {instance, mock, verify, when} from "../src/ts-mockito";
import {MethodCallToStringConverter} from "../src/utils/MethodCallToStringConverter";
import {Bar} from "./utils/Bar";
import {Foo} from "./utils/Foo";
Expand Down Expand Up @@ -774,6 +774,59 @@ describe("verifying mocked object", () => {
});
});
});

describe("with timeout", () => {
it("should succeed if call already happend", async () => {
// given
foo.getBar();

// when
await verify(mockedFoo.getBar()).timeout(10000);

// then
verify(mockedFoo.getBar()).once();
});

it("should wait for call to happen", async () => {
// given
setTimeout(() => foo.getBar(), 10);

// when
await verify(mockedFoo.getBar()).timeout(10000);

// then
verify(mockedFoo.getBar()).once();
});

it("should fail if call does not happen", async () => {
// given

// when
let error;
try {
await verify(mockedFoo.getBar()).timeout(10);
} catch (e) {
error = e;
}

// then
expect(error.message).toContain("to be called within");
});

it("should not alter call expectations", async () => {
// given
let result: string;
when(mockedFoo.getBar()).thenReturn("abc");
setTimeout(() => result = foo.getBar(), 10);

// when
await verify(mockedFoo.getBar()).timeout(10000);

// then
expect(result).toEqual("abc");
verify(mockedFoo.getBar()).once();
});
});
});

function verifyCallCountErrorMessage(error, expectedCallCount, receivedCallCount): void {
Expand Down

0 comments on commit d81029e

Please sign in to comment.