@ava/cooperate

中文文档
1.0.0发布于 2021/11/04MITTypeScriptES Module近 30 天下载 4995

Plugin to enable cooperation between AVA test files

原始使用文档
文档目录4 节
  1. Usage
  2. Locks
  3. Reservations
  4. Semaphores

@ava/cooperate

AVA 4 plugin to enable cooperation between test files.

Install this as a development dependency alongside AVA itself:

npm install --save-dev @ava/cooperate

Usage

Cooperation takes place within a shared context:

import {SharedContext} from '@ava/cooperate';

const context = new SharedContext('my-context');

Across all test files, contexts with the same ID (here: my-context) are shared.

Locks

You can create a lock within a context:

const lock = context.createLock('my-lock');

A lock needs to be acquired. This is asynchronous:

const release = await lock.acquire();

Release the lock when you no longer need it:

release();

Locks are released automatically once your tests are done.

Use acquireNow() to either acquire the lock, or fail:

const release = await lock.acquireNow();

If the lock cannot be acquired this will throw with a LockAcquisitionError:

try {
  await lock.acquireNow();
} catch (error) {
  // error instanceof LockAcquisitionError
  // error.name === 'LockAcquisitionError'
  // error.lockId === 'my-lock'
}

Reservations

You can reserve primitive values like big integers, numbers and strings. Once reserved, no other test file can reserve these same values (if they use the correct shared context). Reserved values are released when your tests are done.

const reserved = await context.reserve(1, 2, 3);
// `reserved` will be an array containing those values that could be reserved.
// It could be empty.

Semaphores

You can create a counting semaphore within a shared context:

const initialValue = 3; // Must be a non-negative integer.
const semaphore = context.createSemaphore('my-semaphore', initialValue);

Within the same context, semaphores with the same ID must be created with the same initial value. Semaphores created with a different value are unusable. Their methods will reject with a SemaphoreCreationError.

Semaphores have two methods: acquire() and acquireNow(). Use acquire() to decrement the semaphore's value. If the semaphore's value would become negative, instead acquire() waits until the semaphore's value is high enough.

const semaphore = context.createSemaphore('my-semaphore', 3);
const release = await semaphore.acquire();

acquire() returns a function, release(), which increments the semaphore's value by the same amount as was acquired.

The semaphore is managed: if you don't call release(), it'll be run automatically when the test worker exits. Any pending acquire() calls will also be removed from the queue at this time.

acquireNow() works like acquire(), except that if the semaphore can't be decremented immediately, acquireNow() rejects with a SemaphoreDownError rather than wait.

Semaphores are weighted. acquire() and acquireNow() accept a non-negative integer amount, defaulting to 1, by which to decrement or increment the value:

await semaphore.acquire(0);
await semaphore.acquireNow(2);

You can also pass an amount to release() to release just part of the acquisition at a time:

const release = await semaphore.acquire(3); // Decrements the semaphore by 3
release(1); // Increments the semaphore by 1
release(); // Increments the semaphore by the remaining 2

acquire() calls resolve in FIFO order. If the current value is 1, and a call tries to acquire 2, subsequent acquire() calls have to wait, even if they want to acquire just 1.

acquireNow() skips the queue and decrements immediately if possible.

Lower-level, unmanaged semaphores

You can create a lower-level, unmanaged semaphore which doesn't have any auto-release behavior. Instead you need to increment the semaphore in code.

const initialValue = 3; // Must be a non-negative integer.
const semaphore = context.createUnmanagedSemaphore('my-semaphore', initialValue);

Unmanaged semaphores mustn't use the same ID as a managed semaphore, within the same context. Semaphores with the same ID must be created with the same initial value. Mismatched managed and unmanaged semaphores, or those created with different values are unusable. Their methods will reject with a SemaphoreCreationError.

Unmanaged semaphores have three methods. down() and downNow() decrement the value and up() increments:

await semaphore.down(0);
await semaphore.downNow(2);
await semaphore.up(); // `amount` defaults to 1

Like the acquire() and acquireNow() methods of managed semaphores, down() waits for the semaphore's value to be at least the requested amount, while downNow() rejects with SemaphoreDownError if the value cannot be decremented immediately.

These unmanaged semaphores do not release the "acquired" amount when a test worker exits.

版本与发布记录

@ava/cooperate 共发布 4 个版本,首次发布于 2020/09/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2021/11/045 年前
0.2.02021/06/205 年前
0.1.12020/10/186 年前
0.1.02020/09/276 年前

关于 @ava/cooperate 的常见问题

@ava/cooperate 是什么?

Plugin to enable cooperation between AVA test files

如何安装 @ava/cooperate?

在项目目录运行 npm install @ava/cooperate,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:ava(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ava/cooperate 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ava/cooperate 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ava/cooperate 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12.22 <13 || >=14.17 <15 || >=16.4 <17 || >=17。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ava/cooperate 可以商用吗?

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

@ava/cooperate 的下载量有多少?

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

@ava/cooperate 还在维护吗?

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