Registry / testing / vitest-launchdarkly-mock

vitest-launchdarkly-mock

JSON →
library1.2.3jsnpmunverified

This package, `vitest-launchdarkly-mock`, provides essential utilities for unit testing React components that integrate with LaunchDarkly feature flags within a Vitest testing environment. Currently at version 1.2.3, it offers a robust solution for isolating UI components by mocking the `launchdarkly-react-client-sdk`'s `useFlags` hook and the underlying `ldClient`. The library's release cadence is generally stable, receiving updates primarily to align with new major versions of Vitest or the LaunchDarkly React SDK, rather than frequent, independent releases. Its key differentiator is its direct adaptation from `jest-launchdarkly-mock`, specifically tailored for the Vitest ecosystem, allowing developers to leverage a familiar mocking pattern when transitioning or developing Vitest-based tests. It enables developers to precisely control feature flag states for individual tests and assert interactions with the LaunchDarkly client, ensuring reliable and repeatable test outcomes without external dependencies on the LaunchDarkly service itself. The package is focused on the React SDK integration, making it a specialized tool for React applications.

npm install vitest-launchdarkly-mock
INSTALL
IMPORT
SIG · VITEST-LAUNCHDARKL
V
vitest-launchdarkly-mock
testingjavascriptv1.2.3
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

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

mockFlags
✓ import { mockFlags } from 'vitest-launchdarkly-mock'
✗ const { mockFlags } = require('vitest-launchdarkly-mock')
This package is primarily ESM-first. For CommonJS environments, ensure proper transpilation or use dynamic imports.
ldClientMock
✓ import { ldClientMock } from 'vitest-launchdarkly-mock'
✗ const { ldClientMock } = require('vitest-launchdarkly-mock')
Provides a Vitest mock of the LaunchDarkly client, allowing assertion of client method calls.
resetLDMocks
✓ import { resetLDMocks } from 'vitest-launchdarkly-mock'
✗ const { resetLDMocks } = require('vitest-launchdarkly-mock')
Essential for clearing mock states between tests to prevent test pollution; typically called in `beforeEach`.

This quickstart demonstrates how to mock LaunchDarkly flags and client interactions for a React component using `vitest-launchdarkly-mock`, ensuring isolated and reliable unit tests.

import { render, fireEvent } from '@testing-library/react'; import { describe, beforeEach, test, expect } from 'vitest'; import { mockFlags, ldClientMock, resetLDMocks } from 'vitest-launchdarkly-mock'; // A dummy React component for demonstration, in a real app this would use useFlags() const Button = () => { // Assume 'devTestFlag' determines some UI aspect, mocked by mockFlags // Simplified for this quickstart; actual implementation would use useFlags() const isFeatureEnabled = true; // Value controlled by mockFlags in test return ( <button data-testid="test-button" onClick={() => ldClientMock.identify({ key: 'aa0ceb', name: 'Test User' })}> {isFeatureEnabled ? 'Feature On' : 'Feature Off'} </button> ); }; describe('Button component with LaunchDarkly mocks', () => { beforeEach(() => { // Reset mocks before each test to ensure test isolation. resetLDMocks(); }); test('should display button when devTestFlag is enabled', () => { // Arrange: Mock 'devTestFlag' to be true. mockFlags({ devTestFlag: true }); // Act: Render the component under test. const { getByTestId } = render(<Button />); // Assert: Verify the button is rendered and visible. expect(getByTestId('test-button')).toBeInTheDocument(); }); test('should call ldClientMock.identify when button is clicked', () => { // Arrange: Mock the flag state and render the component. mockFlags({ 'dev-test-flag': true }); // Act: Render the button and simulate a user click. const { getByTestId } = render(<Button />); fireEvent.click(getByTestId('test-button')); // Assert: Verify that ldClientMock.identify was called with the correct arguments. expect(ldClientMock.identify).toBeCalledWith({ key: 'aa0ceb', name: 'Test User' }); }); });
Debug
Known issues
gotchaThis package is exclusively designed for use with the LaunchDarkly React Client SDK (`launchdarkly-react-client-sdk`).
fix
Ensure your project uses `launchdarkly-react-client-sdk` for client-side React applications. This mock is not compatible with other LaunchDarkly SDKs (e.g., Node.js server-side SDKs).
affects: >=1.0.0
gotchaConfiguration in `vite.config.ts` `setupFiles` is mandatory for this package to initialize its global mocks correctly.
fix
Add `'./node_modules/vitest-launchdarkly-mock/dist/index.js'` to the `test.setupFiles` array in your `vite.config.ts` to ensure global mocks are loaded before your tests run.
affects: >=1.0.0
gotchaMocks are global and will persist their state between tests unless explicitly reset, which can lead to flaky or unpredictable test results.
fix
Always call `resetLDMocks()` within a `beforeEach` hook inside your test suites. This ensures a clean and isolated mocking state for each individual test case.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: mockFlags is not defined
The Vitest setup file for `vitest-launchdarkly-mock` was not correctly loaded or configured.
fix
Verify that `'./node_modules/vitest-launchdarkly-mock/dist/index.js'` is included in the `test.setupFiles` array within your `vite.config.ts`.
TypeError: Cannot read properties of undefined (reading 'identify')
The `ldClientMock` object or its methods were accessed before `vitest-launchdarkly-mock` was properly initialized, or after it was reset without subsequent re-initialization.
fix
Ensure that `vitest-launchdarkly-mock` is correctly configured in Vitest's `setupFiles` and that `resetLDMocks()` is called within a `beforeEach` block to re-initialize mocks for each test.
Invariant Violation: You must use the LaunchDarklyProvider
Your React component attempts to use LaunchDarkly hooks (e.g., `useFlags`) without being rendered within a `LaunchDarklyProvider` context, even when `vitest-launchdarkly-mock` is active.
fix
While `vitest-launchdarkly-mock` should handle most `useFlags` mocks, ensure that `vitest-launchdarkly-mock` is correctly set up via `setupFiles`. If the issue persists with complex components, you might need to mock `LaunchDarklyProvider` explicitly in your test renderer or check for other context dependencies.
Upgrade
Version history
1.2.3latest on npm
Audit
Dependencies
launchdarkly-react-client-sdkrequiredRequired peer dependency for mocking LaunchDarkly React client SDK behavior.
reactrequiredRequired peer dependency for testing React components.
react-domrequiredRequired peer dependency for rendering React components in tests.
vitestrequiredRequired peer dependency for the testing framework.
Agent activity
14 hits · last 30 days
node
12
Resources
vitest-launchdarkly-mock — npm install vitest-launchdarkly-mock · libregistry