Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@castore/lib-test-tools

Package Overview
Dependencies
Maintainers
4
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@castore/lib-test-tools

Test tooling for the Castore library.

latest
Source
npmnpm
Version
2.4.2
Version published
Maintainers
4
Created
Source

Test tools

Test tooling for the Castore library.

📥 Installation

# 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:

# 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, 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.

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.

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

Keywords

event

FAQs

Package last updated on 18 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts