jest-launchdarkly-mock
![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)
![Tweet](https://img.shields.io/twitter/url/https/github.com/launchdarkly/jest-launchdarkly-mock.svg?style=social)
Easily unit test LaunchDarkly feature flagged components with jest :clap:
This package is only compatible with the react sdk.
Installation
yarn add -D jest-launchdarkly-mock
or
npm install jest-launchdarkly-mock --save-dev
Then in jest.config.js
add jest-launchdarkly-mock to setupFiles:
module.exports = {
setupFiles: ['jest-launchdarkly-mock'],
}
Usage
Use the only 3 apis for test cases:
-
mockFlags(flags: LDFlagSet)
: mock flags at the start of each test case. Only mocks
flags returned by the useFlags
hook.
-
ldClientMock
: a jest mock of the ldClient. All
methods of this object are jest mocks.
-
resetLDMocks
: resets both mockFlags and ldClientMock.
Example
import { mockFlags, ldClientMock, resetLDMocks } from 'jest-launchdarkly-mock'
describe('button', () => {
beforeEach(() => {
resetLDMocks()
})
test('flag on', () => {
mockFlags({ devTestFlag: true })
const { getByTestId } = render(<Button />)
expect(getByTestId('test-button')).toBeTruthy()
})
test('identify', () => {
mockFlags({ 'dev-test-flag': true })
const { getByTestId } = render(<Button />)
fireEvent.click(getByTestId('test-button'))
expect(ldClientMock.identify).toBeCalledWith({ key: 'aa0ceb' })
})
})