5

I search to stub a ~/server/middleware/auth server middleware, i.e. a simple true/false server auth middleware. In the present situation, I am making integration tests in the backend of the app. A route is first called, then the returned response and the value updated in the database are checked. I have cases with authentication: ensure the route returns the expected error if the user is not logged in.

Current behavior:

  • check a JsonWebToken emitted by an active directory

Expected behavior of the middleware:

import { Ref } from '@vue/runtime-dom'
import { ref } from 'vue'
import { defineEventHandler } from 'h3'

const NOT_LOGGED_IN = null
export const isLoggedIn: Ref<{ email: string, name: string } | typeof NOT_LOGGED_IN> = ref(NOT_LOGGED_IN)

export const useAuthUserMock = () => ({
  resetAuthMockUser: () => isLoggedIn.value = NOT_LOGGED_IN,
  setAuthMockUser: (email = 'user.mock@email.com', name = 'User Mock') => {
    isLoggedIn.value = {
      email, name
    }
  }
})

export default defineEventHandler((event) => {
  event.context.auth = isLoggedIn.value
})

I tried:

  • another nuxt.config.ts with a different (not possible, custom director
  • vi.mock() but the original middleware is still used instead of the mock
Lyokolux
  • 1,227
  • 2
  • 10
  • 34

0 Answers0