# @civitas-cerebrum/context-store 中文文档

规范地址：https://npmcn.com/package/%40civitas-cerebrum/context-store/

版本：0\.0\.2
npm 元数据更新时间：2026-03-13T16:09:18.337Z
中文译文更新时间：2026-11-09T22:45:40.109Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40civitas-cerebrum/context-store
原文对照：https://npmcn.com/package/%40civitas-cerebrum/context-store/?lang=original
项目主页：https://github.com/Umutayb/js-context-store#readme
源码仓库：https://github.com/Umutayb/js-context-store

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：10.1 kB，6 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2026-03-13T15:52:22.604Z
- 最新版本发布：2026-03-13T16:09:18.024Z
- 近 30 天下载量：2782（2026-10-10 至 2026-11-08，npm 官方下载统计）
- 已发布版本数：2（近 12 个月 2 个）

## 中文简介

用于管理测试状态和跨测试步骤的背景数据的轻量级、强型和框架不可知性用途.

## 安装

```sh
npm install @civitas-cerebrum/context-store
```

也可以使用：`pnpm add @civitas-cerebrum/context-store`、`yarn add @civitas-cerebrum/context-store`、`bun add @civitas-cerebrum/context-store`

## README

# @civitas-cerebrum/context-store (英语)

用于管理测试状态和跨测试步骤的背景数据的轻量级、强型和框架不可知性用途.

在写入端到端(E2E)或集成测试时,您往往需要将一步中生成的数据(如动态ID,被取出的日期,或电子邮件地址)传递给后续步骤. `context-store` 将杂乱的平版 JavaScript 对象替换为基于强烈地图的 API, 提供安全的类型播报, 默认值, 以及干净的迭代 .

## 安装

```bash
npm i @civitas-cerebrum/context-store
```

## 为什么使用上下文存储器?

-   **类型安全 :** 内建的 TypeScript 支持可确保您准确知道您正在检索的数据 .
-   **安全剖析 :** 方法一样 `getBoolean` 财务报告和审定财务报表 `getNumber` 优雅地处理缺失或有可靠倒计时的畸形数据.
-   **框架不可知性:** 与Playwright, Cypress, WebdriverIO, Jest, Mocha, 或 Cucumber 完美合作.
-   **清洁迭代 :** 方便地通过存储的密钥值对循环进行批量断言.

## 快速启动

```typescript
import { ContextStore } from '@civitas-cerebrum/context-store';

// Instantiate a new store for your test
const context = new ContextStore();

// Store data during a step
context.put('UserEmail', 'test@example.com');
context.put('IsSubscribed', true);
context.put('RetryCount', '3'); // Note: Stored as a string

// Retrieve data safely in later steps
const email = context.get('UserEmail');
const isSubscribed = context.getBoolean('IsSubscribed');
const retries = context.getNumber('RetryCount', 0); // Safely parsed to a number!

console.log(email); // 'test@example.com'

```

## API 参考

| 方法 | 说明 | 示例 |
| --- | --- | --- |
| **`put(key, value)`** | 与特定的钥匙相关联 忽略无效/ 未定义输入 . | `context.put('id', 123);` |
| **`get<T>(key, default?)`** | 获取一个值,如果缺少密钥,可选择返回默认值. | `context.get<string>('name', 'N/A');` |
| **`getBoolean(key, default?)`** | 回返 `true` 如果存储的值为布尔真值或字符串 `"true"`。 。 。 . | `context.getBoolean('isActive', false);` |
| **`getNumber(key, default?)`** | 解析并返回数值为数字。 如果解析失败, 则返回默认值 . | `context.getNumber('price', 0);` |
| **`has(key)`** | 回返 `true` 如果商店中包含指定的密钥. | `context.has('token');` |
| **`merge(...sources)`** | 将记录或其他地图中的属性合并到当前存储中. | `context.merge({ a: 1, b: 2 });` |
| **`entries()`** | 返回数组 `[key, value]` 配对,适合循环和断言. | `for (const [k, v] of context.entries())` |
| **`items()`** | 回返a `Set` 包含当前存储的所有密钥. | `context.items();` |
| **`remove(key)`** | 从商店中删除特定条目. | `context.remove('temporaryId');` |
| **`clear()`** | 清除商店的所有数据. | `context.clear();` |

## 高级: Playwright 修复集成

而不是即兴表演 `ContextStore` 每一次测试中,可以手动使用Playwright Fixtures自动注入.

**1\. 界定固定位置(`fixtures.ts`:**

```typescript
import { test as base } from '@playwright/test';
import { ContextStore } from '@civitas-cerebrum/context-store';

type MyFixtures = {
  context: ContextStore;
};

export const test = base.extend<MyFixtures>({
  context: async ({}, use) => {
    // Creates a fresh store for every test run
    const store = new ContextStore();
    await use(store);
  },
});
```

**2\. 在测试中使用(`example.spec.ts`:**

```typescript
import { test } from './fixtures';

test('My test using the context store', async ({ page, context }) => {
  await test.step('Generate Data', async () => {
    context.put('orderId', 'ORD-999');
  });

  await test.step('Verify Data', async () => {
    const orderId = context.get('orderId');
    // Proceed with assertions...
  });
});
```

## 许可证

麻省理工学院

## 常见问题

### @civitas-cerebrum/context-store 是什么？

用于管理测试状态和跨测试步骤的背景数据的轻量级、强型和框架不可知性用途\.

### 如何安装 @civitas-cerebrum/context-store？

在项目目录运行 npm install @civitas-cerebrum/context-store，或使用 pnpm add / yarn add / bun add。

### @civitas-cerebrum/context-store 支持 TypeScript 吗？

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

### @civitas-cerebrum/context-store 支持 ESM 还是 CommonJS？

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

### @civitas-cerebrum/context-store 可以商用吗？

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

### @civitas-cerebrum/context-store 的下载量有多少？

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

### @civitas-cerebrum/context-store 还在维护吗？

最新版本 0\.0\.2 发布于 2026/03/14（7 个月前）；至今共发布 2 个版本，其中过去 12 个月发布了 2 个；首次发布于 2026/03/13。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

