diff --git a/packages/sdk/browser/__tests__/BrowserDataManager.test.ts b/packages/sdk/browser/__tests__/BrowserDataManager.test.ts index 36b58b56b..628602acf 100644 --- a/packages/sdk/browser/__tests__/BrowserDataManager.test.ts +++ b/packages/sdk/browser/__tests__/BrowserDataManager.test.ts @@ -1,5 +1,4 @@ import { jest } from '@jest/globals'; -import { TextEncoder } from 'node:util'; import { ApplicationTags, @@ -26,8 +25,6 @@ import LocalStorage from '../src/platform/LocalStorage'; import { MockHasher } from './MockHasher'; import { goodBootstrapData } from './testBootstrapData'; -global.TextEncoder = TextEncoder; - function mockResponse(value: string, statusCode: number) { const response: Response = { headers: { diff --git a/packages/sdk/browser/__tests__/platform/BrowserEncoding.test.ts b/packages/sdk/browser/__tests__/platform/BrowserEncoding.test.ts index a24865609..ad81b83bf 100644 --- a/packages/sdk/browser/__tests__/platform/BrowserEncoding.test.ts +++ b/packages/sdk/browser/__tests__/platform/BrowserEncoding.test.ts @@ -1,10 +1,5 @@ -// TextEncoder should be part of jsdom, but it is not. So we can import it from node in the tests. -import { TextEncoder } from 'node:util'; - import BrowserEncoding from '../../src/platform/BrowserEncoding'; -global.TextEncoder = TextEncoder; - it('can base64 a basic ASCII string', () => { const encoding = new BrowserEncoding(); expect(encoding.btoa('toaster')).toEqual('dG9hc3Rlcg=='); diff --git a/packages/sdk/browser/__tests__/platform/BrowserHasher.test.ts b/packages/sdk/browser/__tests__/platform/BrowserHasher.test.ts index cb143f119..3fc202a5b 100644 --- a/packages/sdk/browser/__tests__/platform/BrowserHasher.test.ts +++ b/packages/sdk/browser/__tests__/platform/BrowserHasher.test.ts @@ -1,11 +1,7 @@ -// TextEncoder should be part of jsdom, but it is not. So we can import it from node in the tests. import { webcrypto } from 'node:crypto'; -import { TextEncoder } from 'node:util'; import BrowserHasher from '../../src/platform/BrowserHasher'; -global.TextEncoder = TextEncoder; - // Crypto is injectable as it is also not correctly available with the combination of node and jsdom. /** diff --git a/packages/sdk/browser/jest.config.js b/packages/sdk/browser/jest.config.js deleted file mode 100644 index e6a722469..000000000 --- a/packages/sdk/browser/jest.config.js +++ /dev/null @@ -1,8 +0,0 @@ -export default { - extensionsToTreatAsEsm: ['.ts'], - verbose: true, - preset: 'ts-jest/presets/default-esm', - testEnvironment: 'jest-environment-jsdom', - testPathIgnorePatterns: ['./dist', './src'], - testMatch: ['**.test.ts'], -}; diff --git a/packages/sdk/browser/jest.config.json b/packages/sdk/browser/jest.config.json new file mode 100644 index 000000000..6d2e223cd --- /dev/null +++ b/packages/sdk/browser/jest.config.json @@ -0,0 +1,16 @@ +{ + "verbose": true, + "testEnvironment": "jest-environment-jsdom", + "testPathIgnorePatterns": ["./dist", "./src"], + "testMatch": ["**.test.ts"], + "setupFiles": ["./setup-jest.js"], + "transform": { + "^.+\\.ts$": [ + "ts-jest", + { + "tsConfig": "tsconfig.test.json" + } + ], + "^.+.tsx?$": ["ts-jest", {}] + } +} diff --git a/packages/sdk/browser/package.json b/packages/sdk/browser/package.json index 1973107c3..2b05c0efd 100644 --- a/packages/sdk/browser/package.json +++ b/packages/sdk/browser/package.json @@ -30,14 +30,12 @@ "build": "tsc --noEmit && rollup -c rollup.config.js", "lint": "eslint . --ext .ts,.tsx", "prettier": "prettier --write '**/*.@(js|ts|tsx|json|css)' --ignore-path ../../../.prettierignore", - "test": "NODE_OPTIONS=--experimental-vm-modules npx jest --runInBand", + "test": "npx jest --runInBand", "coverage": "yarn test --coverage", "check": "yarn prettier && yarn lint && yarn build && yarn test" }, "dependencies": { - "@launchdarkly/js-client-sdk-common": "1.10.0", - "escape-string-regexp": "^5.0.0", - "rollup-plugin-visualizer": "^5.12.0" + "@launchdarkly/js-client-sdk-common": "1.10.0" }, "devDependencies": { "@jest/globals": "^29.7.0", @@ -62,6 +60,7 @@ "prettier": "^3.0.0", "rimraf": "^5.0.5", "rollup": "^3.23.0", + "rollup-plugin-visualizer": "^5.12.0", "ts-jest": "^29.1.1", "typedoc": "0.25.0", "typescript": "^5.5.3" diff --git a/packages/sdk/browser/setup-jest.js b/packages/sdk/browser/setup-jest.js new file mode 100644 index 000000000..e17ac62cb --- /dev/null +++ b/packages/sdk/browser/setup-jest.js @@ -0,0 +1,24 @@ +const { TextEncoder, TextDecoder } = require('node:util'); +const crypto = require('node:crypto'); + +global.TextEncoder = TextEncoder; + +Object.assign(window, { TextDecoder, TextEncoder }); + +// Based on: +// https://stackoverflow.com/a/71750830 + +Object.defineProperty(global.self, 'crypto', { + value: { + getRandomValues: (arr) => crypto.randomBytes(arr.length), + subtle: { + digest: (algorithm, data) => { + return new Promise((resolve) => + resolve( + crypto.createHash(algorithm.toLowerCase().replace('-', '')).update(data).digest(), + ), + ); + }, + }, + }, +}); diff --git a/packages/sdk/browser/src/goals/GoalTracker.ts b/packages/sdk/browser/src/goals/GoalTracker.ts index f3da9a030..bb5365d0e 100644 --- a/packages/sdk/browser/src/goals/GoalTracker.ts +++ b/packages/sdk/browser/src/goals/GoalTracker.ts @@ -1,5 +1,3 @@ -import escapeStringRegexp from 'escape-string-regexp'; - import { addDocumentEventListener, getHref, @@ -7,6 +5,7 @@ import { getLocationSearch, querySelectorAll, } from '../BrowserApi'; +import escapeStringRegexp from '../vendor/escapeStringRegexp'; import { ClickGoal, Goal, Matcher } from './Goals'; type EventHandler = (goal: Goal) => void; diff --git a/packages/sdk/browser/src/vendor/escapeStringRegexp.ts b/packages/sdk/browser/src/vendor/escapeStringRegexp.ts new file mode 100644 index 000000000..228c0e30b --- /dev/null +++ b/packages/sdk/browser/src/vendor/escapeStringRegexp.ts @@ -0,0 +1,34 @@ +// From here: https://github.com/sindresorhus/escape-string-regexp + +// This is vendored to reduce the complexity of the built and test setup. +// The NPM package for escape-string-regexp is ESM only, and that introduces +// complexity to the jest configuration which works best/easiest with CJS. + +/** + * MIT License + * + * Copyright (c) Sindre Sorhus (https://sindresorhus.com) + * + * 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: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * 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. + * + */ + +/** + * Escape regular expression especial characters. + * + * @param string The regular expression to escape. + * @returns The escaped expression. + */ +export default function escapeStringRegexp(string: string) { + if (typeof string !== 'string') { + throw new TypeError('Expected a string'); + } + + // Escape characters with special meaning either inside or outside character sets. + // 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. + return string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d'); +} diff --git a/packages/sdk/browser/tsconfig.json b/packages/sdk/browser/tsconfig.json index 663a919f9..929948785 100644 --- a/packages/sdk/browser/tsconfig.json +++ b/packages/sdk/browser/tsconfig.json @@ -29,7 +29,6 @@ "node_modules", "contract-tests", "babel.config.js", - "jest.config.js", "jestSetupFile.ts", "**/*.test.ts*" ] diff --git a/packages/sdk/browser/tsconfig.test.json b/packages/sdk/browser/tsconfig.test.json new file mode 100644 index 000000000..6087e302d --- /dev/null +++ b/packages/sdk/browser/tsconfig.test.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "rootDir": "src", + "outDir": "dist", + "lib": ["es6", "DOM"], + "module": "CommonJS", + "strict": true, + "noImplicitOverride": true, + "sourceMap": true, + "declaration": true, + "declarationMap": true, + "stripInternal": true + }, + "exclude": [ + "vite.config.ts", + "__tests__", + "dist", + "docs", + "example", + "node_modules", + "contract-tests", + "babel.config.js", + "jest.config.js", + "jestSetupFile.ts", + "**/*.test.ts*" + ] +}