Skip to content

Commit

Permalink
Mock next-auth session for client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
negreirosleo committed Jan 2, 2024
1 parent b12c145 commit fdbc4bf
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions frontend/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import '@testing-library/jest-dom'
import { Session } from 'next-auth'

Element.prototype.scrollIntoView = () => {}

Expand All @@ -15,3 +16,32 @@ Object.defineProperty(window, 'matchMedia', {
dispatchEvent: jest.fn()
}))
})

// **Global Mocks**

// Due to Jest transformer issues, we mock next-auth's getServerSession and the authOptions directly:
export const mockSession: Session = {
expires: new Date(Date.now() + 2 * 86400).toISOString(),
user: {
name: 'user',
id: 0,
username: 'username',
email: '[email protected]',
firstName: 'user',
lastName: 'last name',
capacities: [],
roles: ['staff', 'admin'],
authorizedScopes: ['client:read']
}
}

jest.mock('./src/app/api/auth/[...nextauth]/route', () => {
return { authOptions: () => {} }
})

jest.mock('next-auth', () => {
return {
__esModule: true,
getServerSession: jest.fn(() => Promise.resolve(mockSession))
}
})

0 comments on commit fdbc4bf

Please sign in to comment.