entropy
entropy8mo ago

Failed to load url with convex-test

Hello, I'm trying to setup vitest and convex test but everytime I write a query or mutation I get a failed to load url error.
import { convexTest } from 'convex-test'
import { expect, test } from 'vitest'
import { api } from '../../_generated/api'
import schema from '../../schema'

test('creating a user', async () => {
const t = convexTest(schema)
const mockUser = {
clerkId: 'user_2fyL',
username: 'cool username',
tag: 'coolusername',
updatedAt: new Date().getTime(),
accountStanding: 'good'
} satisfies WithoutSystemFields<Doc<'users'>>

await t.mutation(api.functions.user.createUser, mockUser)

const user = await t.run(async ctx => {
return await ctx.db
.query('users')
.withIndex('by_clerkId', q => q.eq('clerkId', mockUser.clerkId))
.first()
})

expect(user).toMatchObject(mockUser)
})
import { convexTest } from 'convex-test'
import { expect, test } from 'vitest'
import { api } from '../../_generated/api'
import schema from '../../schema'

test('creating a user', async () => {
const t = convexTest(schema)
const mockUser = {
clerkId: 'user_2fyL',
username: 'cool username',
tag: 'coolusername',
updatedAt: new Date().getTime(),
accountStanding: 'good'
} satisfies WithoutSystemFields<Doc<'users'>>

await t.mutation(api.functions.user.createUser, mockUser)

const user = await t.run(async ctx => {
return await ctx.db
.query('users')
.withIndex('by_clerkId', q => q.eq('clerkId', mockUser.clerkId))
.first()
})

expect(user).toMatchObject(mockUser)
})
No description
23 Replies
entropy
entropyOP8mo ago
weirdly enough if I use the run function it seems to work fine? I just can't pass in my functions to the query or mutation procedures.
Michal Srb
Michal Srb8mo ago
Ah. I bet this is because you nested the test file in a test folder. I think for now you have to put all test files into the "top-level" convex/ folder. I filed https://github.com/get-convex/convex-test/issues/6 to look at this more.
entropy
entropyOP8mo ago
Ah I see thank you will give it a try tomorrow morning
entropy
entropyOP8mo ago
even after moving my test to the top level of the convex folder it still seems to not work with my functions
No description
entropy
entropyOP8mo ago
No description
entropy
entropyOP8mo ago
I am using this within a nextjs project and utilizing vitest workspaces so I can have a different test enviroment for my convex and nextjs. If that could possibly be the issue.
Michal Srb
Michal Srb8mo ago
That's probably it, as I haven't tried workspaces.
entropy
entropyOP8mo ago
I see
Michal Srb
Michal Srb8mo ago
Yep, I can repro
entropy
entropyOP8mo ago
Ahh are there any recommended ways of testing in a nextjs project without workspaces then?
Michal Srb
Michal Srb8mo ago
I think if you just delete the vitest.workspace.ts file everything will work? The only difference is that you can't as easily run only one set of tests or the other?
entropy
entropyOP8mo ago
Hmm interesting well that's fine for now thanks!
Michal Srb
Michal Srb8mo ago
I'm looking into fixing this
entropy
entropyOP8mo ago
awesome thank you
RJ
RJ8mo ago
I've also run into this error twice, in two different projects In one the test file was located in test/here.test.ts and the Convex functions directory was in test/convex (atypical certainly), in the other the test was in src/convex/dir/otherDir/here.test.ts (so, inside the Convex functions directory)
Michal Srb
Michal Srb8mo ago
Thank's @RJ, it's helpful to know! I'm confident I can get this and bunch of the other related issues with convex-test fixed.
RJ
RJ8mo ago
Great, thanks @Michal Srb! Let me know if I can provide any more details/do any testing for you
djbalin
djbalin3mo ago
Hi @Michal Srb , I'm experiencing some problems with convex-test. Specifically, whenever I run a test that imports import { api } from '@/convex/_generated/api' I get the error shown in the screenshot. Seems to be some error in node_modules/convex-test/dist/index.js, so I'm not sure how I can troubleshoot this. It works fine if I run a simpler test that does not import api but only imports import schema from '@/convex/schema'
No description
lee
lee3mo ago
@djbalin what versions of convex-test and convex do you have (npm ls). The newest version of convex-test had a peer dependency on a newer version of convex
djbalin
djbalin3mo ago
@lee ├─ convex-helpers@npm:0.1.56 ├─ convex-test@npm:0.0.33 ├─ convex@npm:1.16.3 Could that be the problem, that the newest Convex version is 1.16.6 and test depends on that?
lee
lee3mo ago
Yeah you need at least convex@1.16.4 . When you installed convex-test it should have printed an error that the peer dependency isn't satisfied
lee
lee3mo ago
https://github.com/get-convex/convex-test/blob/main/package.json#L35 (it's also possible i don't understand peer dependencies or how they interact with pnpm)
GitHub
convex-test/package.json at main · get-convex/convex-test
Testing harness for pure-JS Convex tests. Contribute to get-convex/convex-test development by creating an account on GitHub.
djbalin
djbalin3mo ago
I'm sure you understand it better than I do 😂 thanks man. Guess I should pay more attention to dependency warnings in the future! Works now after updating convex, thanks man! ☺️