conradkohC
Convex Community2y ago
17 replies
conradkoh

convex-test fails for nested optional properties used in index

Hi, I am reporting a bug that happens in convex-test, when you use an optional property in an index.

In this example, you see that add_info is an optional property (by way of being excluded in one of the types within a discriminated union type).
1. There is no type error
2. The test fails in convex-test
3. The test passes against the live convex server

I've also create a full reproduction with different scenarios (refer to commit history) here.
- https://github.com/conradkoh/convex-test-index-bug-repo/commits/master/

Thanks in advance.

Schema
export default defineSchema({
  user: defineTable(
    v.union(
      v.object({
        timestamp: v.number(),
        type: v.literal('without_age'),
        name: v.string(),
      }),
      v.object({
        timestamp: v.number(),
        type: v.literal('with_age'),
        name: v.string(),
        add_info: v.object({
          age: v.number(),
        }),
      })
    )
  ).index('by_age_timestamp', ['add_info.age', 'timestamp']),
});


Query Code
const users = await ctx.db
  .query('user')
  .withIndex('by_age_timestamp', (f) =>
    f.eq('add_info.age', age).gt('timestamp', 0)
  )
  .collect();


Test
  it('local: should not return users that have no age', async () => {
    const t = convexTest(schema);
    await t.mutation(api.user.create, createUserParams);
    const users = await t.query(api.user.list, queryParams);
    expect(users).toEqual([]);
  });


Error
TypeError: Cannot convert undefined or null to object
 ❯ isSimpleObject node_modules/convex-test/dist/index.js:415:30
    413|     const isSimple = prototype === null ||
    414|         prototype === Object.prototype ||
    415|         // Objects generated from other contexts (e.…
       |                              ^
Was this page helpful?