#Unit testing/E2E testing

10 messages · Page 1 of 1 (latest)

alpine crane
#

Pulse check:
How are people generally writing unit/e2e tests for convex actions?

I've been exploring a few options and wanted to browse other approaches (not using Vite so unfortunately not Vitest).

gloomy spade
#

For unit tests, Node.js actions can be run locally in Node.js — so just importing them and running them with a mocked context as the first argument (e.g. jest.fn() for runMutation, scheduling, etc.) is a path I've used some.

non-"use node"; actions are also often runnable locally, I've tested these in Node.js too, but there are cases where the difference in environment could matter, similar to testing browser code in Node.js.

#

re Vitest you don't need to be using Vite to use it, but Jest should be fine too

alpine crane
#

Sweet sounds good thanks. Will give those a shot.

alpine crane
gloomy spade
#

That's a mutation, let me add an action real quick

#

Just updated, the interesting bit is

import { expect, it, vi } from "vitest";
import { sendMeatLorumIpsum } from "./sendMessage";

it("sendMeatLorumIpsum should should insert a long message", async () => {
  const ctx = { runMutation: vi.fn() };
  await sendMeatLorumIpsum(ctx);
  expect(ctx.runMutation.mock.calls.length === 1);
  expect(ctx.runMutation.mock.calls[0][0]).toEqual("sendMessage");
  expect(ctx.runMutation.mock.calls[0][1].author).toEqual("baconipsum.com");
  expect(ctx.runMutation.mock.calls[0][1].body.length > 100).toBeTruthy();
});
alpine crane
#

Sweet thank you will take a look

slate tapir
#

not using Vite so unfortunately not Vitest

You don't need to be using Vite to use Vitest.