-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mock next-auth session for client tests
- Loading branch information
1 parent
b12c145
commit fdbc4bf
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = () => {} | ||
|
||
|
@@ -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)) | ||
} | ||
}) |