@launchdarkly/jest 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (2025-01-24)
4
+
5
+
6
+ ### Features
7
+
8
+ * Implement jest mocks for react-native. ([#535](https://github.com/launchdarkly/js-core/issues/535)) ([aee09c8](https://github.com/launchdarkly/js-core/commit/aee09c87e1810795108157dcd9ab8cfb9fe05020))
9
+
10
+ ## Changelog
11
+
12
+ All notable changes to the LaunchDarkly Jest package will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org).
package/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2024 Catamorphic, Co.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # LaunchDarkly Jest
2
+
3
+ [![NPM][jest-npm-badge]][jest-npm-link]
4
+ [![Actions Status][jest-ci-badge]][jest-ci]
5
+ [![Documentation][jest-ghp-badge]][jest-ghp-link]
6
+ [![NPM][jest-dm-badge]][jest-npm-link]
7
+ [![NPM][jest-dt-badge]][jest-npm-link]
8
+
9
+ > [!CAUTION]
10
+ > This library is a beta version and should not be considered ready for production use while this message is visible.
11
+
12
+ > **Easily unit test LaunchDarkly applications with jest** :clap:
13
+
14
+ For more information, see the [complete reference guide for unit testing](https://docs.launchdarkly.com/guides/sdk/unit-tests).
15
+
16
+ ## Installation
17
+
18
+ ```shell
19
+ # npm
20
+ npm i @launchdarkly/jest --save-dev
21
+
22
+ # yarn
23
+ yarn add -D @launchdarkly/jest
24
+ ```
25
+
26
+ Then in `jest.config.js` add `@launchdarkly/jest/{framework}` to setupFiles:
27
+
28
+ ```js
29
+ // jest.config.js
30
+ module.exports = {
31
+ // for react
32
+ setupFiles: ['@launchdarkly/jest/react'],
33
+
34
+ // for react-native
35
+ setupFiles: ['@launchdarkly/jest/react-native'],
36
+ };
37
+ ```
38
+
39
+ ## Quickstart
40
+
41
+ TODO:
42
+
43
+ ## Developing this package
44
+
45
+ ```shell
46
+ # at js-core repo root
47
+ yarn && yarn build && cd packages/tooling/jest
48
+
49
+ # run tests
50
+ yarn test
51
+ ```
52
+
53
+ ## Verifying SDK build provenance with the SLSA framework
54
+
55
+ LaunchDarkly uses the [SLSA framework](https://slsa.dev/spec/v1.0/about) (Supply-chain Levels for Software Artifacts) to help developers make their supply chain more secure by ensuring the authenticity and build integrity of our published SDK packages. To learn more, see the [provenance guide](PROVENANCE.md).
56
+
57
+ ## About LaunchDarkly
58
+
59
+ - LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can:
60
+ - Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases.
61
+ - Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?).
62
+ - Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file.
63
+ - Grant access to certain features based on user attributes, like payment plan (eg: users on the ‘gold’ plan get access to more features than users in the ‘silver’ plan).
64
+ - Disable parts of your application to facilitate maintenance, without taking everything offline.
65
+ - LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Read [our documentation](https://docs.launchdarkly.com/sdk) for a complete list.
66
+ - Explore LaunchDarkly
67
+ - [launchdarkly.com](https://www.launchdarkly.com/ 'LaunchDarkly Main Website') for more information
68
+ - [docs.launchdarkly.com](https://docs.launchdarkly.com/ 'LaunchDarkly Documentation') for our documentation and SDK reference guides
69
+ - [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ 'LaunchDarkly API Documentation') for our API documentation
70
+ - [blog.launchdarkly.com](https://blog.launchdarkly.com/ 'LaunchDarkly Blog Documentation') for the latest product updates
71
+
72
+ [jest-ci-badge]: https://github.com/launchdarkly/js-core/actions/workflows/jest.yml/badge.svg
73
+ [jest-ci]: https://github.com/launchdarkly/js-core/actions/workflows/jest.yml
74
+ [jest-npm-badge]: https://img.shields.io/npm/v/@launchdarkly/jest.svg?style=flat-square
75
+ [jest-npm-link]: https://www.npmjs.com/package/@launchdarkly/jest
76
+ [jest-ghp-badge]: https://img.shields.io/static/v1?label=GitHub+Pages&message=API+reference&color=00add8
77
+ [jest-ghp-link]: https://launchdarkly.github.io/js-core/packages/tooling/jest/docs/
78
+ [jest-dm-badge]: https://img.shields.io/npm/dm/@launchdarkly/jest.svg?style=flat-square
79
+ [jest-dt-badge]: https://img.shields.io/npm/dt/@launchdarkly/jest.svg?style=flat-square
@@ -0,0 +1,9 @@
1
+ /// <reference types="jest" />
2
+ import { LDClient, LDFlagSet } from '@launchdarkly/react-native-client-sdk';
3
+ export declare const ldClientMock: jest.Mocked<LDClient>;
4
+ export declare const mockLDProvider: jest.Mock<any, any, any>;
5
+ export declare const mockReactNativeLDClient: jest.Mock<any, any, any>;
6
+ export declare const mockUseLDClient: jest.Mock<any, any, any>;
7
+ export declare const mockFlags: (flags: LDFlagSet) => any;
8
+ export declare const resetLDMocks: () => void;
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/react-native/index.ts"],"names":[],"mappings":";AAAA,OAAO,EACL,QAAQ,EACR,SAAS,EAQV,MAAM,uCAAuC,CAAC;AAmB/C,eAAO,MAAM,YAAY,EAAE,IAAI,CAAC,MAAM,CAAC,QAAQ,CA4B9C,CAAC;AAEF,eAAO,MAAM,cAAc,0BAA0B,CAAC;AACtD,eAAO,MAAM,uBAAuB,0BAAmC,CAAC;AACxE,eAAO,MAAM,eAAe,0BAA2B,CAAC;AAWxD,eAAO,MAAM,SAAS,UAAW,SAAS,KAAG,GAgC5C,CAAC;AAEF,eAAO,MAAM,YAAY,YAExB,CAAC"}
@@ -0,0 +1,85 @@
1
+ import { LDProvider, ReactNativeLDClient, useBoolVariation, useJsonVariation, useLDClient, useNumberVariation, useStringVariation, } from '@launchdarkly/react-native-client-sdk';
2
+ jest.mock('@launchdarkly/react-native-client-sdk', () => ({
3
+ LDFlagSet: jest.fn(() => ({})),
4
+ LDProvider: jest.fn().mockImplementation(({ children }) => children),
5
+ ReactNativeLDClient: jest.fn().mockImplementation(),
6
+ useLDClient: jest.fn().mockImplementation(),
7
+ useBoolVariation: jest.fn(),
8
+ useBoolVariationDetail: jest.fn(),
9
+ useNumberVariation: jest.fn(),
10
+ useNumberVariationDetail: jest.fn(),
11
+ useStringVariation: jest.fn(),
12
+ useStringVariationDetail: jest.fn(),
13
+ useJsonVariation: jest.fn(),
14
+ useJsonVariationDetail: jest.fn(),
15
+ useTypedVariation: jest.fn(),
16
+ useTypedVariationDetail: jest.fn(),
17
+ }));
18
+ export const ldClientMock = {
19
+ allFlags: jest.fn(),
20
+ boolVariation: jest.fn(),
21
+ boolVariationDetail: jest.fn(),
22
+ close: jest.fn(),
23
+ flush: jest.fn(() => Promise.resolve({ result: true })),
24
+ // getConnectionMode: jest.fn(),
25
+ getContext: jest.fn(),
26
+ identify: jest.fn().mockResolvedValue(undefined),
27
+ jsonVariation: jest.fn(),
28
+ jsonVariationDetail: jest.fn(),
29
+ logger: {
30
+ error: jest.fn(),
31
+ warn: jest.fn(),
32
+ info: jest.fn(),
33
+ debug: jest.fn(),
34
+ },
35
+ numberVariation: jest.fn(),
36
+ numberVariationDetail: jest.fn(),
37
+ off: jest.fn(),
38
+ on: jest.fn(),
39
+ // setConnectionMode: jest.fn(),
40
+ stringVariation: jest.fn(),
41
+ stringVariationDetail: jest.fn(),
42
+ track: jest.fn(),
43
+ variation: jest.fn(),
44
+ variationDetail: jest.fn(),
45
+ addHook: jest.fn(),
46
+ };
47
+ export const mockLDProvider = LDProvider;
48
+ export const mockReactNativeLDClient = ReactNativeLDClient;
49
+ export const mockUseLDClient = useLDClient;
50
+ const mockUseBoolVariation = useBoolVariation;
51
+ const mockUseNumberVariation = useNumberVariation;
52
+ const mockUseStringVariation = useStringVariation;
53
+ const mockUseJsonVariation = useJsonVariation;
54
+ mockLDProvider.mockImplementation(({ children }) => children);
55
+ mockReactNativeLDClient.mockImplementation(() => ldClientMock);
56
+ mockUseLDClient.mockImplementation(() => ldClientMock);
57
+ export const mockFlags = (flags) => {
58
+ Object.keys(flags).forEach((key) => {
59
+ const defaultValue = flags[key];
60
+ switch (typeof defaultValue) {
61
+ case 'boolean':
62
+ mockUseBoolVariation.mockImplementation((flagKey) => flags[flagKey]);
63
+ ldClientMock.boolVariation.mockImplementation((flagKey) => flags[flagKey]);
64
+ break;
65
+ case 'number':
66
+ mockUseNumberVariation.mockImplementation((flagKey) => flags[flagKey]);
67
+ ldClientMock.numberVariation.mockImplementation((flagKey) => flags[flagKey]);
68
+ break;
69
+ case 'string':
70
+ mockUseStringVariation.mockImplementation((flagKey) => flags[flagKey]);
71
+ ldClientMock.stringVariation.mockImplementation((flagKey) => flags[flagKey]);
72
+ break;
73
+ case 'object':
74
+ mockUseJsonVariation.mockImplementation((flagKey) => flags[flagKey]);
75
+ ldClientMock.jsonVariation.mockImplementation((flagKey) => flags[flagKey]);
76
+ break;
77
+ default:
78
+ break;
79
+ }
80
+ });
81
+ };
82
+ export const resetLDMocks = () => {
83
+ jest.clearAllMocks();
84
+ };
85
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/react-native/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,UAAU,EACV,mBA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ACF,MAAM;YACR,KAAK,QAAQ;gBACX,oBAAoB,CAAC,kBAAkB,CAAC,CAAC,OAAe,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CAAW,CAAC,CAAC;gBACvF,YAAY,CAAC,aAAa,CAAC,kBAAkB,CAC3C,CAAC,OAAe,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CAAW,CAC9C,CAAC;gBACF,MAAM;YACR;gBACE,MAAM;SACT;IACH,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,YAAY,GAAG,GAAG,EAAE;IAC/B,IAAI,CAAC,aAAa,EAAE,CAAC;AACvB,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@launchdarkly/jest",
3
+ "version": "0.1.0",
4
+ "description": "Easily unit test LaunchDarkly feature flagged components with jest",
5
+ "homepage": "https://github.com/launchdarkly/js-core/tree/main/packages/tooling/jest",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/launchdarkly/js-core.git"
9
+ },
10
+ "license": "Apache-2.0",
11
+ "packageManager": "yarn@4.2.2",
12
+ "keywords": [
13
+ "launchdarkly",
14
+ "jest",
15
+ "mock",
16
+ "unit",
17
+ "test"
18
+ ],
19
+ "type": "module",
20
+ "exports": {
21
+ "./react-native": {
22
+ "types": "./dist/src/react-native/index.d.ts",
23
+ "default": "./dist/src/react-native/index.js"
24
+ },
25
+ "./react": {
26
+ "types": "./dist/src/react/index.d.ts",
27
+ "default": "./dist/src/react/index.js"
28
+ }
29
+ },
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "scripts": {
34
+ "clean": "rimraf dist",
35
+ "build": "yarn clean && tsc",
36
+ "tsw": "yarn tsc --watch",
37
+ "start": "rimraf dist && yarn tsw",
38
+ "lint": "eslint . --ext .ts",
39
+ "prettier": "prettier --write '**/*.@(js|ts|tsx|json|css)' --ignore-path ../../../.prettierignore",
40
+ "test": "NODE_OPTIONS=\"--experimental-vm-modules --no-warnings\" jest --ci --runInBand",
41
+ "coverage": "yarn test --coverage",
42
+ "check": "yarn prettier && yarn lint && yarn build && yarn test"
43
+ },
44
+ "devDependencies": {
45
+ "@trivago/prettier-plugin-sort-imports": "^4.1.1",
46
+ "@types/jest": "^29.5.0",
47
+ "@typescript-eslint/eslint-plugin": "^6.20.0",
48
+ "@typescript-eslint/parser": "^6.20.0",
49
+ "eslint": "^8.45.0",
50
+ "eslint-config-airbnb-base": "^15.0.0",
51
+ "eslint-config-airbnb-typescript": "^17.1.0",
52
+ "eslint-config-prettier": "^8.8.0",
53
+ "eslint-plugin-import": "^2.27.5",
54
+ "eslint-plugin-jest": "^27.6.3",
55
+ "eslint-plugin-prettier": "^5.0.0",
56
+ "jest": "^29.5.0",
57
+ "prettier": "^3.0.0",
58
+ "react-test-renderer": "^18.3.1",
59
+ "rimraf": "^5.0.1",
60
+ "ts-jest": "^29.1.0",
61
+ "typedoc": "0.25.0",
62
+ "typescript": "5.1.6"
63
+ },
64
+ "dependencies": {
65
+ "@launchdarkly/react-native-client-sdk": "~10.9.0",
66
+ "@testing-library/react-hooks": "^8.0.1",
67
+ "@testing-library/react-native": "^12.7.2",
68
+ "@types/lodash": "^4.17.7",
69
+ "launchdarkly-react-client-sdk": "^3.4.0",
70
+ "react": "^18.3.1"
71
+ }
72
+ }