riatsila
riatsila6mo ago

Testing with document Ids as a field

Trying out convex test and this code is resulting in an error, do I need to test create a record in the orgs table and then send that to the test mutation & match?
function generateMockID() {
return uuidv4().replace(/-/g, "");
}

test("sending messages", async () => {
const orgId = generateMockID() as Id<'orgs'>;
console.log(orgId);
const t = convexTest(schema);
await t.mutation(api.tasks.insert, {
task: {
name: "Task 1",
text: "This is a task",
slug:"TEST-001",
org_id: orgId,
}
});
await t.mutation(api.tasks.insert, {
task: {
name: "Task 2",
text: "This is another task",
slug:"TEST-002",
org_id: orgId,
}
});

const tasks = await t.query(api.tasks.getAll);
expect(tasks).toMatchObject([
{
name: "Task 1",
text: "This is a task",
slug:"TEST-001",
org_id: orgId,
} ,
{
name: "Task 2",
text: "This is another task",
slug:"TEST-002",
org_id: orgId,
}
]);
});
function generateMockID() {
return uuidv4().replace(/-/g, "");
}

test("sending messages", async () => {
const orgId = generateMockID() as Id<'orgs'>;
console.log(orgId);
const t = convexTest(schema);
await t.mutation(api.tasks.insert, {
task: {
name: "Task 1",
text: "This is a task",
slug:"TEST-001",
org_id: orgId,
}
});
await t.mutation(api.tasks.insert, {
task: {
name: "Task 2",
text: "This is another task",
slug:"TEST-002",
org_id: orgId,
}
});

const tasks = await t.query(api.tasks.getAll);
expect(tasks).toMatchObject([
{
name: "Task 1",
text: "This is a task",
slug:"TEST-001",
org_id: orgId,
} ,
{
name: "Task 2",
text: "This is another task",
slug:"TEST-002",
org_id: orgId,
}
]);
});
error:
FAIL convex/tests/tasks.test.ts > sending messages
Error: Validator error: Expected ID for table "orgs", got `af9c0e9660b94258873a98c0a1b5c198`
❯ validateValidator node_modules/convex-test/dist/index.js:682:23
681| return;
682| }
683| case "array": {
| ^
684| if (!Array.isArray(value)) {
685| throw new Error(`Validator error: Expected \`Array\`, got \`${value}\``);
FAIL convex/tests/tasks.test.ts > sending messages
Error: Validator error: Expected ID for table "orgs", got `af9c0e9660b94258873a98c0a1b5c198`
❯ validateValidator node_modules/convex-test/dist/index.js:682:23
681| return;
682| }
683| case "array": {
| ^
684| if (!Array.isArray(value)) {
685| throw new Error(`Validator error: Expected \`Array\`, got \`${value}\``);
2 Replies
sshader
sshader6mo ago
I'd create an org ID with db.insert. Note you can do something like this:
const orgId = t.run((ctx) => {
return ctx.db.insert("orgs", someTestData)
})
const orgId = t.run((ctx) => {
return ctx.db.insert("orgs", someTestData)
})
and don't have to write a new mutation just for testing. To share some of the details about the implementation here, validators like v.id("orgs") want to be able to distinguish between an ID in the "orgs" table and an ID in the "tasks" table. In tests, we make our IDs look like orgs;1 (or something like that), and do something fancier in production. But this is why generating a UUID isn't quite enough. We could potentially make something like t.allocateNewId("orgs") if it's useful, but hopefully creating a test entry in the orgs table isn't too bad?
riatsila
riatsilaOP6mo ago
Ah make sense, this approach works great — thanks!

Did you find this page helpful?