1.1.0 • Published 1 year ago

vitest-launchdarkly-mock v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

vitest-launchdarkly-mock

npm version npm downloads License PRs Welcome

Star on GitHub

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

This package is a rip-off from jest-launchdarkly-mock, just adapted for Vitest.

This package is only compatible with React SDK.

Installation

yarn add -D vitest-launchdarkly-mock

or

npm install vitest-launchdarkly-mock --save-dev

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

// vite.config.ts

export default defineConfig(() => {
    return {
        test: {
            setupFiles: ['./node_modules/vitest-launchdarkly-mock/dist/index.js'],
        }
    }
});

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 Vitest mock of the ldClient. All methods of this object are Vitest mocks.

  • resetLDMocks : resets both mockFlags and ldClientMock.

Example

import { mockFlags, ldClientMock, resetLDMocks } from 'vitest-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' })
  })
})
1.1.0

1 year ago

1.0.1-types.2

1 year ago

1.0.1-types.3

1 year ago

1.0.1-types.0

1 year ago

1.0.1-types.1

1 year ago

1.0.0

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago

0.0.0

2 years ago

0.1.0

2 years ago