#Convex-test and by_creation_time

5 messages · Page 1 of 1 (latest)

meager karma
#

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);
    });
  },
});

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);
  // ...

Error while running tests

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 🙋‍♂️

odd novaBOT
#

Hi @meager karma. 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']),
});

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:


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

#

@meager karma kapa.ai is still learning and improving, please let us know how it did by reacting below

tribal parrot
#

Yep this sounds like it's a bug in convex-test. We'll look into it and report back