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-mockVerified import paths — ran on the pinned version, not inferred.
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.
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).
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.
Always call `resetLDMocks()` within a `beforeEach` hook inside your test suites. This ensures a clean and isolated mocking state for each individual test case.
Verify that `'./node_modules/vitest-launchdarkly-mock/dist/index.js'` is included in the `test.setupFiles` array within your `vite.config.ts`.
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.
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.