#"Error: Write outside of transaction" with convex-test

10 messages · Page 1 of 1 (latest)

cerulean eagle
#

I'm a bit lost how i go about debugging/fixing an error only thrown inside a test. The code works fine when running against real backend, but when running through vitest with convex-test backedn I get this error: "Error: Write outside of transaction 10126;feed". There is nothing special about the code, it just does a pretty normal db.insert(...). Any ideas how to tackle this?

dapper oreBOT
#

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](https://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!
rustic hearth
#

hmm this is a new assertion so there might be issues with it. can you share your test code? two things to watch out for:

  1. make sure you're awaiting all promises.
  2. if you're using the scheduler, make sure everything scheduled by a test finishes before the test completes.
cerulean eagle
#

The code is nested a bit, this is the part resulting in the error:

  db: DatabaseWriter;
  targetId: FollowTargetIdValidatorType;
  targetTableName: FollowTargetTableNameValidatorType;
  eventId: Id<"event">;
};
export const insertFeedItem = async ({
  db,
  targetId,
  targetTableName,
  eventId,
}: InsertFeedItemProps) => {
  const event = await db.get(eventId);
  if (!event) {
    throw new ConvexError(`InsertFeedItem: Event ${eventId} not found`);
  }
  const feedItemId = await db.insert("feed", {
    targetId,
    targetTableName,
    eventId,
    timestamp: event.timestamp,
  });
  return feedItemId;
}
#

probably not very helpful on its own 🙂

#

I'm not using scheduler, but now I'm checking if all promises are awaited...

#

Because the actual tests are all successful, just in the end the error comes up. (But I'm not verifying that the feed entry is created in the test as this is rather a sideeffect / not focus of the testcase.)

#

Yes, somewhere up the call chain an await was missing 🙂

rustic hearth
#

ooh cool

cerulean eagle
#

Thank you for the quick response @rustic hearth