@castore/test-tools
v1.25.3
Published
Test tooling for the Castore library.
Downloads
679
Readme
Test tools
Test tooling for the Castore library.
📥 Installation
# npm
npm install --save-dev @castore/test-tools
# yarn
yarn add --dev @castore/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 InMemoryStorageAdapter
, 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/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 InMemoryStorageAdapter
matching the provided initial state.
import { EventStore } from '@castore/core';
import { muteEventStore } from '@castore/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);
});
});