@launchdarkly/jest
v1.0.0
Published
Easily unit test LaunchDarkly feature flagged components with jest
Maintainers
Readme
LaunchDarkly Jest
Easily unit test LaunchDarkly feature flagged applications with jest
For more information, see the complete reference guide for unit testing.
Installation
yarn add -D @launchdarkly/jestor
npm install @launchdarkly/jest --save-devThen in jest.config.js add @launchdarkly/jest/{framework} to setupFiles:
// jest.config.js
module.exports = {
// for react-native
setupFiles: ['@launchdarkly/jest/react-native'],
};Usage
Use these 3 APIs for your test cases:
mockFlags(flags: LDFlagSet): Mock flags at the start of each test case. Only mocks flags returned by theuseFlagshook.getLDClient(): Returns a jest mock of the LDClient. All methods of this object are jest mocks.resetLDMocks(): Resets both mockFlags and getLDClient mocks.
Example
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import { mockFlags, resetLDMocks, getLDClient } from '@launchdarkly/jest/react-native';
import Welcome from './Welcome';
describe('Welcome component', () => {
afterEach(() => {
// reset before each test case
resetLDMocks();
});
test('evaluates a boolean flag', () => {
// arrange
// You can use camelCase, kebab-case, or snake_case keys
mockFlags({ 'my-boolean-flag': true });
// act
const { getByText } = render(<Welcome />);
// assert
expect(getByText('Flag value is true')).toBeTruthy();
});
test('captures a track call', () => {
// arrange
mockFlags({ myBooleanFlag: true });
const client = getLDClient();
// act
const { getByTestId } = render(<Welcome />);
fireEvent.press(getByTestId('track-button'));
// assert: track gets called
expect(client.track).toHaveBeenCalledWith('event-name', { foo: 'bar' });
expect(client.track).toHaveBeenCalledTimes(1);
});
});Developing this package
# at js-core repo root
yarn && yarn build && cd packages/tooling/jest
# run tests
yarn testNote
LaunchDarkly plans to support test data sources for the React Native and other client-side SDKs in the future. Once this feature is avaliable, we will deprecate this package.
Verifying SDK build provenance with the SLSA framework
LaunchDarkly uses the SLSA framework (Supply-chain Levels for Software Artifacts) to help developers make their supply chain more secure by ensuring the authenticity and build integrity of our published SDK packages. To learn more, see the provenance guide.
About LaunchDarkly
- LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can:
- Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases.
- Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?).
- Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file.
- Grant access to certain features based on user attributes, like payment plan (eg: users on the ‘gold’ plan get access to more features than users in the ‘silver’ plan).
- Disable parts of your application to facilitate maintenance, without taking everything offline.
- LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Read our documentation for a complete list.
- Explore LaunchDarkly
- launchdarkly.com for more information
- docs.launchdarkly.com for our documentation and SDK reference guides
- apidocs.launchdarkly.com for our API documentation
- blog.launchdarkly.com for the latest product updates
