Skip to content

Commit

Permalink
[Test Utils] [Recorder] Migrate to esm (#28667)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR
`@azure-tools/test-recorder`

### Issues associated with this PR
#28475

### Describe the problem that is addressed by this PR
Migrates `@azure-tools/test-recorder` to ESM, supports both vitest and
mocha.


### What's in the PR
This PR also pulls in @jeremymeng's change to update recorder file path
calculation that got reverted
#28423


#### One major non-breaking change to `env`
This involves bumping recorder to 4.0.0, a new major due to the nature
of the change.
`process.env` is employed in both node and browser vitest with
playwright.
- removes dotenv dependency
- env shims for browser
- deletes karma.conf

```
                       Exported "env" in 3.1.0                             Exported "env" in 4.0.0
node                    Process.env	                                    Process.env
browser                 Window.__env__	                                    --No shim--
```
Depends on #28917

### Exposed sanitizer types 
- Enhanced the `addSanitizers` method and `SanitizerOptions` options bag
by exposing the following sanitizer types for more flexible usage:

- `FindReplaceSanitizer`: A sanitizer that finds and replaces specified
strings.
- `RegexSanitizer`: A sanitizer that uses regular expressions for
pattern matching and replacement.
- `StringSanitizer`: A sanitizer that handles string-based sanitization
tasks.
- `HeaderSanitizer`: A sanitizer specifically designed for handling HTTP
headers.
- `ConnectionStringSanitizer`: A sanitizer that securely handles
connection strings.
- `RemoveHeaderSanitizer`: A sanitizer that removes specified headers
from HTTP requests or responses.

This update aims to provide users with a more comprehensive and
customizable sanitization process.

### Breaking Changes

- The `@azure-tools/test-recorder@4.0.0` package now supports `vitest`
and `playwright` (stops support for `mocha` and `karma`), employs
`process.env` in both Node and browser environments. This aligns with
the latest testing frameworks and provides improved testing
capabilities.

- The package has been simplified by removing the `dotenv` dependency
and the `karma.conf` file, env shims for the browser. This streamlines
the package dependencies and configuration files, respectively.
- These changes introduce a new `env` strategy for all SDKs once they
migrate to ESM and depend on `@azure-tools/test-recorder` version 4, as
we employ `process.env` through vitest to access environment variables
in both Node and browser environments.

---------

Co-authored-by: Jeremy Meng <jeremy.ymeng@gmail.com>
Co-authored-by: Matthew Podwysocki <matthewp@microsoft.com>
  • Loading branch information
3 people committed Apr 1, 2024
1 parent 1f4aa9a commit 7f1f103
Show file tree
Hide file tree
Showing 40 changed files with 1,582 additions and 712 deletions.
1,465 changes: 1,056 additions & 409 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions sdk/test-utils/recorder/.tshy/browser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "./build.json",
"include": [
"../src/**/*.ts",
"../src/**/*.mts",
"../src/**/*.tsx"
],
"exclude": [],
"compilerOptions": {
"outDir": "../.tshy-build/browser"
}
}
8 changes: 8 additions & 0 deletions sdk/test-utils/recorder/.tshy/build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "../src",
"module": "nodenext",
"moduleResolution": "nodenext"
}
}
16 changes: 16 additions & 0 deletions sdk/test-utils/recorder/.tshy/commonjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "./build.json",
"include": [
"../src/**/*.ts",
"../src/**/*.cts",
"../src/**/*.tsx"
],
"exclude": [
"../src/**/*.mts",
"../src/utils/encoding-browser.mts",
"../src/utils/relativePathCalculator-browser.mts"
],
"compilerOptions": {
"outDir": "../.tshy-build/commonjs"
}
}
15 changes: 15 additions & 0 deletions sdk/test-utils/recorder/.tshy/esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "./build.json",
"include": [
"../src/**/*.ts",
"../src/**/*.mts",
"../src/**/*.tsx"
],
"exclude": [
".././src/utils/encoding-browser.mts",
".././src/utils/relativePathCalculator-browser.mts"
],
"compilerOptions": {
"outDir": "../.tshy-build/esm"
}
}
15 changes: 15 additions & 0 deletions sdk/test-utils/recorder/.tshy/react-native.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "./build.json",
"include": [
"../src/**/*.ts",
"../src/**/*.mts",
"../src/**/*.tsx"
],
"exclude": [
".././src/utils/encoding-browser.mts",
".././src/utils/relativePathCalculator-browser.mts"
],
"compilerOptions": {
"outDir": "../.tshy-build/react-native"
}
}
26 changes: 26 additions & 0 deletions sdk/test-utils/recorder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
# Release History

## 4.0.0 (Unreleased)

### Features Added

- Enhanced the `addSanitizers` method and `SanitizerOptions` options bag by exposing the following sanitizer types for more flexible usage:

- `FindReplaceSanitizer`: A sanitizer that finds and replaces specified strings.
- `RegexSanitizer`: A sanitizer that uses regular expressions for pattern matching and replacement.
- `StringSanitizer`: A sanitizer that handles string-based sanitization tasks.
- `HeaderSanitizer`: A sanitizer specifically designed for handling HTTP headers.
- `ConnectionStringSanitizer`: A sanitizer that securely handles connection strings.
- `RemoveHeaderSanitizer`: A sanitizer that removes specified headers from HTTP requests or responses.

This update aims to provide users with a more comprehensive and customizable sanitization process.

### Breaking Changes

- The `@azure-tools/test-recorder@4.0.0` package now supports `vitest` and `playwright` (stops support for `mocha` and `karma`), employs `process.env` in both Node and browser environments. This aligns with the latest testing frameworks and provides improved testing capabilities.

- The package has been simplified by removing the `dotenv` dependency and the `karma.conf` file, env shims for the browser. This streamlines the package dependencies and configuration files, respectively.
- These changes introduce a new `env` strategy for all SDKs once they migrate to ESM and depend on `@azure-tools/test-recorder` version 4, as we employ `process.env` through vitest to access environment variables in both Node and browser environments.

### Bugs Fixed

### Other Changes

## 3.1.0 (2023-03-14)

### Features Added
Expand Down
124 changes: 0 additions & 124 deletions sdk/test-utils/recorder/karma.conf.js

This file was deleted.

101 changes: 61 additions & 40 deletions sdk/test-utils/recorder/package.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
{
"name": "@azure-tools/test-recorder",
"version": "3.1.0",
"version": "4.0.0",
"sdk-type": "utility",
"description": "This library provides interfaces and helper methods to provide recording and playback capabilities for the tests in Azure JS/TS SDKs",
"main": "dist/index.js",
"module": "dist-esm/src/index.js",
"types": "./types/src/index.d.ts",
"browser": {
"./dist-esm/src/utils/relativePathCalculator.js": "./dist-esm/src/utils/relativePathCalculator.browser.js",
"./dist-esm/src/utils/env.js": "./dist-esm/src/utils/env.browser.js",
"./dist-esm/src/utils/encoding.js": "./dist-esm/src/utils/encoding.browser.js",
"./dist-esm/test/utils/server.js": "./dist-esm/test/utils/server.browser.js"
},
"main": "./dist/commonjs/index.js",
"types": "./dist/commonjs/index.d.ts",
"scripts": {
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
"build:browser": "echo skipped",
"build:node": "tsc -p . && dev-tool run bundle",
"build:node": "tshy && npm run build:test",
"build:samples": "echo Skipped.",
"build:test": "tsc -p . && dev-tool run bundle",
"build": "npm run clean && tsc -p . && dev-tool run bundle",
"build:test": "tshy && dev-tool run build-test",
"build": "npm run clean && npm run build:test",
"check-format": "dev-tool run vendored prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
"clean": "rimraf --glob dist dist-esm test-dist typings *.tgz *.log",
"extract-api": "echo skipped",
"format": "dev-tool run vendored prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
"integration-test:browser": "concurrently \"npm run tests:server\" \"npm run test:browser-with-proxy\" --kill-others --success first",
"integration-test:node": "concurrently \"npm run tests:server\" \"npm run test:node-with-proxy\" --kill-others --success first",
"test:node-with-proxy": "dev-tool run test:node-ts-input -- --timeout 1200000 \"test/*.spec.ts\"",
"test:browser-with-proxy": "dev-tool run test:browser",
"test:node-with-proxy": "dev-tool run test:vitest",
"test:browser-with-proxy": "dev-tool run test:vitest --browser",
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
"tests:server": "cross-env TS_NODE_COMPILER_OPTIONS=\"{\\\"module\\\": \\\"commonjs\\\"}\" ts-node test/utils/server.ts",
"tests:server": "node server.mjs",
"lint:fix": "eslint --no-eslintrc -c ../../.eslintrc.internal.json package.json src test --ext .ts --fix --fix-type [problem,suggestion]",
"lint": "eslint --no-eslintrc -c ../../.eslintrc.internal.json package.json src test --ext .ts",
"pack": "npm pack 2>&1",
Expand Down Expand Up @@ -65,40 +58,68 @@
"homepage": "https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/test-utils/recorder/",
"sideEffects": false,
"dependencies": {
"@azure/core-rest-pipeline": "^1.13.0",
"@azure/core-auth": "^1.3.2",
"@azure/core-client": "^1.0.0",
"@azure/core-rest-pipeline": "^1.13.0",
"@azure/core-util": "^1.6.1",
"@azure/logger": "^1.0.0"
},
"devDependencies": {
"@azure/core-client": "^1.0.0",
"@azure-tools/vite-plugin-browser-test-map": "^1.0.0",
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@types/express": "^4.16.0",
"@microsoft/api-extractor": "^7.40.3",
"@types/fs-extra": "^8.0.0",
"@types/chai": "^4.1.6",
"@types/mocha": "^10.0.0",
"@types/node": "^18.0.0",
"c8": "^9.1.0",
"chai": "^4.2.0",
"@vitest/browser": "^1.4.0",
"@vitest/coverage-istanbul": "^1.4.0",
"concurrently": "^8.2.0",
"cross-env": "7.0.3",
"dotenv": "^16.0.0",
"eslint": "^8.0.0",
"express": "^4.16.3",
"karma": "^6.2.0",
"karma-chrome-launcher": "^3.0.0",
"karma-coverage": "^2.0.0",
"karma-env-preprocessor": "^0.1.1",
"karma-firefox-launcher": "^1.1.0",
"karma-junit-reporter": "^2.0.1",
"karma-mocha": "^2.0.1",
"karma-mocha-reporter": "^2.2.5",
"karma-sourcemap-loader": "^0.3.8",
"mocha": "^10.0.0",
"eslint": "^8.56.0",
"express": "^4.19.2",
"loupe": "^3.1.0",
"playwright": "^1.41.2",
"prettier": "^3.2.5",
"rimraf": "^5.0.5",
"tslib": "^2.2.0",
"ts-node": "^10.0.0",
"typescript": "~5.3.3"
}
"tshy": "^1.13.0",
"tslib": "^2.6.2",
"typescript": "~5.3.3",
"vitest": "^1.4.0"
},
"tshy": {
"exports": {
"./package.json": "./package.json",
".": "./src/index.ts"
},
"dialects": [
"esm",
"commonjs"
],
"esmDialects": [
"browser",
"react-native"
]
},
"exports": {
"./package.json": "./package.json",
".": {
"browser": {
"types": "./dist/browser/index.d.ts",
"default": "./dist/browser/index.js"
},
"react-native": {
"types": "./dist/react-native/index.d.ts",
"default": "./dist/react-native/index.js"
},
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
}
},
"type": "module"
}
File renamed without changes.
19 changes: 13 additions & 6 deletions sdk/test-utils/recorder/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export { Recorder } from "./recorder";
export { relativeRecordingsPath } from "./utils/relativePathCalculator";
export { Recorder } from "./recorder.js";
export { relativeRecordingsPath } from "./utils/relativePathCalculator.js";
export {
SanitizerOptions,
RecorderStartOptions,
Expand All @@ -12,7 +12,14 @@ export {
assertEnvironmentVariable,
setEnvironmentVariables,
testPollingOptions,
} from "./utils/utils";
export { env } from "./utils/env";
export { delay } from "./utils/delay";
export { CustomMatcherOptions } from "./matcher";
FindReplaceSanitizer,
RegexSanitizer,
StringSanitizer,
HeaderSanitizer,
ConnectionStringSanitizer,
RemoveHeaderSanitizer,
} from "./utils/utils.js";
export { delay } from "./utils/delay.js";
export { env } from "./utils/env.js";
export { CustomMatcherOptions } from "./matcher.js";
export { TestInfo, VitestTestContext } from "./testInfo.js";
Loading

0 comments on commit 7f1f103

Please sign in to comment.