Files
triggerdotdev--trigger.dev/packages/testing/README.md
nicktrn 6de8bd470b Add @trigger.dev/testing package (#432)
* feat: add testing package

* feat: add unit testing example

* Delete examples/unit-testing/dummy-integration/tasks.ts

* Delete examples/unit-testing/dummy-integration/types.ts

* Update DummyIntegration to new format

* Update testing package to new integration format

* Update readme to new task spy format

* Create rotten-meals-cough.md

---------

Co-authored-by: Matt Aitken <matt@mattaitken.com>
2023-09-11 13:38:05 +01:00

1.7 KiB

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