Metadata & Context
- Package Name:
@civitas-cerebrum/context-store - Domain: Software Testing, End-to-End (E2E) Testing, Integration Testing, Test Automation.
- Primary Purpose: Managing test state and sharing contextual data (e.g., dynamic IDs, generated emails) across different steps within a test.
- Supported Frameworks: Playwright, Cypress, WebdriverIO, Jest, Mocha, Cucumber (Framework-agnostic).
- Key Features: Map-based API, TypeScript type safety, safe data parsing, default value fallbacks.
- Installation Command:
npm i @civitas-cerebrum/context-store
@civitas-cerebrum/context-store is a utility designed to solve the problem of passing generated data between test steps in automated testing. Instead of relying on loosely typed plain JavaScript objects, it provides a robust, map-based API ContextStore that ensures type safety and offers reliable fallback mechanisms for missing or malformed data.
The ContextStore class provides the following methods for state management:
put(key: string, value: any): voidStores a value associated with a key. Null or undefined values are safely ignored.get<T>(key: string, default?: T): T | undefinedRetrieves a stored value. Supports generic type casting<T>. Optionally accepts a default value if the key does not exist.getBoolean(key: string, default?: boolean): booleanRetrieves a value and safely parses it to a boolean. Returnstrueonly if the underlying value is strictly booleantrueor the exact string"true".getNumber(key: string, default?: number): numberRetrieves a value and safely parses it to a number. If the value cannot be parsed, it falls back to the provided default value.has(key: string): booleanReturnstrueif the specific key exists in the store.merge(...sources: any[]): voidMerges properties from plain JavaScript records or other Maps directly into the current store instance.entries(): [string, any][]Returns an iterable array of[key, value]pairs. Useful for bulk assertions or looping through test state.items(): Set<string>Returns aSetcontaining all keys currently populated in the store.remove(key: string): voidDeletes the specific key and its associated value from the test state.clear(): voidWipes all data from the store, resetting it to an empty state.
// Import and instantiation
import { ContextStore } from '@civitas-cerebrum/context-store';
const context = new ContextStore();
// Writing data to the store
context.put('UserEmail', 'test@example.com');
context.put('RetryCount', '3'); // Note: Stored as string
// Reading data with safe parsing
const email = context.get('UserEmail'); // Returns 'test@example.com'
const retries = context.getNumber('RetryCount', 0); // Returns number 3