# @castore/lib-test-tools

> Test tooling for the Castore library.

Latest version **2.4.2** (published 2025-04-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @castore/lib-test-tools
pnpm add @castore/lib-test-tools
yarn add @castore/lib-test-tools
bun add @castore/lib-test-tools
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 2.4.2 |
| Published | 2025-04-18 |
| First published | 2023-10-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 117.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 276 |
| Maintainers | julietteff, charlesgery, valentinbeggi, thomasaribart |
| Keywords | event, source, store, typescript |

## Links

- npm: https://www.npmjs.com/package/@castore/lib-test-tools
- Repository: https://github.com/castore-dev/castore
- Homepage: https://github.com/castore-dev/castore#readme
- Issues: https://github.com/castore-dev/castore/issues
- npm.io page: https://npm.io/package/@castore/lib-test-tools

## Dependencies (1)

- [@castore/event-storage-adapter-in-memory](https://npm.io/package/@castore/event-storage-adapter-in-memory.md) 2.4.2

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 2.4.2 (latest) — 2025-04-18
- 2.4.1 — 2025-02-25
- 2.4.0 — 2025-02-18
- 2.3.1 — 2024-04-25
- 2.3.0 — 2024-03-20
- 2.2.0 — 2024-01-16
- 2.1.0 — 2023-11-03
- 2.0.0 — 2023-10-27

## README

# Test tools

Test tooling for the [Castore](https://github.com/castore-dev/castore) library.

## 📥 Installation

```bash
# npm
npm install --save-dev @castore/lib-test-tools

# yarn
yarn add --dev @castore/lib-test-tools
```

This package has `@castore/core` as peer dependency, so you will have to install it as well:

```bash
# npm
npm install @castore/core

# yarn
yarn add @castore/core
```

## 👩‍💻 Usage

### MockEventStore

The `mockEventStore` util returns a copy of the provided `EventStore` connected to an [`InMemoryEventStorageAdapter`](https://github.com/castore-dev/castore/tree/main/packages/event-storage-adapter-in-memory), empty or with a given initial state. It follows the `EventStore` interface but adds a `reset` method to reset it to the provided initial state. The original event store is not muted.

```ts
import { EventStore } from '@castore/core';
import { mockEventStore } from '@castore/lib-test-tools';

const pokemonsEventStore = new EventStore({
  ...
});

const pokemonAppearCommand = new Command({
  ...
});

describe('My awesome test', () => {
  const mockedPokemonsEventStore = mockEventStore(pokemonsEventStore, [
    // 👇 Provide initial state (list of event details) in a type-safe way
    {
      aggregateId: '123',
      version: 1,
      type: 'POKEMON_APPEARED',
      ...
    },
  ]);

  beforeEach(() => {
    // 👇 Reset to initial state
    mockedPokemonsEventStore.reset();
  });

  it('pushes a POKEMON_APPEARED event', async () => {
    const { pokemonId } = await pokemonAppearCommand.handler({
      requiredEventsStores: [mockedPokemonsEventStore],
      ...
    });

    const { events } = await mockedPokemonsEventStore.getEvents(pokemonId);

    expect(events).toHaveLength(1);
  });
});
```

### MuteEventStore

Unlike `mockEventStore`, the `muteEventStore` util mutes the original event store and replace its storage adapter with an `InMemoryEventStorageAdapter` matching the provided initial state.

```ts
import { EventStore } from '@castore/core';
import { muteEventStore } from '@castore/lib-test-tools';

const pokemonsEventStore = new EventStore({
  ...
});

const functionUsingTheEventStore = async () => {
  ...
};

describe('My awesome test', () => {
  muteEventStore(pokemonsEventStore, [
    // 👇 Provide initial state (list of event details) in a type-safe way
    {
      aggregateId: '123',
      version: 1,
      type: 'POKEMON_APPEARED',
      ...
    },
  ]);

  it('does something incredible', async () => {
    await functionUsingTheEventStore();

    // 👇 Use the original event store
    const { events } = await pokemonsEventStore.getEvents('123');

    expect(events).toHaveLength(1);
  });
});
```

---
_Source: https://npm.io/package/@castore/lib-test-tools · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
