Files
triggerdotdev--trigger.dev/packages/testing
github-actions[bot] cfa3e24123 chore: Update version for release (#444)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2023-09-13 13:38:03 +01:00
..

Trigger.dev Testing Package

The testing package provides useful helpers to write your own tests in jest and vitest with complete type support.

Usage

  1. Install the package:
# npm
npm install -D @trigger.dev/testing

# yarn
yarn add -D @trigger.dev/testing

# pnpm
pnpm add -D @trigger.dev/testing
  1. Import the package in a test as follows:
import { toHaveSucceeded, createJobTester } from "@trigger.dev/testing";
import { expect, vi } from "vitest";

expect.extend({ toHaveSucceeded });
const testJob = createJobTester(vi);
  1. 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 Example for a working setup with Vitest.

License

MIT