Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-launchdarkly-mock

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-launchdarkly-mock

Easily unit test LaunchDarkly feature flagged components with jest

  • 2.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
107K
increased by1.9%
Maintainers
1
Weekly downloads
 
Created
Source

jest-launchdarkly-mock

npm version npm downloads License PRs Welcome

Star on GitHub Tweet

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

This package is only compatible with the react sdk.

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' })
  })
})

Keywords

FAQs

Package last updated on 06 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc