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

babel-plugin-chai-assert-async

v0.1.0

Published

Transforms `assert.async.*` calls into `test-until` expressions that resolve when the assertion passes, preserving error messages for failed assertions.

Downloads

12

Readme

babel-plugin-chai-assert-async

Transforms assert.async.* calls into test-until expressions that resolve when the assertion passes, preserving error messages for failed assertions.

Build Status

Installation

Assuming you're already using Babel, install the Babel plugin and the peer dependency with your package manager of choice:

$ npm install babel-plugin-chai-assert-async test-until

Usage

Via .babelrc

.babelrc

{
  "plugins": ["chai-assert-async"]
}

Via CLI

$ babel --plugins chai-assert-async script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["chai-assert-async"]
});

Details

During tests, it's sometime's useful to test values that are set asynchronously, or to wait for a condition to be true before continuing the test. Here's a convoluted example:

function setValueAsync(obj) {
  setTimeout(() => obj.val = 42, 100)
}

describe('setValueAsync', function() {
  it('sets a value asynchronously', function() {
    const obj = {val: 0}
    setValueAsync(obj)
    assert.equal(obj.val, 42) // will fail
  })
})

This test will fail because obj.val does not equal 42 at the time the assertion is run.

We can use a library like test-until to ensure the test doesn't continue until obj.val does equal 42. Here's an example with Mocha, async/await

import until from 'test-until'
// ....
it('sets a value asynchronously', function() {
  const obj = {val: 0}
  setValueAsync(obj)
  await until(() => obj.val === 42)
})

However, in converting from the assert.equal version of this test to the await until version, we lost some information. In particular, it's more difficult to know exactly what we're testing, and if the until expression times out, we get a very generic error message like "timed out waiting for something to happen" instead of something useful like "expected obj.val to equal 42".

This transform allows you to convert something like assert.equal(obj.val, 42) into an equivalent until expression by writing assert.async.equal(obj.val, 42):

function setValueAsync(obj) {
  setTimeout(() => obj.val = 42, 100)
}

describe('setValueAsync', function() {
  it('sets a value asynchronously', async function() {
    const obj = {val: 0}
    setValueAsync(obj)
    await assert.async.equal(obj.val, 42)
  })
})

In this case, if the async assertion fails, the original error message like "expected obj.val to equal 42" will be preserved.

If you want to pass a timeout to test-until, you can specify it as an argument to async:

await assert.async(100).equal(obj.val, 42)

Examples

Basic

In

async function test() {
  await assert.async.equal(thing, other);
}

Out

"use strict";

import until from 'test-until';

async function test() {
  await until(async function (fail) {
    try {
      assert.equal(thing, other);
      return true;
    } catch (err) {
      return fail(err);
    }
  });
}

With explicit timeout

In

async function test() {
  await assert.async(500).equal(thing, other);
}

Out

"use strict";

import until from 'test-until';

async function test() {
  await until(async function (fail) {
    try {
      assert.equal(thing, other);
      return true;
    } catch (err) {
      return fail(err);
    }
  }, 500);
}