-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add test coverage #9
base: main
Are you sure you want to change the base?
Changes from all commits
5ddf670
12ba4e2
ef73393
19c5c4a
ac80825
c445053
f048c62
1679e3a
5c5c0ca
4d7cda8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Run tests | ||
on: | ||
push: | ||
branches: ["main"] | ||
pull_request: | ||
branches: ["main"] | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Use Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: "18.x" | ||
cache: "npm" | ||
- run: npm i | ||
- run: npm ci | ||
- run: cd example && npm i && cd .. | ||
- run: npm test |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/// <reference types="vite/client" /> | ||
|
||
import { convexTest } from "convex-test"; | ||
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; | ||
import { Migrations } from "@convex-dev/migrations"; | ||
import { DataModel } from "./_generated/dataModel"; | ||
import schema from "./schema"; | ||
import componentSchema from "../../src/component/schema"; | ||
import migrationsSchema from "../node_modules/@convex-dev/migrations/src/component/schema"; | ||
import { api, components, internal } from "./_generated/api"; | ||
|
||
const migrationsModules = import.meta.glob( | ||
"../node_modules/@convex-dev/migrations/src/component/**/*.ts" | ||
); | ||
|
||
const modules = import.meta.glob("./**/*.ts"); | ||
const componentModules = import.meta.glob("../../src/component/**/*.ts"); | ||
|
||
describe("migrations client", () => { | ||
async function setupTest() { | ||
const t = convexTest(schema, modules); | ||
t.registerComponent("migrations", migrationsSchema, migrationsModules); | ||
await t.mutation(internal.example.seed, { count: 10 }); | ||
return t; | ||
} | ||
|
||
let testEnv: { | ||
t: ReturnType<typeof convexTest>; | ||
migrations: Migrations<DataModel>; | ||
testMigration: ReturnType<typeof Migrations.prototype.define>; | ||
}; | ||
|
||
beforeEach(async () => { | ||
vi.useFakeTimers(); | ||
const t = await setupTest(); | ||
const migrations = new Migrations<DataModel>(components.migrations); | ||
const testMigration = migrations.define({ | ||
table: "myTable", | ||
migrateOne: async (ctx: { db: any }, doc: { optionalField?: string }) => ({ optionalField: "default" }) | ||
}); | ||
testEnv = { t, migrations, testMigration }; | ||
}); | ||
|
||
afterEach(async () => { | ||
await testEnv.t.finishAllScheduledFunctions(vi.runAllTimers); | ||
vi.useRealTimers(); | ||
}); | ||
|
||
test("run basic migration", async () => { | ||
const { t, migrations, testMigration } = testEnv; | ||
|
||
await migrations.runOne(t, testMigration); | ||
Check failure on line 52 in example/convex/client.test.ts
|
||
|
||
const docs = await t.query("myTable").collect(); | ||
for (const doc of docs) { | ||
expect(doc.optionalField).toBe("default"); | ||
} | ||
}); | ||
|
||
test("run validation migration", async () => { | ||
const { t, migrations } = testEnv; | ||
|
||
const validateMigration = migrations.define({ | ||
table: "myTable", | ||
customRange: (query) => | ||
query.withIndex("by_requiredField", (q) => q.eq("requiredField", "")), | ||
migrateOne: async (ctx, doc: { requiredField?: string }) => { | ||
return { requiredField: "<unknown>" }; | ||
}, | ||
}); | ||
|
||
await migrations.runOne(t, validateMigration); | ||
Check failure on line 72 in example/convex/client.test.ts
|
||
|
||
const docs = await t.query("myTable") | ||
.withIndex("by_requiredField", (q) => q.eq("requiredField", "")) | ||
.collect(); | ||
expect(docs).toHaveLength(0); | ||
}); | ||
|
||
test("run type conversion migration", async () => { | ||
const { t, migrations } = testEnv; | ||
|
||
const convertMigration = migrations.define({ | ||
table: "myTable", | ||
migrateOne: async (ctx: { db: any }, doc: { _id: any; unionField: string | number }) => { | ||
if (typeof doc.unionField === "number") { | ||
await ctx.db.patch(doc._id, { unionField: doc.unionField.toString() }); | ||
} | ||
}, | ||
}); | ||
|
||
await migrations.runOne(t, convertMigration); | ||
Check failure on line 92 in example/convex/client.test.ts
|
||
|
||
const docs = await t.query("myTable").collect(); | ||
for (const doc of docs) { | ||
expect(typeof doc.unionField).toBe("string"); | ||
} | ||
}); | ||
|
||
test("run batch migration with parallelize", async () => { | ||
const { t, migrations } = testEnv; | ||
|
||
const batchMigration = migrations.define({ | ||
table: "myTable", | ||
batchSize: 2, | ||
parallelize: true, | ||
migrateOne: async (ctx, doc: { processed?: boolean }) => { | ||
return { processed: true }; | ||
}, | ||
}); | ||
|
||
await migrations.runOne(t, batchMigration); | ||
Check failure on line 112 in example/convex/client.test.ts
|
||
|
||
const docs = await t.query("myTable").collect(); | ||
for (const doc of docs) { | ||
expect(doc.processed).toBe(true); | ||
} | ||
}); | ||
|
||
test("handle failing migration", async () => { | ||
const { t, migrations } = testEnv; | ||
|
||
const failingMigration = migrations.define({ | ||
table: "myTable", | ||
migrateOne: async (_ctx, _doc) => { | ||
throw new Error("Migration failed"); | ||
}, | ||
}); | ||
|
||
await expect(migrations.runOne(t, failingMigration)).rejects.toThrow("Migration failed"); | ||
}); | ||
|
||
test("track migration state", async () => { | ||
const { t, migrations } = testEnv; | ||
|
||
const stateMigration = migrations.define({ | ||
table: "myTable", | ||
migrateOne: async (ctx, doc: { state?: string }) => { | ||
return { state: "migrated" }; | ||
}, | ||
}); | ||
|
||
const migration = migrations.runOne(t, stateMigration); | ||
Check failure on line 143 in example/convex/client.test.ts
|
||
|
||
// Check initial state | ||
let status = await t.query(api.lib.getStatus, { names: ["stateMigration"] }); | ||
Check failure on line 146 in example/convex/client.test.ts
|
||
expect(status[0].state).toBe("inProgress"); | ||
|
||
await migration; | ||
|
||
// Check final state | ||
status = await t.query(api.lib.getStatus, { names: ["stateMigration"] }); | ||
expect(status[0]).toMatchObject({ | ||
isDone: true, | ||
state: "success", | ||
processed: expect.any(Number), | ||
latestStart: expect.any(Number), | ||
latestEnd: expect.any(Number) | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you'll need to define the migration as a public export of this file and provide a reference to it like here with
testApi.foo
: https://github.com/get-convex/convex-helpers/blob/main/packages/convex-helpers/server/customFunctions.test.ts#L360-L381