@civitas-cerebrum/context-store

中文文档
0.0.2发布于 2026/03/14MITTypeScriptCommonJS零依赖构建来源已验证近 30 天下载 2782

A lightweight, strongly-typed, and framework-agnostic utility for managing test state and contextual data across test steps.

原始使用文档
文档目录6 节
  1. Installation
  2. Why use context-store?
  3. Quick Start
  4. API Reference
  5. Advanced: Playwright Fixture Integration
  6. License

@civitas-cerebrum/context-store

A lightweight, strongly-typed, and framework-agnostic utility for managing test state and contextual data across test steps.

When writing end-to-end (E2E) or integration tests, you often need to pass data generated in one step (like a dynamic ID, an extracted date, or an email address) to subsequent steps. context-store replaces messy plain JavaScript objects with a robust map-based API, providing safe type-casting, default values, and clean iteration.

Installation

npm i @civitas-cerebrum/context-store

Why use context-store?

  • Type Safety: Built-in TypeScript support ensures you know exactly what data you are retrieving.
  • Safe Parsing: Methods like getBoolean and getNumber gracefully handle missing or malformed data with reliable fallbacks.
  • Framework Agnostic: Works perfectly with Playwright, Cypress, WebdriverIO, Jest, Mocha, or Cucumber.
  • Clean Iteration: Easily loop through stored key-value pairs for bulk assertions.

Quick Start

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', '[email protected]');
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); // '[email protected]'

API Reference

MethodDescriptionExample
put(key, value)Associates a value with a specific key. Ignores null/undefined inputs.context.put('id', 123);
get<T>(key, default?)Retrieves a value, optionally falling back to a default if the key is missing.context.get<string>('name', 'N/A');
getBoolean(key, default?)Returns true if the stored value is a boolean true or the string "true".context.getBoolean('isActive', false);
getNumber(key, default?)Parses and returns the value as a number. Falls back to default if parsing fails.context.getNumber('price', 0);
has(key)Returns true if the store contains the specified key.context.has('token');
merge(...sources)Merges properties from records or other Maps into the current store.context.merge({ a: 1, b: 2 });
entries()Returns an array of [key, value] pairs, perfect for loops and assertions.for (const [k, v] of context.entries())
items()Returns a Set of all keys currently in the store.context.items();
remove(key)Deletes a specific entry from the store.context.remove('temporaryId');
clear()Wipes all data from the store.context.clear();

Advanced: Playwright Fixture Integration

Instead of instantiating ContextStore manually inside every single test, you can inject it automatically using Playwright Fixtures.

1. Define the fixture (fixtures.ts):

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. Use it in your tests (example.spec.ts):

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...
  });
});

License

MIT

版本与发布记录

@civitas-cerebrum/context-store 共发布 2 个版本,首次发布于 2026/03/13;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.0.2latest2026/03/147 个月前
0.0.12026/03/137 个月前

关于 @civitas-cerebrum/context-store 的常见问题

@civitas-cerebrum/context-store 是什么?

A lightweight, strongly-typed, and framework-agnostic utility for managing test state and contextual data across test steps.

如何安装 @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 和提交记录判断。