dyno-bot/dyno-levelsranks-master/__tests__/main.spec.ts
2020-09-12 19:08:48 +01:00

30 lines
812 B
TypeScript

import { Delays, greeter } from '../src/main';
describe('greeter function', () => {
// Read more about fake timers: http://facebook.github.io/jest/docs/en/timer-mocks.html#content
jest.useFakeTimers();
const name: string = 'John';
let hello: string;
// Act before assertions
beforeAll(async () => {
const p: Promise<string> = greeter(name);
jest.runOnlyPendingTimers();
hello = await p;
});
// Assert if setTimeout was called properly
it('delays the greeting by 2 seconds', () => {
expect((<jest.Mock<void>> setTimeout).mock.calls.length).toBe(1);
expect((<jest.Mock<void>> setTimeout).mock.calls[0][1]).toBe(Delays.Long);
});
// Assert greeter result
it('greets a user with `Hello, {name}` message', () => {
expect(hello).toBe(`Hello, ${name}`);
});
});