N
Nuxt7mo ago
dwol

How can I mock or stub Keycloak in unit tests?

So basically I have a nuxt plugin to setup Keycloak, how can I mock or stub keycloak during unit tests? This is my plugin
import Keycloak from 'keycloak-js'

export default defineNuxtPlugin(async (_nuxtApp) => {
const config = useRuntimeConfig()
const keycloak = new Keycloak({
url: config.public.keycloakAuthUrl,
realm: config.public.keycloakRealm,
clientId: config.public.keycloakClientId
})

try {
// init keycloak instance
await keycloak.init({
onLoad: 'check-sso',
responseMode: 'query',
pkceMethod: 'S256'
})

} catch (error) {
console.error('Failed to initialize Keycloak adapter: ', error)
}

return {
provide: {
// provide global keycloak instance
keycloak
}
}
})
import Keycloak from 'keycloak-js'

export default defineNuxtPlugin(async (_nuxtApp) => {
const config = useRuntimeConfig()
const keycloak = new Keycloak({
url: config.public.keycloakAuthUrl,
realm: config.public.keycloakRealm,
clientId: config.public.keycloakClientId
})

try {
// init keycloak instance
await keycloak.init({
onLoad: 'check-sso',
responseMode: 'query',
pkceMethod: 'S256'
})

} catch (error) {
console.error('Failed to initialize Keycloak adapter: ', error)
}

return {
provide: {
// provide global keycloak instance
keycloak
}
}
})
Then I've tried to stub useNuxtApp like this, ive also tried mocking using mockNuxtImport
vi.stubGlobal('useNuxtApp', () => ({
$keycloak: {
login: mockLogin,
logout: vi.fn(),
loadUserProfile: vi.fn(),
authenticated: false,
tokenParsed: mockParsedToken
}
}))
vi.stubGlobal('useNuxtApp', () => ({
$keycloak: {
login: mockLogin,
logout: vi.fn(),
loadUserProfile: vi.fn(),
authenticated: false,
tokenParsed: mockParsedToken
}
}))
But I still get this error Failed to initialize Keycloak adapter: clientId missing Can anyone point me in the right direction?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server