Testing Authenticated Functions
@OccultSlolem writes:
What's a good approach for writing tests for Convex actions, queries, and mutations, especially when those tests are dependent on auth state?
2 Replies
You have a few options here. One is to call the function directly, passing in mock objects, and passing in something for auth that returns what you expect from the
getUserIdentity()
function.
A more manual method would be to call it from the dashboard. You can check "Act as a user" and put whatever JSON into the field, for instance putting in the tokenIdentifier
or subject
you might expect in the function
Finally, you could decompose your function into testable units inside of the auth environment.
That's all I can think of for nowWe definitely want to provide a more straightforward testing setup in the future. For now, for automated tests, you have to provide your own mocks for any context passed into functions. You could for example call a query from a test:
export const myQuery = …
// test.ts
myQuery({auth: {getUserIdentity() { return { … }; }}}, {arg0: …} )