Skip to content

Commit

Permalink
Mock next-auth session for client tests
Browse files Browse the repository at this point in the history
The Jest transformer breaks with some server libs that are used by the
server actions, this globally mocks the next-auth session so we don't
have to worry about it ever again.
  • Loading branch information
negreirosleo committed Jan 3, 2024
1 parent 2122887 commit d1565b5
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 d1565b5

Please sign in to comment.