Yvens
Yvens9mo ago

Convex-test/ents

Is there a way to use ctx.table in a test ?
await t.run(async (ctx) => {
// roles created
ctx.table("users").insert({ firstname, roles: [roleId1, roleId2] })
});
await t.run(async (ctx) => {
// roles created
ctx.table("users").insert({ firstname, roles: [roleId1, roleId2] })
});
Or should I insert data in the "users_to_roles" table created by ents ?
const t = convexTest(schema);
await t.run(async (ctx) => {
// roles created
// users created
ctx.db.insert("users_to_roles", { userId, roleId })
});
const t = convexTest(schema);
await t.run(async (ctx) => {
// roles created
// users created
ctx.db.insert("users_to_roles", { userId, roleId })
});
I don't get how to re-create the edge in my db setup. Thank you
33 Replies
Michal Srb
Michal Srb9mo ago
One quick workaround is to not use t.run, instead write an internal mutation or action perform what you need. I'll share how to use convex-test and convex-ents together in a bit.
Yvens
YvensOP9mo ago
Hello @Michal Srb, yes I did just as you suggested after thinking for a bit. Well as of now if it is the preferred way of doing things with ents, I don't have any more questions. Thank you
ampp
ampp9mo ago
We are trying to implement tests on ents as well, its just funny we tried to do it today too.
Chuck_Williams
Chuck_Williams9mo ago
I'm new here & working w/ @ampp on our project as co-developer.
Yvens
YvensOP9mo ago
@ampp did you guys managed to make it work for you ?
ampp
ampp9mo ago
we just deferred it, because it seems to get hung up on a the ent schema
Yvens
YvensOP9mo ago
If you can give me a reproduction MAYBE I can check what’s wrong, as for me I only have a problem with the type definition of the schema (ents types I think ) that I passed to the convexTest function but everything else is working fine
ampp
ampp9mo ago
so its not getting solved by calling a internal mutation? we just moved onto other problems till we can get best practices idea. import { convexTest } from "convex-test"; import { expect, test } from "vitest"; import { api } from "./_generated/api"; import schema from "./schema"; test("test assertion : Hello world... ", async () => { const t = convexTest(schema); // await t.mutation(api.messages.send, { body: "Hi!", author: "Sarah" }); // await t.mutation(api.messages.send, { body: "Hey!", author: "Tom" }); // const messages = await t.query(api.messages.list); // expect(messages).toMatchObject([ // { body: "Hi!", author: "Sarah" }, // { body: "Hey!", author: "Tom" } // ]); }); ; it errors at schema.
Yvens
YvensOP9mo ago
Do you mean that it crashes or just like me you’ve got the type warning only ?
ampp
ampp9mo ago
yeah its typescript i think
Yvens
YvensOP9mo ago
Ok my bad, we have the same problem then. It is because defineSchema is replaced by defineEntSchema I think, let’s wait to hear about it from @Michal Srb For now I just silenced it with // @ts-ignore
ampp
ampp9mo ago
I'm curious if you figured out how to use the identity example with ents, the docs have: const asSarah = t.withIdentity({ name: "Sarah" }); await asSarah.mutation(api.tasks.create, { text: "Add tests" }); It don't seem to automatically work with the ctx.viewer
Yvens
YvensOP9mo ago
No sorry I didn't try to do that
Michal Srb
Michal Srb9mo ago
@ampp presumably your viewer is a document loaded from the DB, so you need to create it in the test. The t.withIdentity only sets the ctx.auth.getUserIdentity() return value.
ampp
ampp9mo ago
I don't get it, I'm creating the user named Sarah, it returns 10000;users which seems odd : const user: string & { __tableName: "users"; } then im suppose to call t.identity({ name: "Sarah" } ) as i see no way to pass that a actual reference to the db user i just created
Michal Srb
Michal Srb9mo ago
@ampp what's your definition of ctx.viewer ?
Michal Srb
Michal Srb9mo ago
@Yvens @ampp please follow the new docs on testing with Ents: https://labs.convex.dev/convex-ents/testing If you get any type errors, upgrade all packages to their latest versions (convex, convex-test, convex-helpers, convex-ents).
Testing Ents - Convex Ents
Relations, default values, unique fields and more for Convex
ampp
ampp9mo ago
almost exactly the same saas-starter, or the rules page
Michal Srb
Michal Srb9mo ago
@ampp you'd do this:
test("sending messages", async () => {
const t = convexTest(schema);
await t.run(async (baseCtx) => {
const ctx = await runCtx(baseCtx);
await ctx.table("users").insert({name: "Sarah", tokenIdentifier: "foo"});
});
const asSarah = t.identity({ tokenIdentifier: "foo" });
// use asSarah ....
});
test("sending messages", async () => {
const t = convexTest(schema);
await t.run(async (baseCtx) => {
const ctx = await runCtx(baseCtx);
await ctx.table("users").insert({name: "Sarah", tokenIdentifier: "foo"});
});
const asSarah = t.identity({ tokenIdentifier: "foo" });
// use asSarah ....
});
ampp
ampp9mo ago
So we ended up getting it to work this way:
const t2 = t.withIdentity({ tokenIdentifier: "foo" });
const [sarahId] = await t2.run(async (baseCtx) => {
const ctx = await runCtx(baseCtx);
// console.log("ctx", {ctx, t});
return await ctx.table("users")
.insert({
const t2 = t.withIdentity({ tokenIdentifier: "foo" });
const [sarahId] = await t2.run(async (baseCtx) => {
const ctx = await runCtx(baseCtx);
// console.log("ctx", {ctx, t});
return await ctx.table("users")
.insert({
But now its causing this error when we run npx convex dev unless we comment out any convex-test import
✘ [ERROR] Could not resolve "crypto"

../../node_modules/.pnpm/convex-test@0.0.12_convex@1.11.1/node_modules/convex-test/dist/index.js:3:27:
3 │ import { createHash } from "crypto";
╵ ~~~~~~~~

The package "crypto" wasn't found on the file system but is built into node. Are you trying to
bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
✘ [ERROR] Could not resolve "crypto"

../../node_modules/.pnpm/convex-test@0.0.12_convex@1.11.1/node_modules/convex-test/dist/index.js:3:27:
3 │ import { createHash } from "crypto";
╵ ~~~~~~~~

The package "crypto" wasn't found on the file system but is built into node. Are you trying to
bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
Chuck_Williams
Chuck_Williams9mo ago
It looks to me like ../../node_modules/.pnpm/convex-test@0.0.12_convex@1.11.1/node_modules/convex-test/package.json is either missing an entry for "crypto" - or the embedded "crypto" node module needs to be packaged correctly in there? Not 100% - but this is a blocker for us.
ballingt
ballingt9mo ago
It sounds like you're trying to load this test library in a Convex function, this isn't supported. convex-test runs only in node Check you convex/ files for an import of convex-test or of something that imports it @ampp where are you importing convex-test? Don't import it from files in the convex/ directory that aren't tests
ampp
ampp9mo ago
Yeah we also have the convex/testSetup.ts here: https://labs.convex.dev/convex-ents/testing maybe ill try moving that out of the convex folder.. But we were having the same issue with it before the testSetup.ts when using the identity function in our main .test.ts file (we are using clerk if that is relevant)
Testing Ents - Convex Ents
Relations, default values, unique fields and more for Convex
ampp
ampp9mo ago
I did make one change to testSetup.ts file, it was importing entsTableFactory
ballingt
ballingt9mo ago
This is an error that only happens when a file in the convex folder imports convex-test, which is what you need to avoid
ampp
ampp9mo ago
Ok that seems to work to move testSetup.ts out. Apricate the help, we're happy to have it setup. Just curious if there is a way to have a file in the convex folder that won't be processed by npx convex dev.
ballingt
ballingt9mo ago
If the file ends in .test.ts or .test.js it will not be processed, but we're thinking about ways to expand this But it can get confusing to keep track of which files you can and can't import from which other files, so I think keeping these separate helps
ampp
ampp9mo ago
The biggest thing im noticing writing the tests is they aren't respecting v.optional validators and i mostly use optional when i am using the viewer's data via ctx.viewer. Is this a expected behavior, i didn't see it in the limitations section.
Michal Srb
Michal Srb9mo ago
@ampp I cannot repro the issue with optional validators, can you open a new thread and provide reproduction instructions?
Michal Srb
Michal Srb9mo ago
@ampp I changed the instructions at https://labs.convex.dev/convex-ents/testing to suggest setup.testing.ts instead of testSetup.ts, that way both Vitest and Convex ignore the file.
Testing Ents - Convex Ents
Relations, default values, unique fields and more for Convex
Yvens
YvensOP9mo ago
Thank you ! Hello @Michal Srb @ballingt , I made the update but getting the type error Cannot find module 'convex-ents' or its corresponding type declarations. for convex-ent: 0.5.1 And also: cannot use namespace 'EntDefinition' as a type. Any idea ?
Michal Srb
Michal Srb9mo ago
Another user reported this too, something to do with bundling, will get back to you on this tomorrow. If you downgrade to ents 0.4.x and previous convex-test it should work for you.
Yvens
YvensOP9mo ago
Yes I did thank you

Did you find this page helpful?