adam
adam4mo 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.
7 Replies
Convex Bot
Convex Bot4mo 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!
erquhart
erquhart4mo ago
I see the difference in behavior you're pointing at, but can you help me understand the goal here? _creationTime is guaranteed (and required) for every Convex document. If _creationTime is accepted by defineTable, it still wouldn't result in any change to the definition of the field (eg., it can't be optional or of a different type).
adam
adamOP4mo ago
I’m using a shared Zod schema to define my tables. This schema contains _creationTime as an optional value as it may or may not exist. I understand passing _creationTime as part of the schema has no impact on the table created. It’s working fine for defineSchema() so I would expect convex-test to parse the valid Convex schema without errors. Thanks for looking into it and let me know if it needs further clarification.
erquhart
erquhart4mo ago
Ah I see, it's ignored by the actual backend, so it's a convenience, but it's breaking in tests. Got it. Looks like a pretty simple fix, would need an exception for '_creationTime' here: https://github.com/get-convex/convex-test/blob/83f7e271d41f086b16cfcf8f7efc53efd7491b19/index.ts#L846. Not sure how the rest of the code would handle it, though. Actually it looks like insert, patch, and replace all account for it with the same kind of logic as the backend.
erquhart
erquhart4mo ago
GitHub
allow system fields in schema by erquhart · Pull Request #38 · ge...
This aligns with backend behavior. This is already supported in insert/patch/replace mocks, they just aren't allowed by the field validation regex.
erquhart
erquhart4mo ago
Fixed in 0.0.37
adam
adamOP4mo ago
This has fixed the issue - thanks @erquhart!

Did you find this page helpful?