#schema type issue with convex-test

4 messages · Page 1 of 1 (latest)

hybrid isle
#

I just created my first convex function test. However I get a ts error when I provide schema to convexTest() - why?

const t = convexTest(schema, modules) // issue with schema see below

For the code present, we get this error:

Argument of type 'SchemaDefinition<{ projects: TableDefinition<VObject<{ description?: string | undefined; skills: Id<"skills">[]; name: string; createdBy: Id<"users">; status: "active" | "archived" | "deleted"; }, { name: VString<string, "required">; description: VString<...>; createdBy: VId<...>; status: VUnion<...>; skills: VArray...' is not assignable to parameter of type 'SchemaDefinition<GenericSchema, boolean>'.
  Types of property 'tables' are incompatible.
    Type '{ projects: TableDefinition<VObject<{ description?: string | undefined; skills: Id<"skills">[]; name: string; createdBy: Id<"users">; status: "active" | "archived" | "deleted"; }, { name: VString<string, "required">; description: VString<...>; createdBy: VId<...>; status: VUnion<...>; skills: VArray<...>; }, "requir...' is not assignable to type 'GenericSchema'.
      Property 'projects' is incompatible with index signature.
        Type 'TableDefinition<VObject<{ description?: string | undefined; skills: Id<"skills">[]; name: string; createdBy: Id<"users">; status: "active" | "archived" | "deleted"; }, { name: VString<string, "required">; description: VString<...>; createdBy: VId<...>; status: VUnion<...>; skills: VArray<...>; }, "required", "skills...' is not assignable to type 'TableDefinition<Validator<any, any, any>, {}, {}, {}>'.
          Types have separate declarations of a private property 'indexes'.

Fix it, verify, and then give a concise explanation.

tardy iceBOT
#

Hi @hybrid isle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

This TypeScript error comes from a type incompatibility between the SchemaDefinition type your schema value has, and the SchemaDefinition<GenericSchema, boolean> that convexTest expects.

From the docs, convexTest’s first argument is a SchemaDefinition<GenericSchema, boolean>. [Using convex-test] That GenericSchema is defined as:

type GenericSchema = Record<string, TableDefinition>;

[GenericSchema]

And SchemaDefinition is:

class SchemaDefinition<Schema extends GenericSchema, StrictTableTypes extends boolean> {
  tables: Schema;
  strictTableNameTypes: StrictTableTypes;
  readonly schemaValidation: boolean;
}

[SchemaDefinition]

Your error shows that the SchemaDefinition type of schema has a TableDefinition whose generic parameters don’t match the TableDefinition<Validator<any, any, any>, {}, {}, {}> that GenericSchema is written against, so TypeScript treats them as incompatible (“Types have separate declarations of a private property 'indexes'”).

This usually happens when:

  • schema is not the default export from convex/schema.ts created with defineSchema, or
  • schema is imported from a different Convex package / version than the one convex-test is using, so the TableDefinition class is coming from two different module instances (hence the “separate declarations of a private property” message).

To fix and verify:

  1. Ensure your schema is defined exactly like the docs, using defineSchema and defineTable from "convex/server":

    // convex/schema.ts
    import { defineSchema, defineTable } from "convex/server";
    import { v } from "convex/values";
#

const schema = defineSchema({
projects: defineTable({
name: v.string(),
description: v.string(),
createdBy: v.id("users"),
status: v.union(
v.literal("active"),
v.literal("archived"),
v.literal("deleted")
),
skills: v.array(v.id("skills")),
}),
// ...other tables
});

export default schema;


[[Schemas](<https://docs.convex.dev/database/schemas>); [defineSchema](<https://docs.convex.dev/api/modules/server#defineschema>)]

2. Import that exact default export into your test, and call `convexTest` with it:

```ts
// convex/projects.test.ts
import { convexTest } from "convex-test";
import { test } from "vitest";
import schema from "./schema";
const modules = import.meta.glob("./**/*.ts");

test("some behavior", async () => {
  const t = convexTest(schema, modules);
  // use `t`...
});

[Get started; Using convex-test]

  1. Make sure there is only one version of convex in your dependency tree, and that convex-test depends on the same version (otherwise you get two distinct TableDefinition classes, which is exactly what the “separate declarations of a private property 'indexes'” error indicates).

If you already define and import schema as above and still see this error, the remaining likely cause is a version mismatch between convex and convex-test. The knowledge sources don’t include specific version-compatibility guidance, so I can’t be more precise than recommending aligning them to the same Convex release.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: