Convex-test/ents
Is there a way to use ctx.table in a test ?
Or should I insert data in the "users_to_roles" table created by ents ?
I don't get how to re-create the edge in my db setup.
Thank you
33 Replies
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.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
We are trying to implement tests on ents as well, its just funny we tried to do it today too.
I'm new here & working w/ @ampp on our project as co-developer.
@ampp did you guys managed to make it work for you ?
we just deferred it, because it seems to get hung up on a the ent schema
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
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.
Do you mean that it crashes or just like me you’ve got the type warning only ?
yeah its typescript i think
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
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
No sorry I didn't try to do that
@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.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
@ampp what's your definition of
ctx.viewer
?@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
almost exactly the same saas-starter, or the rules page
@ampp you'd do this:
So we ended up getting it to work this way:
But now its causing this error when we run npx convex dev unless we comment out any convex-test import
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.
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
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
I did make one change to testSetup.ts file, it was importing entsTableFactory
This is an error that only happens when a file in the convex folder imports convex-test, which is what you need to avoid
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.
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
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.
@ampp I cannot repro the issue with optional validators, can you open a new thread and provide reproduction instructions?
@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
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 ?
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.
Yes I did thank you