punn
punn2y ago

Unit testing/E2E testing

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).
6 Replies
ballingt
ballingt2y ago
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
punn
punnOP2y ago
Sweet sounds good thanks. Will give those a shot. Are there any code snippets you could share?
ballingt
ballingt2y ago
Been out all weekend, sorry for the delay! Here's a quick example: https://github.com/thomasballinger/convex-tutorial-test/compare/tests 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();
});
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();
});
punn
punnOP2y ago
Sweet thank you will take a look
Michal Srb
Michal Srb7mo ago
not using Vite so unfortunately not Vitest
You don't need to be using Vite to use Vitest.
punn
punnOP7mo ago
been trying out the new tests, working well so far!

Did you find this page helpful?