0.2.3 • Published 2 years ago

jest-ld-mock v0.2.3

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

jest-launchdarkly-mock

npm version npm downloads License PRs Welcome

Star on GitHub Tweet

Installation

yarn -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, Options?: mockFlagsOptions): mock flags at the start of each test case.
Options
nametypedefaultdescription
skipFormatting(optional)booleanfalseIf true, flags will not be formatted to camelCase or kebab-case
  • 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 kebab-case or camelCase 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' })
  })
})