Skip to content

Commit 7c9b55a

Browse files
committed
add example test
1 parent 3ff7dc4 commit 7c9b55a

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

example/convex/example.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
2+
import { initConvexTest } from "./setup.test";
3+
import { internal } from "./_generated/api";
4+
import { migrations } from "./example";
5+
6+
describe("example", () => {
7+
beforeEach(async () => {
8+
vi.useFakeTimers();
9+
});
10+
11+
afterEach(async () => {
12+
vi.useRealTimers();
13+
});
14+
15+
test("setDefaultValue", async () => {
16+
const t = initConvexTest();
17+
await t.mutation(internal.example.seed, { count: 10 });
18+
await t.run(async (ctx) => {
19+
const docs = await ctx.db.query("myTable").collect();
20+
expect(docs).toHaveLength(10);
21+
expect(docs.some((doc) => doc.optionalField === undefined)).toBe(true);
22+
await migrations.runOne(ctx, internal.example.setDefaultValue, {
23+
batchSize: 2,
24+
dryRun: false,
25+
});
26+
});
27+
await t.finishAllScheduledFunctions(vi.runAllTimers);
28+
await t.run(async (ctx) => {
29+
const after = await ctx.db.query("myTable").collect();
30+
expect(after).toHaveLength(10);
31+
expect(after.every((doc) => doc.optionalField !== undefined)).toBe(true);
32+
});
33+
});
34+
});

example/convex/setup.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference types="vite/client" />
2+
import { test } from "vitest";
3+
import { convexTest } from "convex-test";
4+
import schema from "./schema.js";
5+
import component from "@convex-dev/migrations/test";
6+
7+
const modules = import.meta.glob("./**/*.*s");
8+
// When users want to write tests that use your component, they need to
9+
// explicitly register it with its schema and modules.
10+
export function initConvexTest() {
11+
const t = convexTest(schema, modules);
12+
component.register(t);
13+
return t;
14+
}
15+
16+
test("setup", () => {});

0 commit comments

Comments
 (0)