Skip to content

How to test next-auth middleware? #1600

Answered by songz
flacial asked this question in Q&A
Discussion options

You must be logged in to vote

FYI When struggling with unit test, the correct solution isn't "let's add functional test". It works, but it hides the underlying problem which is that we want to figure out a way to unit test the function.

I don't think we need to test the return value of nextauth middleware, but rather what we pass into nextauth right via the callbacks? If so, we just need to test the callback functions.

Example (about signInCallback)

export const signInCallback = async ({ user, account, profile, email, credentials }) {
      ...
}

...
  callbacks: {
    signIn: signinCallback,
    ...
...
}

In your test you'd just test signInCallback right?

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@flacial
Comment options

flacial Mar 16, 2022
Maintainer Author

Answer selected by flacial
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants