Skip to content

Commit

Permalink
chore: Change testing to use cjs.
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion committed Oct 25, 2024
1 parent ae2b5bb commit e87f59e
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 26 deletions.
3 changes: 0 additions & 3 deletions packages/sdk/browser/__tests__/BrowserDataManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { jest } from '@jest/globals';
import { TextEncoder } from 'node:util';

import {
ApplicationTags,
Expand All @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +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;

Check failure on line 3 in packages/sdk/browser/__tests__/platform/BrowserEncoding.test.ts

View workflow job for this annotation

GitHub Actions / build-test-browser (18)

Delete `⏎`

Check failure on line 3 in packages/sdk/browser/__tests__/platform/BrowserEncoding.test.ts

View workflow job for this annotation

GitHub Actions / build-test-browser (21)

Delete `⏎`
it('can base64 a basic ASCII string', () => {
const encoding = new BrowserEncoding();
Expand Down
4 changes: 0 additions & 4 deletions packages/sdk/browser/__tests__/platform/BrowserHasher.test.ts
Original file line number Diff line number Diff line change
@@ -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.

/**
Expand Down
8 changes: 0 additions & 8 deletions packages/sdk/browser/jest.config.js

This file was deleted.

26 changes: 26 additions & 0 deletions packages/sdk/browser/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"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",
{}
]
}
}
7 changes: 3 additions & 4 deletions packages/sdk/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
23 changes: 23 additions & 0 deletions packages/sdk/browser/setup-jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { TextEncoder, TextDecoder } = require('node:util');
const crypto = require('node:crypto');

global.TextEncoder = TextEncoder;

Object.assign(window, { TextDecoder, TextEncoder });

Object.defineProperty(global.self, "crypto", {
value: {
getRandomValues: (arr) => crypto.randomBytes(arr.length),
subtle: {
digest: (algorithm, data) => {
return new Promise((resolve, reject) =>
resolve(
crypto.createHash(algorithm.toLowerCase().replace("-", ""))
.update(data)
.digest()
)
);
},
},
},
});
3 changes: 1 addition & 2 deletions packages/sdk/browser/src/goals/GoalTracker.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import escapeStringRegexp from 'escape-string-regexp';

import {
addDocumentEventListener,
getHref,
getLocationHash,
getLocationSearch,
querySelectorAll,
} from '../BrowserApi';
import escapeStringRegexp from '../vendor/escapeStringRegexp';
import { ClickGoal, Goal, Matcher } from './Goals';

type EventHandler = (goal: Goal) => void;
Expand Down
34 changes: 34 additions & 0 deletions packages/sdk/browser/src/vendor/escapeStringRegexp.ts
Original file line number Diff line number Diff line change
@@ -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 <[email protected]> (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');
}
2 changes: 1 addition & 1 deletion packages/sdk/browser/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"node_modules",
"contract-tests",
"babel.config.js",
"jest.config.js",
"jest.config.cjs",
"jestSetupFile.ts",
"**/*.test.ts*"
]
Expand Down
27 changes: 27 additions & 0 deletions packages/sdk/browser/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -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*"
]
}

0 comments on commit e87f59e

Please sign in to comment.