@triggery/testing
v0.10.0
Published
Testing utilities for Triggery — isolated runtime, fake scheduler, mock conditions/actions. Zero runtime dependencies, works with Vitest / Jest / node:test.
Downloads
465
Maintainers
Readme
@triggery/testing
Testing utilities for Triggery. Zero runtime dependencies. Framework-agnostic — works the same in React, Solid, Vue tests, Node, or a worker; works with Vitest, Jest, and node:test.
Install
pnpm add -D @triggery/testingWhat's in the box
createTestRuntime({ triggers? })— isolated runtime per test (no global state pollution).mockCondition(trigger, name, value | getter)— supply a condition without rendering a component.mockAction(trigger, name, fn)— register an action handler (typicallyvi.fn()/jest.fn()/ a closure).flushMicrotasks()— drain the default microtask scheduler before asserting.createFakeScheduler()— controllable virtual clock foractions.debounce / throttle / defer:install()/uninstall()swapglobalThis.setTimeout/clearTimeoutfor a controlled implementation.advance(ms)runs every timer due within the window and drains microtasks.flushAll()runs every pending timer regardless of scheduled time.- Test-runner agnostic — no dependency on
vi.useFakeTimers().
Example
import { createTrigger } from '@triggery/core';
import { createTestRuntime } from '@triggery/testing';
import { expect, test, vi } from 'vitest';
test('mod with sound shows toast and plays sound', async () => {
const rt = createTestRuntime();
const t = createTrigger<{
events: { 'new-message': string };
conditions: { user: { isMod: boolean } };
actions: { showToast: string; playSound: 'beep' | 'mod-alert' };
}>(
{
id: 'msg',
events: ['new-message'],
required: ['user'],
handler: ({ event, conditions, actions, check }) => {
if (!check.is('user', (u) => u.isMod)) return;
actions.showToast?.(event.payload);
actions.playSound?.('mod-alert');
},
},
rt,
);
rt.mockCondition(t, 'user', { isMod: true });
const showToast = vi.fn();
rt.mockAction(t, 'showToast', showToast);
rt.fireSync('new-message', 'hi');
expect(showToast).toHaveBeenCalledWith('hi');
});Documentation
Full documentation, recipes and API reference at https://triggeryjs.github.io/packages/testing/.
Related packages
@triggery/core— Required peer —createTestRuntimewrapscreateRuntime.
See the full package list in the repo README.
License
MIT © Aleksey Skhomenko
