Ross Allen
Ross Allen14mo ago

`Missing "./_generated/server" specifier in "convex" package` when testing

I am attempting to set up testing in Convex following the testing guide (https://docs.convex.dev/functions/testing) but am getting the following error after writing my first test.
Error: Missing "./_generated/server" specifier in "convex" package
❯ e node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:21914:25
❯ n node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:21914:627
❯ o node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:21914:1297
❯ resolveExportsOrImports node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:29466:20
❯ resolveDeepImport node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:29485:31
❯ tryNodeResolve node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:29210:20
❯ Context.resolveId node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:28978:28
❯ Object.resolveId node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:63984:64
❯ TransformContext.resolve node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:63689:23
Error: Missing "./_generated/server" specifier in "convex" package
❯ e node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:21914:25
❯ n node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:21914:627
❯ o node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:21914:1297
❯ resolveExportsOrImports node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:29466:20
❯ resolveDeepImport node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:29485:31
❯ tryNodeResolve node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:29210:20
❯ Context.resolveId node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:28978:28
❯ Object.resolveId node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:63984:64
❯ TransformContext.resolve node_modules/vite/dist/node/chunks/dep-V3BH7oO1.js:63689:23
it's reproducible with only the following, and I don't know what the error is telling me:
import { describe, expect, test } from "vitest";
import { api } from "./_generated/api";
import { convexTest } from "convex-test";
import schema from "./schema";

describe("updateMessage", () => {
test("sends messages", async () => {
const t = convexTest(schema);
});
});
import { describe, expect, test } from "vitest";
import { api } from "./_generated/api";
import { convexTest } from "convex-test";
import schema from "./schema";

describe("updateMessage", () => {
test("sends messages", async () => {
const t = convexTest(schema);
});
});
9 Replies
ballingt
ballingt14mo ago
What does the rest of your project look like? The unusual thing happening here is that vitest is looking for "./_generated/server" in the convex package instead of in the convex directory. to figure out why, I'm wondering if you - have surprising files in your convex directory like a package.json - have a different tsconfig.json settings than expected - have some path aliases defined but if it's possible to share your code that might be quickest (and thanks for letting us know! this testing guide is new, we're probably not accounting for all the different ways a project can be set up yet)
Ross Allen
Ross AllenOP14mo ago
Oh those are good tips * no package.json in the convex/ directory. there's a package.json at the root and all Convex-related files are in the convex/ directory also at the root * the project does have tsconfig.json vs. tsconfig.node.json for Vite's different environments * the project aliases "@/" to the "./src" directory
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
^ that's in vite.config.ts in tsconfig there is the following:
"paths": {
"@/*": [
"src/*"
]
},
"paths": {
"@/*": [
"src/*"
]
},
Vitest claims to use resolve.alias from the Vite config: https://vitest.dev/config/#alias I'm more familiar with React and Jest than Vue and Vite. I appreciate the help
ballingt
ballingt14mo ago
for some quick debugging, anything different if you replace . with ../convex in that import? Or add a file extension .js to that one and the schema one?
Ross Allen
Ross AllenOP14mo ago
the ../convex didn't change the error
import { describe, test } from "vitest";
import { convexTest } from "convex-test";
import schema from "../convex/schema";

describe("updateMessage", () => {
test("sends messages", () => {
convexTest(schema);
});
});
import { describe, test } from "vitest";
import { convexTest } from "convex-test";
import schema from "../convex/schema";

describe("updateMessage", () => {
test("sends messages", () => {
convexTest(schema);
});
});
^ same error
import { describe, test } from "vitest";
import { convexTest } from "convex-test";
import schema from "../convex/schema.js";

describe("updateMessage", () => {
test("sends messages", () => {
convexTest(schema);
});
});
import { describe, test } from "vitest";
import { convexTest } from "convex-test";
import schema from "../convex/schema.js";

describe("updateMessage", () => {
test("sends messages", () => {
convexTest(schema);
});
});
^ same here too
FAIL convex/sendMessage.test.ts [ convex/sendMessage.test.ts ]
Error: Missing "./_generated/server" specifier in "convex" package
FAIL convex/sendMessage.test.ts [ convex/sendMessage.test.ts ]
Error: Missing "./_generated/server" specifier in "convex" package
ballingt
ballingt14mo ago
Thanks for trying these, I just guessing at how to get around this confusion. If you could share your code or make a copy and rip out the private parts to repro I'd love to see
Ross Allen
Ross AllenOP14mo ago
okay I may have tracked it down. I started binary searching through schema.ts by commenting stuff out. there was one table schema defined in a separate file userSettings.ts
import type { Doc } from "convex/_generated/dataModel";
import { mutationWithUser } from "./userHelpers";
import { internalQuery } from "convex/_generated/server";
import { queryWithUser } from "./userHelpers";
import { v } from "convex/values";

export const userSettings = v.object({
name: v.string(),
userId: v.id("users"),
value: v.union(v.string(), v.number(), v.boolean()),
});
import type { Doc } from "convex/_generated/dataModel";
import { mutationWithUser } from "./userHelpers";
import { internalQuery } from "convex/_generated/server";
import { queryWithUser } from "./userHelpers";
import { v } from "convex/values";

export const userSettings = v.object({
name: v.string(),
userId: v.id("users"),
value: v.union(v.string(), v.number(), v.boolean()),
});
schema.ts
import { userSettings } from "./users/userSettings";

export default defineSchema(
{
userSettings: defineTable(userSettings).index("by_user", ["userId"]),
...
import { userSettings } from "./users/userSettings";

export default defineSchema(
{
userSettings: defineTable(userSettings).index("by_user", ["userId"]),
...
might be those funky paths. unsure why they are importing from "convex/" instead of "./" yes that seems to be it. I don't know why those paths were "convex/.." or even how they were working? I changed them to "../.." and now it looks like the error is gone
ballingt
ballingt14mo ago
oh that must have been it, yeah convex is both a package and a directory, possibly something to change someday if this kind of path aliasing gets more popular schema is especially tough because convex/schema is an entry point and ./convex/schema.ts is a file
Michal Srb
Michal Srb14mo ago
My guess is that it's a difference between esbuild config used by Convex and esbuild config used by Vitest. The first one ignored the invalid unused paths, the second one threw an error.
Ross Allen
Ross AllenOP14mo ago
thank you both for commenting. @ballingt, your initial comment is what made me start looking at the imports in schema.ts and sent me searching in the right spot

Did you find this page helpful?