npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

qunit-wait-for

v2.0.2

Published

Wait for a QUnit assertion

Downloads

1,814

Readme

qunit-wait-for

Verify

Wait for a QUnit Assertion

Installation

Install the dependency:

yarn add -D qunit-wait-for

and then add the following in your JavaScript code:

import QUnit from "qunit";
import { installWaitFor } from "qunit-wait-for";

installWaitFor(QUnit);

If you're using Ember, the right place for that snippet is your tests/test-helper.js.

What does it do?

A common pattern in UI testing is the idea of needing to wait for some condition to be met before moving on in your tests. Normally we do some setup, interact with our application, wait for some event to take place, and then perform our assertions. For example, you might see something like this in an Ember integration test:

// Start with some modal rendered
await render(hbs`<ModalDialog />`);

// Close the modal, which might need time to animate off-screen
await click("[data-test-close-button]");

// Wait for the modal to actually be gone
await waitUntil(() => findAll("[data-test-modal-element]").length === 0);

// Actually _assert_ that the modal is gone
assert.dom("[data-test-modal-element").doesNotExist();

Many testing libraries provide helper functions to wait for your tests to catch up to the desired state:

  • In Ember, there are many test helpers that serve this purpose; waitUntil, settled, and await-ing the promise returned from other test helpers all serve this purpose
  • In React, the async utilities from @testing-library/dom provide this functionality

However, needing to know when -- and how -- to correctly wait for the UI under test to reach the right state adds complexity to your tests and can couple them tightly to the underlying implementation of the code being tested.

There's another approach that you can take that's much cleaner; rather than waiting for your tests to reach some state and then asserting against it, you can let your assertions run immediately and gracefully handle an initial failure. Then, try the assertion again, over and over until it passes (or a timeout is reached). I call this pattern "convergence testing" based on work from The Frontside on BigTest.js. The result is a test that both correctly waits for asynchronous operations to complete and is decoupled from specific logic around how to wait for the right state.

With qunit-wait-for, the example above can be simplified to this:

await render(hbs`<ModalDialog />`);

await click("[data-test-close-button]");

await assert.waitFor(() => {
  assert.dom("[data-test-modal-element").doesNotExist();
});

Usage

To use it, pass a callback to assert.waitFor and within it place your normal assertion:

await assert.waitFor(() => {
  assert.dom("[data-test-my-element]").exists();
});

The resulting promise resolves when either the condition is met or the timeout is reached. This promise should always be await-ed so ensure one of those two things has happened before moving on.

You can also provide an override for the amount of time to wait for the assertion to pass, if needed. By default it will wait for up to 1000ms (1 second):

// Wait for up to 2 seconds instead of 1
await assert.waitFor(
  () => {
    assert.dom("[data-test-my-element]").exists();
  },
  { timeout: 2000 }
);

Prior Art