jest-launchdarkly-mock is a utility package designed to simplify unit testing of React components that integrate with LaunchDarkly feature flags. It provides a straightforward API to mock feature flag states and the LaunchDarkly client within Jest test environments, specifically for applications using the `launchdarkly-react-client-sdk`. The current stable version is 2.2.1, with releases occurring periodically to address bugs and support new features of the underlying LaunchDarkly SDKs. Key differentiators include its tight integration with Jest for mocking, its explicit support for React SDK hooks like `useFlags`, and its ability to simulate client-side LaunchDarkly behavior without network calls. It supports LaunchDarkly's custom contexts feature introduced in v2.0.0, which evolved from the 'users' concept. The library is primarily used in TypeScript projects, shipping with its own type definitions.
npm install jest-launchdarkly-mockVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to mock feature flag states and assert LaunchDarkly client interactions within a Jest test using `mockFlags`, `ldClientMock`, and `resetLDMocks`.
Review the migration guide for `launchdarkly-react-client-sdk` v3.0.0 and update your mock `identify` and `track` calls to use context objects instead of user objects.
Ensure your tests explicitly call `mockFlags` to set desired flag states. If you were relying on implicit defaults, those tests might now fail. This primarily affects `useFlags` hook usage.
Verify that your application uses `launchdarkly-react-client-sdk`. If using the plain JS SDK, a different mocking strategy or library will be required.
Add `'jest-launchdarkly-mock'` to the `setupFiles` array in your `jest.config.js` or equivalent Jest configuration file.
Upgrade your React version to `17.0.2` or higher, or use an older version of `jest-launchdarkly-mock` (e.g., 1.0.3 or earlier) that has a less restrictive React peer dependency.
Ensure `jest-launchdarkly-mock` is listed in your `jest.config.js` `setupFiles` array and `resetLDMocks()` is called in a `beforeEach` hook.
Verify that `jest-launchdarkly-mock` is present in your Jest `setupFiles` configuration. Also, ensure your component is rendered within the scope of the mock.
Check your React peer dependency version (should be `>=17.0.2`) and ensure `jest-launchdarkly-mock` is correctly configured in `setupFiles`. Consider upgrading both `react` and `launchdarkly-react-client-sdk`.