Skip to content

Commit 4d568b4

Browse files
authored
Merge branch 'main' into skz/react-native-rendering
2 parents 2086a07 + 3de0b30 commit 4d568b4

File tree

2 files changed

+64
-31
lines changed

2 files changed

+64
-31
lines changed

.github/workflows/server-node.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ jobs:
3838
- name: Launch the test service in the background
3939
run: yarn contract-test-service 2>&1 &
4040
- name: Clone and run contract tests from feat/fdv2 branch
41+
# NOTE: This is a temporary workaround to run the contract tests from the feat/fdv2 branch.
42+
# The commit hash is a few commits before the HEAD of the feat/fdv2 branch. We do this because
43+
# the HEAD of the feat/fdv2 branch requires the synchronizers be passed as a list which is not
44+
# implemented yet. (SDK-1798)
4145
run: |
4246
mkdir -p /tmp/sdk-test-harness
4347
git clone https://github.com/launchdarkly/sdk-test-harness.git /tmp/sdk-test-harness
4448
cp ./contract-tests/testharness-suppressions-fdv2.txt /tmp/sdk-test-harness/testharness-suppressions-fdv2.txt
4549
cd /tmp/sdk-test-harness
46-
git checkout feat/fdv2
50+
git checkout de833af990da23a89b66c5366809b5be8c27e3f8
4751
go build -o test-harness .
4852
./test-harness -url http://localhost:8000 -debug --skip-from=testharness-suppressions-fdv2.txt
4953
env:

packages/tooling/jest/README.md

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,24 @@
66
[![NPM][jest-dm-badge]][jest-npm-link]
77
[![NPM][jest-dt-badge]][jest-npm-link]
88

9-
> [!CAUTION]
10-
> This library is a beta version and should not be considered ready for production use while this message is visible.
119

12-
> **Easily unit test LaunchDarkly applications with jest** :clap:
10+
11+
**Easily unit test LaunchDarkly feature flagged applications with jest**
1312

1413
For more information, see the [complete reference guide for unit testing](https://docs.launchdarkly.com/guides/sdk/unit-tests).
1514

1615
## Installation
1716

18-
```shell
19-
# npm
20-
npm i @launchdarkly/jest --save-dev
21-
22-
# yarn
17+
```bash
2318
yarn add -D @launchdarkly/jest
2419
```
2520

21+
or
22+
23+
```bash
24+
npm install @launchdarkly/jest --save-dev
25+
```
26+
2627
Then in `jest.config.js` add `@launchdarkly/jest/{framework}` to setupFiles:
2728

2829
```js
@@ -33,34 +34,59 @@ module.exports = {
3334
};
3435
```
3536

36-
## Quickstart
37+
## Usage
38+
39+
Use these 3 APIs for your test cases:
40+
41+
- `mockFlags(flags: LDFlagSet)`: Mock flags at the start of each test case. Only mocks flags returned by the `useFlags` hook.
3742

38-
// Welcome.test.tsx
43+
- `getLDClient()`: Returns 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.
44+
45+
- `resetLDMocks()`: Resets both mockFlags and getLDClient mocks.
46+
47+
## Example
48+
49+
```tsx
3950
import React from 'react';
40-
import { render } from '@testing-library/react-native';
41-
import {
42-
mockFlags,
43-
resetLDMocks,
44-
getLDClient,
45-
} from '@launchdarkly/js-core/tooling/jest';
51+
import { render, fireEvent } from '@testing-library/react-native';
52+
import { mockFlags, resetLDMocks, getLDClient } from '@launchdarkly/jest/react-native';
4653
import Welcome from './Welcome';
4754

48-
afterEach(() => {
49-
resetLDMocks();
50-
});
51-
52-
test('evaluates a boolean flag', () => {
53-
mockFlags({ 'my-boolean-flag': true });
54-
const { getByText } = render(<Welcome />);
55-
expect(getByText('Flag value is true')).toBeTruthy();
55+
describe('Welcome component', () => {
56+
afterEach(() => {
57+
// reset before each test case
58+
resetLDMocks();
59+
});
60+
61+
test('evaluates a boolean flag', () => {
62+
// arrange
63+
// You can use camelCase, kebab-case, or snake_case keys
64+
mockFlags({ 'my-boolean-flag': true });
65+
66+
// act
67+
const { getByText } = render(<Welcome />);
68+
69+
// assert
70+
expect(getByText('Flag value is true')).toBeTruthy();
71+
});
72+
73+
test('captures a track call', () => {
74+
// arrange
75+
mockFlags({ myBooleanFlag: true });
76+
const client = getLDClient();
77+
78+
// act
79+
const { getByTestId } = render(<Welcome />);
80+
fireEvent.press(getByTestId('track-button'));
81+
82+
// assert: track gets called
83+
expect(client.track).toHaveBeenCalledWith('event-name', { foo: 'bar' });
84+
expect(client.track).toHaveBeenCalledTimes(1);
85+
});
5686
});
87+
```
5788

58-
test('captures a track call', () => {
59-
const client = getLDClient(); // mocked client from LD jest tooling
60-
client.track('event-name', { foo: 'bar' });
61-
expect(client.track).toHaveBeenCalledWith('event-name', { foo: 'bar' });
62-
expect(client.track).toHaveBeenCalledTimes(1);
63-
});
89+
---
6490

6591
## Developing this package
6692

@@ -71,6 +97,9 @@ yarn && yarn build && cd packages/tooling/jest
7197
# run tests
7298
yarn test
7399
```
100+
## Note
101+
102+
LaunchDarkly plans to support [test data sources](https://launchdarkly.com/docs/sdk/features/test-data-sources) for the React Native and other client-side SDKs in the future. Once this feature is avaliable, we will deprecate this package.
74103

75104
## Verifying SDK build provenance with the SLSA framework
76105

0 commit comments

Comments
 (0)