@systemfsoftware/trigger.dev_testing
v2.3.19-beta.0
Published
A collection of useful tools to write tests for Trigger.dev.
Downloads
7
Readme
Trigger.dev Testing Package
The testing package provides useful helpers to write your own tests in jest and vitest with complete type support.
Usage
- Install the package:
# npm
npm install -D @systemfsoftware/trigger.dev_testing
# yarn
yarn add -D @systemfsoftware/trigger.dev_testing
# pnpm
pnpm add -D @systemfsoftware/trigger.dev_testing
- Import the package in a test as follows:
import { toHaveSucceeded, createJobTester } from "@systemfsoftware/trigger.dev_testing";
import { expect, vi } from "vitest";
expect.extend({ toHaveSucceeded });
const testJob = createJobTester(vi);
- You can then use it like this:
const jobToTest = client.defineJob({
id: "test-job",
name: "Test Job",
version: "0.1.0",
trigger: eventTrigger({
name: "test.trigger",
}),
integrations: {
dummy,
},
run: async (payload, io, ctx) => {
return await io.dummy.doSomething("test-task", {
foo: payload.foo,
});
},
});
const testRun = await testJob(jobToTest, {
payload: {
foo: "bar",
},
tasks: {
"test-task": {
bar: "baz",
},
},
});
// job run was successful
expect(testRun).toHaveSucceeded();
// task was called exactly once
expect(testRun.tasks["test-task"]).toHaveBeenCalledOnce();
// task was called with correct params
expect(testRun.tasks["test-task"]).toHaveBeenCalledWith({ foo: "bar" });
// mocked task output was correctly returned
expect(testRun.tasks["test-task"]).toHaveReturnedWith({ bar: "baz" });
// job run has expected output
expect(testRun.output).toEqual({ bar: "baz" });
More information
See the official Trigger.dev Unit Testing Reference for a working setup with Vitest.
License
MIT