1. flush-promises
Flush promises in tests
flush-promises
Package: flush-promises
Created by: kentor
Last modified: Sat, 18 Jun 2022 02:27:25 GMT
Version: 1.0.2
License: MIT
Downloads: 1,118,233
Repository: https://github.com/kentor/flush-promises

Install

npm install flush-promises
yarn add flush-promises

flush-promises

Build Status npm

Flush all pending resolved promise handlers. Useful in tests.

example with async/await

 const flushPromises = require('flush-promises');

test('flushPromises', async () => {
  let a;
  let b;

  Promise.resolve().then(() => {
    a = 1;
  }).then(() => {
    b = 2;
  })

  await flushPromises();

  expect(a).toBe(1);
  expect(b).toBe(2);
});

TypeScript

 import * as flushPromises from "flush-promises";

test("flushPromises", async () => {
  let a;
  let b;
 
  Promise.resolve().then(() => {
    a = 1;
  }).then(() => {
    b = 2;
  });
 
  await flushPromises();
 
  expect(a).toBe(1);
  expect(b).toBe(2);
});

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code