Registry / testing / jest-launchdarkly-mock

jest-launchdarkly-mock

JSON →
library2.2.1jsnpmunverified

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-mock
INSTALL
IMPORT
SIG · JEST-LAUNCHDARKLY-
J
jest-launchdarkly-mock
testingjavascriptv2.2.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mockFlags
import { mockFlags } from 'jest-launchdarkly-mock'
const { mockFlags } = require('jest-launchdarkly-mock')
Used to set specific flag values for a test case. Ensure Jest's `setupFiles` is configured correctly.
ldClientMock
import { ldClientMock } from 'jest-launchdarkly-mock'
const ldClientMock = require('jest-launchdarkly-mock').ldClientMock
A Jest mock object for the LaunchDarkly client, allowing assertion of client method calls like `identify`.
resetLDMocks
import { resetLDMocks } from 'jest-launchdarkly-mock'
import resetLDMocks from 'jest-launchdarkly-mock/reset'
Essential to call in `beforeEach` to clear mock states between tests, preventing contamination.

Demonstrates how to mock feature flag states and assert LaunchDarkly client interactions within a Jest test using `mockFlags`, `ldClientMock`, and `resetLDMocks`.

import { render, fireEvent } from '@testing-library/react'; import { mockFlags, ldClientMock, resetLDMocks } from 'jest-launchdarkly-mock'; import React from 'react'; // Assume Button component is defined elsewhere and uses useFlags or withLDConsumer const Button = () => { // In a real app, this would use LaunchDarkly hooks/HOCs const devTestFlag = true; // Simplified for this quickstart const onClick = () => { // Simulate identify call if (ldClientMock && ldClientMock.identify) { ldClientMock.identify({ key: 'user-key-123' }); } }; return ( <button data-testid="test-button" onClick={onClick}> {devTestFlag ? 'Feature On' : 'Feature Off'} </button> ); }; describe('Button component with LaunchDarkly mocks', () => { beforeEach(() => { // Clears all previous flag mocks and client mock calls resetLDMocks(); }); test('should show "Feature On" when devTestFlag is true', () => { // Arrange: Set a specific flag state mockFlags({ devTestFlag: true }); // Act: Render the component const { getByTestId } = render(<Button />); // Assert: Check the component's output based on the mocked flag expect(getByTestId('test-button')).toHaveTextContent('Feature On'); }); test('should call ldClientMock.identify on button click', () => { // Arrange: Set flag and ensure identify mock is available mockFlags({ devTestFlag: false }); // Act: Render and click the button const { getByTestId } = render(<Button />); fireEvent.click(getByTestId('test-button')); // Assert: Verify that the ldClientMock.identify function was called with the expected arguments expect(ldClientMock.identify).toHaveBeenCalledWith({ key: 'user-key-123' }); }); });
Debug
Known issues
breakingVersion 2.0.0 introduced support for LaunchDarkly's 'custom contexts' feature, which replaces the older 'users' concept. If you're upgrading from v1.x, you will need to adjust your tests to use the new context structure where applicable, aligning with the `launchdarkly-react-client-sdk` v3.0.0 changes.
fix
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.
affects: >=2.0.0
breakingPrior to v2.1.0, the `useFlags` mock might have returned different default values. Since v2.1.0, `useFlags` now returns an empty object by default, which aligns with its actual implementation in the SDK.
fix
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.
affects: >=2.1.0
gotchajest-launchdarkly-mock is designed specifically for mocking components that interact with the `launchdarkly-react-client-sdk`. It is not compatible with the plain `launchdarkly-js-client-sdk` or other LaunchDarkly SDKs.
fix
Verify that your application uses `launchdarkly-react-client-sdk`. If using the plain JS SDK, a different mocking strategy or library will be required.
affects: >=1.0.0
gotchaIt is crucial to include `jest-launchdarkly-mock` in Jest's `setupFiles` configuration. Without this, the global mocks will not be properly initialized, leading to errors like `ldClientMock` or `mockFlags` being undefined.
fix
Add `'jest-launchdarkly-mock'` to the `setupFiles` array in your `jest.config.js` or equivalent Jest configuration file.
affects: >=1.0.0
gotchaThe peer dependency for React was updated to `>=17.0.2` in versions 1.0.5 and 1.0.6. If you are using an older version of React, you might encounter peer dependency warnings or runtime issues.
fix
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.
affects: >=1.0.5
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'identify')
The `ldClientMock` was not properly initialized or `ldClientMock.identify` was accessed before a test ran or before `jest-launchdarkly-mock` was loaded.
fix
Ensure `jest-launchdarkly-mock` is listed in your `jest.config.js` `setupFiles` array and `resetLDMocks()` is called in a `beforeEach` hook.
Error: Uncaught [TypeError: Cannot read properties of undefined (reading 'useFlags')]
The React context or hooks related to LaunchDarkly were not mocked correctly, likely due to `jest-launchdarkly-mock` not being configured in Jest's `setupFiles`.
fix
Verify that `jest-launchdarkly-mock` is present in your Jest `setupFiles` configuration. Also, ensure your component is rendered within the scope of the mock.
Invariant Violation: Invalid hook call. Hooks can only be called inside of the body of a function component.
This error can occur if the `launchdarkly-react-client-sdk` is not properly mocked or if React versions are mismatched, leading to incorrect hook behavior.
fix
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`.
Upgrade
Version history
2.2.1latest on npm
Audit
Dependencies
launchdarkly-react-client-sdkrequiredPeer dependency for integration with LaunchDarkly in React applications.
reactrequiredPeer dependency for React applications. Requires React 17 or newer.
Agent activity
4 hits · last 30 days
node
4
Resources