Yvens
Yvens10mo ago

Problem testing convex function with convex-test.

I'm using ents to handle my db logic.
No description
8 Replies
ballingt
ballingt10mo ago
Is this something that was working before, is it a regression? Or has this never worked? Also what's not working here, the test or the implementation?
Yvens
YvensOP10mo ago
Hello, only the test is not working, the implementation is working fine I just copied the code in the documentation and replace it with my own function that I know is working to test the water.
import { convexTest } from "convex-test";
import { expect, test } from "vitest";
import { api } from "./_generated/api";
import schema from "./schema";

test("sending messages", 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" }
]);
});
import { convexTest } from "convex-test";
import { expect, test } from "vitest";
import { api } from "./_generated/api";
import schema from "./schema";

test("sending messages", 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" }
]);
});
` And this is my code:
// Start of test
import { convexTest } from "convex-test";
import { expect, test } from "vitest";
import { api } from "./_generated/api";
import schema from "./schema";
import { Id } from "./_generated/dataModel";

test.only("test", async() => {
const t = convexTest(schema);
await t.mutation(api.users.storeUser, {firstName: "Yvens",lastName: "Belaston",email: "yvens@gmail.com"});
})

// End of test

// Store User function
export const storeUser = mutation({
args: {
firstName: v.string(),
lastName: v.string(),
email: v.string(),
},
handler: async (ctx, args) => {
const user = await ctx.table("users").get("email", args.email);
if (!user) {
const userId = await ctx.table("users").insert({
firstname: args.firstName,
lastname: args.lastName,
email: args.email,
})
return userId;
}

return user._id;
}
})
// Start of test
import { convexTest } from "convex-test";
import { expect, test } from "vitest";
import { api } from "./_generated/api";
import schema from "./schema";
import { Id } from "./_generated/dataModel";

test.only("test", async() => {
const t = convexTest(schema);
await t.mutation(api.users.storeUser, {firstName: "Yvens",lastName: "Belaston",email: "yvens@gmail.com"});
})

// End of test

// Store User function
export const storeUser = mutation({
args: {
firstName: v.string(),
lastName: v.string(),
email: v.string(),
},
handler: async (ctx, args) => {
const user = await ctx.table("users").get("email", args.email);
if (!user) {
const userId = await ctx.table("users").insert({
firstname: args.firstName,
lastname: args.lastName,
email: args.email,
})
return userId;
}

return user._id;
}
})
Please let me know if you need anything else @ballingt
ballingt
ballingt10mo ago
This looks like a bug in https://github.com/get-convex/convex-test, could you open an issue there? It may not be until next week that this gets fixed. specifically https://github.com/get-convex/convex-test/blob/de3affc4292f286e67963ad6df4c39d552f40588/index.ts#L256
Yvens
YvensOP10mo ago
@ballingt , I can't seem to access those links, I get 404 I think the repo is private
ballingt
ballingt10mo ago
ah thanks, sorry about that! this is very new I'll file this, bet it gets fixed next week. I would just make it public but I didn't write it so want to confirm with the folks that did
Yvens
YvensOP10mo ago
Ok thank you, let me know if you get any news about that. I'll just test my function manually for now.
Michal Srb
Michal Srb10mo ago
This particular issue has been fixed, please upgrade to latest version of convex-test
Yvens
YvensOP10mo ago
@Michal Srb Thank you !

Did you find this page helpful?