conradkoh
conradkoh8mo ago

convex-test does not work in yarn workspace

When following the guide on convex.dev, if one is using a yarn workspace with packages hoisted, convex-test will error out and not be able to find the _generated directory. This was the guide I followed: - https://docs.convex.dev/functions/testing
FAIL convex/budget.test.ts > create / update budgets
Error: Could not find the "_generated" directory, make sure to run `npx convex dev` or `npx convex codegen`. If your Convex functions aren't defined in a directory called "convex" sibling to your node_modules, provide the second argument to `convexTest`
❯ findModulesRoot ../../node_modules/convex-test/dist/index.js:889:11
887| "`npx convex dev` or `npx convex codegen`. " +
888| (userProvidedModules
889| ? "Make sure your `import.meta.glob` includes the files in the " +
| ^
890| '"_generated" directory'
891| : "If your Convex functions aren't defined in a directory " +
❯ moduleCache ../../node_modules/convex-test/dist/index.js:871:20
❯ Module.convexTest ../../node_modules/convex-test/dist/index.js:918:18
❯ convex/budget.test.ts:9:13
FAIL convex/budget.test.ts > create / update budgets
Error: Could not find the "_generated" directory, make sure to run `npx convex dev` or `npx convex codegen`. If your Convex functions aren't defined in a directory called "convex" sibling to your node_modules, provide the second argument to `convexTest`
❯ findModulesRoot ../../node_modules/convex-test/dist/index.js:889:11
887| "`npx convex dev` or `npx convex codegen`. " +
888| (userProvidedModules
889| ? "Make sure your `import.meta.glob` includes the files in the " +
| ^
890| '"_generated" directory'
891| : "If your Convex functions aren't defined in a directory " +
❯ moduleCache ../../node_modules/convex-test/dist/index.js:871:20
❯ Module.convexTest ../../node_modules/convex-test/dist/index.js:918:18
❯ convex/budget.test.ts:9:13
This is because of how the library uses import.meta.glob with the assumption that the relative path of node_modules is ../../../convex/**/*.*s relative to where the library is. This is untrue for workspaces where packages are hoisted. A workaround exists, by ensuring that the "nohoist" option is used in the workspace. It would be good to include in the docs, or update such that convex-test can run as expected even when it is hoisted to the workspace root (e.g. by using the directory of the process perhaps).
9 Replies
Michal Srb
Michal Srb8mo ago
Testing | Convex Developer Hub
Automating the testing of your Convex functions is easy.
Michal Srb
Michal Srb8mo ago
Are you using yarn workspaces for RN? (this is a setup that we do want to support, so we should document whichever solution is preferable)
conradkoh
conradkohOP8mo ago
oo yeah I actually tried manually doing the import.meta.glob(...) step and it also worked (although typescript in the editor was complaining. but the tests passed as well.
Michal Srb
Michal Srb8mo ago
You need /// <reference types="vite/client" /> in the file where you call glob
conradkoh
conradkohOP8mo ago
I'm using yarn workspaces in general, to keep the frontend (remix) separate from the backend (convex). I generally default to using a workspace with something like - /apps/webapp - web frontend - /services/backend - backend this allows me to add other apps like RN for example into - /apps/mobile in the future. mm to clarify - I have my interim solution - which is just to ensure that the convex-test package does not get hoisted to the root.
Michal Srb
Michal Srb8mo ago
Gotcha, I'll think about how to mention it in the docs.
conradkoh
conradkohOP8mo ago
I think the expectation is that convex-test should work regardless of where it is installed. right now it is assuming that node_modules is always ../../../convex/_generated/**/*.*s away. I think if that assumption is changed (e.g. maybe use the working directory to find where the script is executed from instead) might be a little more precise. path.join(process.cwd(), convex/**/*.*s) perhaps
Michal Srb
Michal Srb8mo ago
The import string has to be a literal (for Vite to correctly bundle the code). So the linked docs must be used anytime this assumption is not correct.
conradkoh
conradkohOP8mo ago
oh I see..

Did you find this page helpful?