From bc3b6318b65da3ce74804ca1c5ee6f6ac55b1164 Mon Sep 17 00:00:00 2001 From: Jay Date: Fri, 13 Mar 2026 18:55:58 +0200 Subject: [PATCH] refactor: remove old test suite update docs (#7498) * chore: update all ci workflows * chore: added readme for tests * chore: fix release branch * Update .github/workflows/release-branch.yml Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> --------- Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> --- .github/workflows/publish.yml | 1 - .github/workflows/release-branch.yml | 118 ++++++++++++-- .github/workflows/run-ci.yml | 7 +- karma.conf.cjs | 224 --------------------------- package.json | 22 +-- tests/README.md | 110 +++++++++++++ 6 files changed, 225 insertions(+), 257 deletions(-) delete mode 100644 karma.conf.cjs create mode 100644 tests/README.md diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 550b8406..7e2623d0 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -12,7 +12,6 @@ permissions: jobs: publish: runs-on: ubuntu-latest - steps: - name: Checkout repo uses: actions/checkout@v6 diff --git a/.github/workflows/release-branch.yml b/.github/workflows/release-branch.yml index 013914d8..fee76836 100644 --- a/.github/workflows/release-branch.yml +++ b/.github/workflows/release-branch.yml @@ -21,12 +21,112 @@ permissions: pull-requests: write jobs: - release: + build-and-run-vitest: + name: Build and run vitest runs-on: ubuntu-latest - steps: - name: Checkout repo uses: actions/checkout@v6 + with: + persist-credentials: true + - name: Setup node + uses: actions/setup-node@v6 + with: + node-version: 24.x + cache: npm + - name: Install dependencies + run: npm ci + - name: Build project + run: npm run build + - name: Install Playwright with deps + run: npx playwright install --with-deps + - name: Run unit tests + run: npm run test:vitest:unit + - name: Run browser tests + run: npm run test:vitest:browser:headless + - name: Upload build artifact + uses: actions/upload-artifact@v7 + with: + name: axios + path: dist + retention-days: 1 + + cjs-smoke-tests: + name: CJS smoke tests (Node ${{ matrix.node-version }}) + needs: build-and-run-vitest + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + node-version: [12, 14, 16, 18] + steps: + - name: Checkout repo + uses: actions/checkout@v6 + with: + persist-credentials: true + - name: Setup node + uses: actions/setup-node@v6 + with: + node-version: ${{ matrix.node-version }} + cache: npm + cache-dependency-path: tests/smoke/cjs/package-lock.json + - name: Install dependencies + if: matrix.node-version == 16 || matrix.node-version == 18 + run: npm ci + - name: Download build artifact + uses: actions/download-artifact@v8 + with: + name: axios + path: dist + - name: Install CJS smoke test dependencies + working-directory: tests/smoke/cjs + run: npm install + - name: Run CJS smoke tests + working-directory: tests/smoke/cjs + run: npm run test:smoke:cjs:mocha + + esm-smoke-tests: + name: ESM smoke tests (Node ${{ matrix.node-version }}) + needs: build-and-run-vitest + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + node-version: [20, 22, 24] + steps: + - name: Checkout repo + uses: actions/checkout@v6 + with: + persist-credentials: true + - name: Setup node + uses: actions/setup-node@v6 + with: + node-version: ${{ matrix.node-version }} + cache: npm + cache-dependency-path: tests/smoke/esm/package-lock.json + - name: Install dependencies + run: npm ci + - name: Download build artifact + uses: actions/download-artifact@v8 + with: + name: axios + path: dist + - name: Install ESM smoke test dependencies + working-directory: tests/smoke/esm + run: npm install + - name: Run ESM smoke tests + working-directory: tests/smoke/esm + run: npm run test:smoke:esm:vitest + + bump-version-and-create-pr: + name: Bump version and create PR + needs: [build-and-run-vitest, cjs-smoke-tests, esm-smoke-tests] + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v6 + with: + persist-credentials: false - name: Setup Node.js uses: actions/setup-node@v6 with: @@ -61,19 +161,13 @@ jobs: run: npm run preversion - name: Build project run: npm run build - - name: Run unit tests - run: npm run test:node - - name: Run package tests - run: npm run test:package - - name: Run browser tests - run: npm run test:browser - name: Create Pull Request uses: peter-evans/create-pull-request@v8 with: - branch: "release" - commit-message: "chore(release): prepare release ${{ steps.bump-version.outputs.newTag }}" - body: "This PR prepares the release ${{ steps.bump-version.outputs.newTag }}." - title: "chore(release): prepare release ${{ steps.bump-version.outputs.newTag }}" + branch: 'release' + commit-message: 'chore(release): prepare release ${{ steps.bump-version.outputs.newTag }}' + body: 'This PR prepares the release ${{ steps.bump-version.outputs.newTag }}.' + title: 'chore(release): prepare release ${{ steps.bump-version.outputs.newTag }}' maintainer-can-modify: true draft: false labels: | diff --git a/.github/workflows/run-ci.yml b/.github/workflows/run-ci.yml index 05803bf1..2abf7450 100644 --- a/.github/workflows/run-ci.yml +++ b/.github/workflows/run-ci.yml @@ -13,7 +13,8 @@ concurrency: cancel-in-progress: true jobs: - ci: + build-and-run-vitest: + name: Build and run vitest runs-on: ubuntu-latest steps: - name: Checkout repo @@ -46,7 +47,7 @@ jobs: cjs-smoke-tests: name: CJS smoke tests (Node ${{ matrix.node-version }}) - needs: ci + needs: build-and-run-vitest runs-on: ubuntu-latest strategy: fail-fast: false @@ -80,7 +81,7 @@ jobs: esm-smoke-tests: name: ESM smoke tests (Node ${{ matrix.node-version }}) - needs: ci + needs: build-and-run-vitest runs-on: ubuntu-latest strategy: fail-fast: false diff --git a/karma.conf.cjs b/karma.conf.cjs deleted file mode 100644 index 9f102319..00000000 --- a/karma.conf.cjs +++ /dev/null @@ -1,224 +0,0 @@ -/* eslint-disable no-console */ -/* eslint-disable no-unused-vars */ -/* eslint-disable func-names */ -// Karma configuration -// Generated on Fri Aug 15 2014 23:11:13 GMT-0500 (CDT) - -'use strict'; - -var resolve = require('@rollup/plugin-node-resolve').default; -var commonjs = require('@rollup/plugin-commonjs'); - -function createCustomLauncher(browser, version, platform) { - return { - base: 'SauceLabs', - browserName: browser, - version: version, - platform: platform, - }; -} - -module.exports = function (config) { - var customLaunchers = {}; - var browsers = process.env.Browsers && process.env.Browsers.split(','); - var sauceLabs; - - if (process.env.SAUCE_USERNAME || process.env.SAUCE_ACCESS_KEY) { - customLaunchers = {}; - - var runAll = true; - var options = [ - 'SAUCE_CHROME', - 'SAUCE_FIREFOX', - 'SAUCE_SAFARI', - 'SAUCE_OPERA', - 'SAUCE_IE', - 'SAUCE_EDGE', - 'SAUCE_IOS', - 'SAUCE_ANDROID', - ]; - - options.forEach(function (opt) { - if (process.env[opt]) { - runAll = false; - } - }); - - // Chrome - if (runAll || process.env.SAUCE_CHROME) { - customLaunchers.SL_Chrome = createCustomLauncher('chrome'); - // customLaunchers.SL_ChromeDev = createCustomLauncher('chrome', 'dev'); - // customLaunchers.SL_ChromeBeta = createCustomLauncher('chrome', 'beta'); - } - - // Firefox - if (runAll || process.env.SAUCE_FIREFOX) { - //customLaunchers.SL_Firefox = createCustomLauncher('firefox'); - // customLaunchers.SL_FirefoxDev = createCustomLauncher('firefox', 'dev'); - // customLaunchers.SL_FirefoxBeta = createCustomLauncher('firefox', 'beta'); - } - - // Safari - if (runAll || process.env.SAUCE_SAFARI) { - // customLaunchers.SL_Safari7 = createCustomLauncher('safari', 7); - // customLaunchers.SL_Safari8 = createCustomLauncher('safari', 8); - customLaunchers.SL_Safari9 = createCustomLauncher('safari', 9.0, 'OS X 10.11'); - customLaunchers.SL_Safari10 = createCustomLauncher('safari', '10.1', 'macOS 10.12'); - customLaunchers.SL_Safari11 = createCustomLauncher('safari', '11.1', 'macOS 10.13'); - } - - // Opera - if (runAll || process.env.SAUCE_OPERA) { - // TODO The available versions of Opera are too old and lack basic APIs - // customLaunchers.SL_Opera11 = createCustomLauncher('opera', 11, 'Windows XP'); - // customLaunchers.SL_Opera12 = createCustomLauncher('opera', 12, 'Windows 7'); - } - - // IE - if (runAll || process.env.SAUCE_IE) { - customLaunchers.SL_IE11 = createCustomLauncher('internet explorer', 11, 'Windows 8.1'); - } - - // Edge - if (runAll || process.env.SAUCE_EDGE) { - customLaunchers.SL_Edge = createCustomLauncher('microsoftedge', null, 'Windows 10'); - } - - // IOS - if (runAll || process.env.SAUCE_IOS) { - // TODO IOS7 capture always timesout - // customLaunchers.SL_IOS7 = createCustomLauncher('iphone', '7.1', 'OS X 10.10'); - // TODO Mobile browsers are causing failures, possibly from too many concurrent VMs - // customLaunchers.SL_IOS8 = createCustomLauncher('iphone', '8.4', 'OS X 10.10'); - // customLaunchers.SL_IOS9 = createCustomLauncher('iphone', '9.2', 'OS X 10.10'); - } - - // Android - if (runAll || process.env.SAUCE_ANDROID) { - // TODO Mobile browsers are causing failures, possibly from too many concurrent VMs - // customLaunchers.SL_Android4 = createCustomLauncher('android', '4.4', 'Linux'); - // customLaunchers.SL_Android5 = createCustomLauncher('android', '5.1', 'Linux'); - } - - browsers = Object.keys(customLaunchers); - - sauceLabs = { - recordScreenshots: false, - connectOptions: { - // port: 5757, - logfile: 'sauce_connect.log', - }, - public: 'public', - }; - } else if (process.env.TRAVIS_PULL_REQUEST && process.env.TRAVIS_PULL_REQUEST !== 'false') { - console.log( - 'Cannot run on Sauce Labs as encrypted environment variables are not available to PRs. ' + - 'Running on Travis.' - ); - browsers = ['Firefox']; - } else if (process.env.GITHUB_ACTIONS === 'true') { - console.log('Running ci on GitHub Actions.'); - browsers = ['FirefoxHeadless', 'ChromeHeadless']; - } else { - browsers = browsers || ['Chrome']; - console.log( - `Running ${browsers} locally since SAUCE_USERNAME and SAUCE_ACCESS_KEY environment variables are not set.` - ); - } - - config.set({ - // base path that will be used to resolve all patterns (eg. files, exclude) - basePath: '', - - // frameworks to use - // available frameworks: https://npmjs.org/browse/keyword/karma-adapter - frameworks: ['jasmine-ajax', 'jasmine', 'sinon'], - - // list of files / patterns to load in the browser - files: [ - { pattern: 'test/specs/__helpers.js', watched: false }, - { pattern: 'test/specs/**/*.spec.js', watched: false }, - ], - - // list of files to exclude - exclude: [], - - // preprocess matching files before serving them to the browser - // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor - preprocessors: { - 'test/specs/__helpers.js': ['rollup'], - 'test/specs/**/*.spec.js': ['rollup'], - }, - - rollupPreprocessor: { - plugins: [resolve({ browser: true }), commonjs()], - output: { - format: 'iife', - name: '_axios', - sourcemap: 'inline', - }, - }, - - // test results reporter to use - // possible values: 'dots', 'progress' - // available reporters: https://npmjs.org/browse/keyword/karma-reporter - // Disable code coverage, as it's breaking CI: - // reporters: ['dots', 'coverage', 'saucelabs'], - reporters: ['progress'], - - // web server port - port: 9876, - - // Increase timeouts to prevent the issue with disconnected tests (https://goo.gl/nstA69) - captureTimeout: 4 * 60 * 1000, - browserDisconnectTimeout: 10000, - browserDisconnectTolerance: 1, - browserNoActivityTimeout: 4 * 60 * 1000, - - // enable / disable colors in the output (reporters and logs) - colors: true, - - // level of logging - // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG - logLevel: config.LOG_INFO, - - // enable / disable watching file and executing tests whenever any file changes - autoWatch: false, - - // start these browsers - // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher - browsers: browsers, - - // Continuous Integration mode - // if true, Karma captures browsers, runs the tests and exits - singleRun: false, - - // Webpack config - webpack: { - mode: 'development', - cache: true, - devtool: 'inline-source-map', - externals: [ - { - './adapters/http': 'var undefined', - }, - ], - }, - - webpackServer: { - stats: { - colors: true, - }, - }, - - // Coverage reporting - coverageReporter: { - type: 'lcov', - dir: 'coverage/', - subdir: '.', - }, - - sauceLabs: sauceLabs, - customLaunchers: customLaunchers, - }); -}; diff --git a/package.json b/package.json index c6e3e82b..3d320a4d 100644 --- a/package.json +++ b/package.json @@ -45,28 +45,16 @@ "type": "module", "types": "index.d.ts", "scripts": { - "test": "npm run test:node && npm run test:browser && npm run test:package", - "test:node": "npm run test:mocha", - "test:node:coverage": "c8 npm run test:mocha", - "test:browser": "npm run test:karma", + "build": "gulp clear && cross-env NODE_ENV=production rollup -c -m", + "version": "npm run build && git add package.json", + "preversion": "gulp version", + "test": "npm run test:vitest", "test:vitest": "vitest run", "test:vitest:unit": "vitest run --project unit", "test:vitest:browser": "vitest run --project browser", "test:vitest:browser:headless": "vitest run --project browser-headless", "test:vitest:watch": "vitest", - "test:package": "npm run test:eslint && npm run test:exports", - "test:eslint": "node bin/ssl_hotfix.js eslint lib/**/*.js", - "test:mocha": "node bin/ssl_hotfix.js mocha test/unit/**/*.js --timeout 30000 --exit", - "test:exports": "node bin/ssl_hotfix.js mocha test/module/test.js --timeout 30000 --exit", - "test:karma": "node ./bin/run-karma-tests.js", - "test:karma:firefox": "node bin/ssl_hotfix.js cross-env LISTEN_ADDR=:: Browsers=Firefox karma start karma.conf.cjs --single-run", - "test:karma:server": "node bin/ssl_hotfix.js cross-env karma start karma.conf.cjs", - "test:build:version": "node ./bin/check-build-version.js", "start": "node ./sandbox/server.js", - "preversion": "gulp version", - "version": "npm run build && git add package.json", - "prepublishOnly": "npm run test:build:version", - "build": "gulp clear && cross-env NODE_ENV=production rollup -c -m", "examples": "node ./examples/server.js", "fix": "eslint --fix lib/**/*.js", "prepare": "husky install && npm run prepare:hooks", @@ -232,4 +220,4 @@ ], "report-dir": "./coverage" } -} \ No newline at end of file +} diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 00000000..8a58f0ae --- /dev/null +++ b/tests/README.md @@ -0,0 +1,110 @@ +# Test Contribution Guide + +This guide explains how to contribute tests inside the `tests` directory. +It is intentionally scoped to this directory only. + +## Tests Directory Layout + +```text +tests/ + browser/ # browser runtime tests + setup/ # shared test setup utilities + smoke/ # package-compat smoke suites (esm + cjs) + unit/ # focused unit/behavior tests +``` + +Use the runtime-first layout already present in this directory: + +- Put browser-runtime behavior in `tests/browser`. +- Put non-browser focused tests in `tests/unit`. +- Put packaging/compatibility smoke checks in `tests/smoke/esm/tests` and `tests/smoke/cjs/tests`. +- Reuse helpers from `tests/setup` instead of duplicating setup logic. + +## File Naming Conventions + +Follow the existing file patterns: + +- Unit tests: `*.test.js` +- Browser tests: `*.browser.test.js` +- ESM smoke tests: `*.smoke.test.js` +- CJS smoke tests: `*.smoke.test.cjs` + +When adding a new test, match the nearest existing file name pattern in the same subdirectory. + +## Suite-Specific Authoring Patterns + +### Unit (`tests/unit`) + +- Keep tests focused on one behavior or API surface. +- For adapter/network behavior, prefer local test servers using utilities from `tests/setup/server.js`. +- Ensure server cleanup with `try/finally` so tests do not leak resources. +- Keep fixtures close to the tests that use them (see `tests/unit/adapters` for examples). + +Representative files: + +- `tests/unit/adapters/http.test.js` +- `tests/unit/adapters/fetch.test.js` +- `tests/unit/regression.test.js` + +### Browser (`tests/browser`) + +- Use local in-file `MockXMLHttpRequest` style mocks when testing request behavior. +- Replace global XHR in `beforeEach` and restore it in `afterEach`. +- Reset spies/mocks in cleanup hooks to keep tests isolated. +- Keep assertions centered on observable request/response behavior. + +Representative files: + +- `tests/browser/requests.browser.test.js` +- `tests/browser/adapter.browser.test.js` +- `tests/browser/defaults.browser.test.js` + +### Smoke (`tests/smoke`) + +- Keep ESM and CJS smoke coverage aligned for compatibility-sensitive behavior. +- If you add a new smoke scenario in one format, add the equivalent in the other format. +- Keep smoke tests small and focused on import/runtime behavior and critical request flows. + +Representative files: + +- `tests/smoke/esm/tests/fetch.smoke.test.js` +- `tests/smoke/cjs/tests/fetch.smoke.test.cjs` +- `tests/smoke/esm/tests/basic.smoke.test.js` +- `tests/smoke/cjs/tests/basic.smoke.test.cjs` + +## Shared Setup Utilities (`tests/setup`) + +Use shared helpers before introducing new setup code: + +- `tests/setup/server.js` + - Server lifecycle helpers: `startHTTPServer`, `stopHTTPServer`, `stopAllTrackedHTTPServers` + - Timing helpers: `setTimeoutAsync` + - Data/stream helpers used by adapter tests +- `tests/setup/browser.setup.js` + - Browser cleanup hook (`afterEach`) for clearing test DOM state + +General expectation: if a helper can be reused by multiple tests in this directory, add or extend it in `tests/setup` instead of copying setup code between test files. + +## Fixtures and Test Data + +- Prefer colocated fixtures near the test files that need them. +- Keep fixture names explicit and stable. +- For matrix-like scenarios, prefer concise table-driven cases inside the test file when practical. + +Examples of colocated fixtures: + +- `tests/unit/adapters/cert.pem` +- `tests/unit/adapters/key.pem` +- `tests/unit/adapters/axios.png` + +## Contributor Checklist + +Before opening a PR for tests in this directory: + +- File is placed in the correct suite directory (`unit`, `browser`, or `smoke`). +- File name matches the local pattern (`*.test.js`, `*.browser.test.js`, `*.smoke.test.js`, `*.smoke.test.cjs`). +- Test setup/teardown is explicit and leaves no global/server state behind. +- Shared setup logic uses `tests/setup` helpers where possible. +- Smoke tests remain ESM/CJS consistent when behavior is format-sensitive. +- Fixtures are colocated and minimal. +- Assertions are deterministic and avoid unnecessary timing/network flakiness.