Riki
Riki7mo ago

Convex-test and by_creation_time

Hello, when testing a function that uses the index "by_creation_time" , convex-test are failing and reporting an error. It looks like to me the code is valid, so I believe it's a convex-test issue: Example: Code
export const deleteNotifications = internalMutation({
handler: async ctx => {
const now = Date.now();
const staleTimestamp = now - 30 * 24 * 60 * 60 * 1000;
const notificationsToDelete = await ctx.db
.query('notifications')
.withIndex('by_creation_time', q => q.lt('_creationTime', staleTimestamp))
.take(500);

await asyncMap(notificationsToDelete, async notification => {
await ctx.db.delete(notification._id);
});
},
});
export const deleteNotifications = internalMutation({
handler: async ctx => {
const now = Date.now();
const staleTimestamp = now - 30 * 24 * 60 * 60 * 1000;
const notificationsToDelete = await ctx.db
.query('notifications')
.withIndex('by_creation_time', q => q.lt('_creationTime', staleTimestamp))
.take(500);

await asyncMap(notificationsToDelete, async notification => {
await ctx.db.delete(notification._id);
});
},
});
Test
import {convexTest} from 'convex-test';
import {expect, test, vi} from 'vitest';
import schema from './schema';
import {internal} from './_generated/api';

test('Delete old notifications', async () => {
// ...
const t = convexTest(schema);
await t.mutation(internal.notifications.deleteNotifications);
// ...
import {convexTest} from 'convex-test';
import {expect, test, vi} from 'vitest';
import schema from './schema';
import {internal} from './_generated/api';

test('Delete old notifications', async () => {
// ...
const t = convexTest(schema);
await t.mutation(internal.notifications.deleteNotifications);
// ...
Error while running tests
Cannot use index "by_creation_time" for table "notifications" because it is not declared in the schema.
Cannot use index "by_creation_time" for table "notifications" because it is not declared in the schema.
Just reporting the issue, thanks in advance if you fix it. Have a nice day Convex team 🙋‍♂️
3 Replies
kapa.ai
kapa.ai7mo ago
Hi @Riki. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The error message you're seeing suggests that the by_creation_time index is not declared in your schema for the notifications table. While Convex automatically creates the by_creation_time index and uses it for database queries that don't specify an index, it seems that this index might not be automatically available in the testing environment provided by convex-test. To resolve this issue, you could try explicitly declaring the by_creation_time index in your schema for the notifications table. Here's an example of how you might do this:
export default defineSchema({
notifications: defineTable({
// ... your other fields ...
}).index('by_creation_time', ['_creationTime']),
});
export default defineSchema({
notifications: defineTable({
// ... your other fields ...
}).index('by_creation_time', ['_creationTime']),
});
This will explicitly declare the by_creation_time index in your schema, which should make it available for use in your tests. If you continue to experience issues, I would recommend reaching out to the Convex support community for further assistance. Sources: - Database > Indexes > Limits (https://docs.convex.dev/database/indexes#limits) - Functions > Testing > convex-test library > Calling functions with t.query, t.mutation and t.action (https://docs.convex.dev/functions/testing#calling-functions-with-tquery-tmutation-and-taction) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7mo ago
<@145858609085939712> kapa.ai is still learning and improving, please let us know how it did by reacting below
sshader
sshader7mo ago
Yep this sounds like it's a bug in convex-test. We'll look into it and report back Should be fixed in convex-test@0.0.21

Did you find this page helpful?