TripleSpeeder
TripleSpeeder•10mo ago

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

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?
6 Replies
Convex Bot
Convex Bot•10mo ago
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 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!
lee
lee•10mo ago
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.
TripleSpeeder
TripleSpeederOP•10mo ago
The code is nested a bit, this is the part resulting in the error:
type InsertFeedItemProps = {
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;
}
type InsertFeedItemProps = {
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 🙂
lee
lee•10mo ago
ooh cool
TripleSpeeder
TripleSpeederOP•10mo ago
Thank you for the quick response @lee
lee
lee•10mo ago
i'm trying to think of whether the test can detect this and print a better error 🤔

Did you find this page helpful?