-
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
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
1 parent
2122887
commit d1565b5
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)) | ||
} | ||
}) |