Skip to content

Commit

Permalink
Clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
72636c committed Apr 10, 2022
1 parent e0f5df9 commit 19e89cd
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions src/utils/wait.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import * as wait from './wait';

const delayMicrotask = () =>
Promise.resolve()
.then(() => undefined)
.then(() => undefined);

const sleep = jest.spyOn(wait, 'sleep');

beforeEach(jest.resetAllMocks);

describe('throwOnTimeout', () => {
const withMicrotaskDelay = () =>
Promise.resolve()
.then(() => undefined)
.then(() => undefined);

it('propagates a fulfilled promise within the timeout', async () => {
sleep.mockImplementation(withMicrotaskDelay);
sleep.mockImplementation(delayMicrotask);

const value = 123;
const promise = jest.fn().mockResolvedValue(value);
Expand All @@ -23,7 +23,7 @@ describe('throwOnTimeout', () => {
});

it('propagates a rejected promise within the timeout', async () => {
sleep.mockImplementation(withMicrotaskDelay);
sleep.mockImplementation(delayMicrotask);

const err = new Error('Badness!');

Expand All @@ -38,7 +38,7 @@ describe('throwOnTimeout', () => {
it('enforces the timeout', async () => {
sleep.mockResolvedValue();

const promise = jest.fn().mockImplementation(withMicrotaskDelay);
const promise = jest.fn().mockImplementation(delayMicrotask);

await expect(
wait.throwOnTimeout(promise(), { s: 1 }),
Expand All @@ -50,13 +50,8 @@ describe('throwOnTimeout', () => {
});

describe('withTimeout', () => {
const withMicrotaskDelay = () =>
Promise.resolve()
.then(() => undefined)
.then(() => undefined);

it('propagates a static value for easier `jest.mock`ing', async () => {
sleep.mockImplementation(withMicrotaskDelay);
sleep.mockImplementation(delayMicrotask);

const value = 123;

Expand All @@ -70,7 +65,7 @@ describe('withTimeout', () => {
});

it('propagates a fulfilled promise within the timeout', async () => {
sleep.mockImplementation(withMicrotaskDelay);
sleep.mockImplementation(delayMicrotask);

const value = 123;
const promise = jest.fn().mockResolvedValue(value);
Expand All @@ -85,7 +80,7 @@ describe('withTimeout', () => {
});

it('propagates a rejected promise within the timeout', async () => {
sleep.mockImplementation(withMicrotaskDelay);
sleep.mockImplementation(delayMicrotask);

const err = new Error('Badness!');

Expand All @@ -100,7 +95,7 @@ describe('withTimeout', () => {
it('enforces the timeout', async () => {
sleep.mockResolvedValue();

const promise = jest.fn().mockImplementation(withMicrotaskDelay);
const promise = jest.fn().mockImplementation(delayMicrotask);

await expect(wait.withTimeout(promise(), { s: 3 })).resolves.toStrictEqual({
ok: false,
Expand Down

0 comments on commit 19e89cd

Please sign in to comment.