Easily unit test LaunchDarkly feature flagged components with jest
npm install jest-launchdarkly-mock





> Easily unit test LaunchDarkly feature flagged components with jest :clap:
This package is only compatible with the react sdk.
``bash`
yarn add -D jest-launchdarkly-mock
or
`bash`
npm install jest-launchdarkly-mock --save-dev
Then in jest.config.js add jest-launchdarkly-mock to setupFiles:
`js`
// jest.config.js
module.exports = {
setupFiles: ['jest-launchdarkly-mock'],
}
* mockFlags(flags: LDFlagSet): mock flags at the start of each test case. Only mocksuseFlags
flags returned by the hook.
* ldClientMock: a jest mock of the ldClient. All
methods of this object are jest mocks.
* resetLDMocks : resets both mockFlags and ldClientMock.
tsx
import { mockFlags, ldClientMock, resetLDMocks } from 'jest-launchdarkly-mock'describe('button', () => {
beforeEach(() => {
// reset before each test case
resetLDMocks()
})
test('flag on', () => {
// arrange
// You can use the original unchanged case, kebab-case, camelCase or snake_case keys.
mockFlags({ devTestFlag: true })
// act
const { getByTestId } = render()
// assert
expect(getByTestId('test-button')).toBeTruthy()
})
test('identify', () => {
// arrange
mockFlags({ 'dev-test-flag': true })
// act
const { getByTestId } = render()
fireEvent.click(getByTestId('test-button'))
// assert: identify gets called
expect(ldClientMock.identify).toBeCalledWith({ key: 'aa0ceb' })
})
})
``