adam
adam2d ago

`convex-test` invalid identifier

Given this code:
// messages.test.ts
import { convexTest } from 'convex-test';
import { expect, test } from 'vitest';
import { api } from './_generated/api';
import schema from './schema';

test('insert', async () => {
const t = convexTest(schema);
await t.mutation(api.messages.send, { author: 'adam' });
const messages = await t.query(api.messages.list);
expect(messages).toMatchObject([{ author: 'adam' }]);
});
// messages.test.ts
import { convexTest } from 'convex-test';
import { expect, test } from 'vitest';
import { api } from './_generated/api';
import schema from './schema';

test('insert', async () => {
const t = convexTest(schema);
await t.mutation(api.messages.send, { author: 'adam' });
const messages = await t.query(api.messages.list);
expect(messages).toMatchObject([{ author: 'adam' }]);
});
It works as expected when the schema looks like this:
// schema.ts
import { defineSchema, defineTable } from 'convex/server';
import { v } from 'convex/values';

export default defineSchema(
{
messages: defineTable({
author: v.string()
}),
},
{ schemaValidation: true },
);
// schema.ts
import { defineSchema, defineTable } from 'convex/server';
import { v } from 'convex/values';

export default defineSchema(
{
messages: defineTable({
author: v.string()
}),
},
{ schemaValidation: true },
);
However, when a system field such as _creationTime is added to the schema, vitest throws this error: Error: Field names must be valid identifiers, got "_creationTime"
// schema.ts
import { defineSchema, defineTable } from 'convex/server';
import { v } from 'convex/values';

export default defineSchema(
{
messages: defineTable({
author: v.string(),
_creationTime: v.optional(v.number()), // <= !
}),
},
{ schemaValidation: true },
);
// schema.ts
import { defineSchema, defineTable } from 'convex/server';
import { v } from 'convex/values';

export default defineSchema(
{
messages: defineTable({
author: v.string(),
_creationTime: v.optional(v.number()), // <= !
}),
},
{ schemaValidation: true },
);
https://github.com/get-convex/convex-test/blob/83f7e271d41f086b16cfcf8f7efc53efd7491b19/index.ts#L838 Desired behaviour Can convex-test accept a schema with the same policy as defineSchema({...}) used in schema.ts and not a more strict policy that doesn't allow for _creationTime? defineSchema({...}) allows the _creationTime field.
1 Reply
Convex Bot
Convex Bot2d ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!

Did you find this page helpful?