@atom/babel-plugin-chai-assert-async

中文文档
1.0.0发布于 2019/02/26MITCommonJS近 30 天下载 2094

await assert.async()

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. Via .babelrc
  4. Via CLI
  5. Via Node API
  6. Details
  7. Examples

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 @atom/babel-plugin-chai-assert-async test-until

Usage

Via .babelrc

.babelrc

{
  "plugins": ["@atom/babel-plugin-chai-assert-async"]
}

Via CLI

$ babel --plugins @atom/babel-plugin-chai-assert-async script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["@atom/babel-plugin-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);
}

版本与发布记录

@atom/babel-plugin-chai-assert-async 共发布 2 个版本,首次发布于 2019/02/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2019/02/267 年前
1.0.0-02019/02/267 年前

关于 @atom/babel-plugin-chai-assert-async 的常见问题

@atom/babel-plugin-chai-assert-async 是什么?

await assert.async()

如何安装 @atom/babel-plugin-chai-assert-async?

在项目目录运行 npm install @atom/babel-plugin-chai-assert-async,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@babel/core(7.x)、test-until(1.x)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@atom/babel-plugin-chai-assert-async 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/atom__babel-plugin-chai-assert-async 是否存在。

@atom/babel-plugin-chai-assert-async 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@atom/babel-plugin-chai-assert-async 可以商用吗?

@atom/babel-plugin-chai-assert-async 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@atom/babel-plugin-chai-assert-async 的下载量有多少?

根据 npm 官方下载统计,@atom/babel-plugin-chai-assert-async 在 2026/10/07 – 2026/11/05 这 30 天内被下载 2,094 次,日均约 70 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@atom/babel-plugin-chai-assert-async 还在维护吗?

最新版本 1.0.0 发布于 2019/02/26(7 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/02/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。