Home
Softono

Jest Launchdarkly Mock

Open source Apache-2.0 TypeScript
29
Stars
21
Forks
20
Issues
30
Watchers
2 months
Last Commit

 About Jest Launchdarkly Mock

Easily unit test LaunchDarkly feature flagged components with jest

Platforms

Web Self-hosted

Languages

TypeScript

Links

Need Help Installing Jest Launchdarkly Mock?

We provide expert installation service for this software. Our team will install, configure, and secure Jest Launchdarkly Mock on your server. plans start at just $30.

Jest Launchdarkly Mock

View on GitHub

jest-launchdarkly-mock

npm version npm downloads License PRs Welcome

Star on GitHub Tweet

[!CAUTION] This package is only compatible with the Launchdarkly React SDK. The team is actively working on a new major version of the React SDK in our js-core monorepo. At this time, we are NOT planning to update this package to be compatible with the new major version.

Easily unit test LaunchDarkly feature flagged components with jest :clap:

Installation

yarn add -D jest-launchdarkly-mock

or

npm install jest-launchdarkly-mock --save-dev

Then in jest.config.js add jest-launchdarkly-mock to setupFiles:

// jest.config.js
module.exports = {
  setupFiles: ['jest-launchdarkly-mock'],
}

Usage

Use the only 3 apis for test cases:

  • mockFlags(flags: LDFlagSet): mock flags at the start of each test case. Only mocks flags returned by the useFlags hook.

  • ldClientMock: a jest mock of the ldClient. All methods of this object are jest mocks.

  • resetLDMocks : resets both mockFlags and ldClientMock.

Example

import { mockFlags, ldClientMock, resetLDMocks } from 'jest-launchdarkly-mock'

describe('button', () => {
  beforeEach(() => {
    // reset before each test case
    resetLDMocks()
  })

  test('flag on', () => {
      // arrange
      // You can use the original unchanged case, kebab-case, camelCase or snake_case keys.
      mockFlags({ devTestFlag: true })
  
      // act
      const { getByTestId } = render(<Button />)

      // assert
      expect(getByTestId('test-button')).toBeTruthy()
    })

  test('identify', () => {
    // arrange
    mockFlags({ 'dev-test-flag': true })
    
    // act
    const { getByTestId } = render(<Button />)
    fireEvent.click(getByTestId('test-button'))

    // assert: identify gets called
    expect(ldClientMock.identify).toBeCalledWith({ key: 'aa0ceb' })
  })
})