Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/launchdarkly/jest-launchdarkly-mock
Easily unit test LaunchDarkly feature flagged components with jest
https://github.com/launchdarkly/jest-launchdarkly-mock
feature feature-flags feature-toggles flags jest launchdarkly mock test unit
Last synced: 13 days ago
JSON representation
Easily unit test LaunchDarkly feature flagged components with jest
- Host: GitHub
- URL: https://github.com/launchdarkly/jest-launchdarkly-mock
- Owner: launchdarkly
- License: apache-2.0
- Created: 2020-09-22T20:41:49.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-10-23T18:30:56.000Z (20 days ago)
- Last Synced: 2024-10-25T02:44:10.529Z (19 days ago)
- Topics: feature, feature-flags, feature-toggles, flags, jest, launchdarkly, mock, test, unit
- Language: TypeScript
- Homepage:
- Size: 224 MB
- Stars: 25
- Watchers: 29
- Forks: 19
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# jest-launchdarkly-mock
[![npm version](https://img.shields.io/npm/v/jest-launchdarkly-mock.svg)](https://www.npmjs.com/package/jest-launchdarkly-mock)
[![npm downloads](https://img.shields.io/npm/dm/jest-launchdarkly-mock.svg)](https://www.npmjs.com/package/jest-launchdarkly-mock)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com/)[![Star on GitHub](https://img.shields.io/github/stars/launchdarkly/jest-launchdarkly-mock?style=social)](https://github.com/launchdarkly/jest-launchdarkly-mock/stargazers)
[![Tweet](https://img.shields.io/twitter/url/https/github.com/launchdarkly/jest-launchdarkly-mock.svg?style=social)](https://twitter.com/intent/tweet?text=Check%20out%20jest-launchdarkly-mock%20by%20%40launchdarkly%20https%3A%2F%2Fgithub.com%2Flaunchdarkly%2Fjest-launchdarkly-mock%20%F0%9F%91%8D)> **Easily unit test LaunchDarkly feature flagged components with jest** :clap:
This package is only compatible with the react sdk.
## Installation
```bash
yarn add -D jest-launchdarkly-mock
```or
```bash
npm install jest-launchdarkly-mock --save-dev
```Then in `jest.config.js` add jest-launchdarkly-mock to setupFiles:
```js
// 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](https://launchdarkly.github.io/js-client-sdk/interfaces/_launchdarkly_js_client_sdk_.ldclient.html). All
methods of this object are jest mocks.* `resetLDMocks` : resets both mockFlags and ldClientMock.
## Example
```tsx
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()// assert
expect(getByTestId('test-button')).toBeTruthy()
})test('identify', () => {
// arrange
mockFlags({ 'dev-test-flag': true })
// act
const { getByTestId } = render()
fireEvent.click(getByTestId('test-button'))// assert: identify gets called
expect(ldClientMock.identify).toBeCalledWith({ key: 'aa0ceb' })
})
})```