get-convex / convex-test

Testing harness for pure-JS Convex tests
Apache License 2.0
3 stars 1 forks source link

ensure that null values are not simple objects #10

Closed conradkoh closed 3 months ago

conradkoh commented 3 months ago

Fix crashes in test when using nested optional properties in an index

  1. There is no type error
  2. The test fails in convex-test
  3. The test passes against the live convex server

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

the issue I think is that `typeof null` is `"object"`, and this was a case not considered in the implementation of the function.

also, supplementing the other lines of the stack trace for info
```typescript
 FAIL  convex/transaction.test.ts > creates transactions
TypeError: Cannot convert undefined or null to object
 ❯ isSimpleObject node_modules/convex-test/dist/index.js:414:30
    412|     const isSimple = prototype === null ||
    413|         prototype === Object.prototype ||
    414|         // Objects generated from other contexts (e.…
       |                              ^
    415|         // conditions but are still simple objects.
    416|         prototype?.constructor?.name === "Object";
 ❯ evaluateFieldPath node_modules/convex-test/dist/index.js:426:18
 ❯ evaluateRangeFilter node_modules/convex-test/dist/index.js:493:20
 ❯ node_modules/convex-test/dist/index.js:265:56
 ❯ node_modules/convex-test/dist/index.js:265:38
 ❯ DatabaseFake._iterateDocs node_modules/convex-test/dist/index.js:232:21
 ❯ DatabaseFake._evaluateQuery node_modules/convex-test/dist/index.js:264:22
 ❯ DatabaseFake.startQuery node_modules/convex-test/dist/index.js:181:30


----

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
xixixao commented 3 months ago

Thanks for the PR, I fixed this in 35d0a84 (the fix required a slightly different approach).