Test tools
Test tooling for the Castore library.
📥 Installation
npm install --save-dev @castore/lib-test-tools
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 install @castore/core
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, [
{
aggregateId: '123',
version: 1,
type: 'POKEMON_APPEARED',
...
},
]);
beforeEach(() => {
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, [
{
aggregateId: '123',
version: 1,
type: 'POKEMON_APPEARED',
...
},
]);
it('does something incredible', async () => {
await functionUsingTheEventStore();
const { events } = await pokemonsEventStore.getEvents('123');
expect(events).toHaveLength(1);
});
});