Skip to main content

Better mocking for the Auth Context package

Previously, mocking the Auth Context was entirely manual and one had to specify what data to populate the Auth Context with. Now, mocking the Auth Context is as simple as calling jest.mock('@rhapsody/auth-context'). The Auth Context is populated with fixture data that can be overridden at any time by the developer. The snippet below shows how the Auth Context can now be mocked:

import { _setMockData } from '@rhapsody/auth-context'

// BEGIN CODE REQUIRED TO MOCK AUTH CONTEXT
jest.mock('@rhapsody/auth-context')

beforeEach(() => {
// Using this function without any overrides resets the mock data back to its
// default state
_setMockData()
})

Note: the _setMockData export is only available when mocking the auth context. It is not a part of the regular auth context package and does not work unless jest.mock('@rhapsody/auth-context') is invoked.

A full testing example is included in the package README.