Skip to content

Commit 48cac54

Browse files
authored
chore: Change testing to use cjs. (#636)
BEGIN_COMMIT_OVERRIDE chore: Change browser packages testing to use CJS. feat: Vendor escapeStringRegexp to simplify builds. END_COMMIT_OVERRIDE SDK-816
1 parent ae2b5bb commit 48cac54

File tree

11 files changed

+105
-27
lines changed

11 files changed

+105
-27
lines changed

packages/sdk/browser/__tests__/BrowserDataManager.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { jest } from '@jest/globals';
2-
import { TextEncoder } from 'node:util';
32

43
import {
54
ApplicationTags,
@@ -26,8 +25,6 @@ import LocalStorage from '../src/platform/LocalStorage';
2625
import { MockHasher } from './MockHasher';
2726
import { goodBootstrapData } from './testBootstrapData';
2827

29-
global.TextEncoder = TextEncoder;
30-
3128
function mockResponse(value: string, statusCode: number) {
3229
const response: Response = {
3330
headers: {

packages/sdk/browser/__tests__/platform/BrowserEncoding.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
// TextEncoder should be part of jsdom, but it is not. So we can import it from node in the tests.
2-
import { TextEncoder } from 'node:util';
3-
41
import BrowserEncoding from '../../src/platform/BrowserEncoding';
52

6-
global.TextEncoder = TextEncoder;
7-
83
it('can base64 a basic ASCII string', () => {
94
const encoding = new BrowserEncoding();
105
expect(encoding.btoa('toaster')).toEqual('dG9hc3Rlcg==');

packages/sdk/browser/__tests__/platform/BrowserHasher.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
// TextEncoder should be part of jsdom, but it is not. So we can import it from node in the tests.
21
import { webcrypto } from 'node:crypto';
3-
import { TextEncoder } from 'node:util';
42

53
import BrowserHasher from '../../src/platform/BrowserHasher';
64

7-
global.TextEncoder = TextEncoder;
8-
95
// Crypto is injectable as it is also not correctly available with the combination of node and jsdom.
106

117
/**

packages/sdk/browser/jest.config.js

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"verbose": true,
3+
"testEnvironment": "jest-environment-jsdom",
4+
"testPathIgnorePatterns": ["./dist", "./src"],
5+
"testMatch": ["**.test.ts"],
6+
"setupFiles": ["./setup-jest.js"],
7+
"transform": {
8+
"^.+\\.ts$": [
9+
"ts-jest",
10+
{
11+
"tsConfig": "tsconfig.test.json"
12+
}
13+
],
14+
"^.+.tsx?$": ["ts-jest", {}]
15+
}
16+
}

packages/sdk/browser/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@
3030
"build": "tsc --noEmit && rollup -c rollup.config.js",
3131
"lint": "eslint . --ext .ts,.tsx",
3232
"prettier": "prettier --write '**/*.@(js|ts|tsx|json|css)' --ignore-path ../../../.prettierignore",
33-
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest --runInBand",
33+
"test": "npx jest --runInBand",
3434
"coverage": "yarn test --coverage",
3535
"check": "yarn prettier && yarn lint && yarn build && yarn test"
3636
},
3737
"dependencies": {
38-
"@launchdarkly/js-client-sdk-common": "1.10.0",
39-
"escape-string-regexp": "^5.0.0",
40-
"rollup-plugin-visualizer": "^5.12.0"
38+
"@launchdarkly/js-client-sdk-common": "1.10.0"
4139
},
4240
"devDependencies": {
4341
"@jest/globals": "^29.7.0",
@@ -62,6 +60,7 @@
6260
"prettier": "^3.0.0",
6361
"rimraf": "^5.0.5",
6462
"rollup": "^3.23.0",
63+
"rollup-plugin-visualizer": "^5.12.0",
6564
"ts-jest": "^29.1.1",
6665
"typedoc": "0.25.0",
6766
"typescript": "^5.5.3"

packages/sdk/browser/setup-jest.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { TextEncoder, TextDecoder } = require('node:util');
2+
const crypto = require('node:crypto');
3+
4+
global.TextEncoder = TextEncoder;
5+
6+
Object.assign(window, { TextDecoder, TextEncoder });
7+
8+
// Based on:
9+
// https://stackoverflow.com/a/71750830
10+
11+
Object.defineProperty(global.self, 'crypto', {
12+
value: {
13+
getRandomValues: (arr) => crypto.randomBytes(arr.length),
14+
subtle: {
15+
digest: (algorithm, data) => {
16+
return new Promise((resolve) =>
17+
resolve(
18+
crypto.createHash(algorithm.toLowerCase().replace('-', '')).update(data).digest(),
19+
),
20+
);
21+
},
22+
},
23+
},
24+
});

packages/sdk/browser/src/goals/GoalTracker.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import escapeStringRegexp from 'escape-string-regexp';
2-
31
import {
42
addDocumentEventListener,
53
getHref,
64
getLocationHash,
75
getLocationSearch,
86
querySelectorAll,
97
} from '../BrowserApi';
8+
import escapeStringRegexp from '../vendor/escapeStringRegexp';
109
import { ClickGoal, Goal, Matcher } from './Goals';
1110

1211
type EventHandler = (goal: Goal) => void;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// From here: https://github.com/sindresorhus/escape-string-regexp
2+
3+
// This is vendored to reduce the complexity of the built and test setup.
4+
// The NPM package for escape-string-regexp is ESM only, and that introduces
5+
// complexity to the jest configuration which works best/easiest with CJS.
6+
7+
/**
8+
* MIT License
9+
*
10+
* Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)
11+
*
12+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
*
18+
*/
19+
20+
/**
21+
* Escape regular expression especial characters.
22+
*
23+
* @param string The regular expression to escape.
24+
* @returns The escaped expression.
25+
*/
26+
export default function escapeStringRegexp(string: string) {
27+
if (typeof string !== 'string') {
28+
throw new TypeError('Expected a string');
29+
}
30+
31+
// Escape characters with special meaning either inside or outside character sets.
32+
// Use a simple backslash escape when it’s always valid, and a `\xnn` escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar.
33+
return string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
34+
}

packages/sdk/browser/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"node_modules",
3030
"contract-tests",
3131
"babel.config.js",
32-
"jest.config.js",
3332
"jestSetupFile.ts",
3433
"**/*.test.ts*"
3534
]

0 commit comments

Comments
 (0)