Skip to content

Commit 4d2acaa

Browse files
authored
refactor: migrate to node:test and setup jsdom environment (#5667)
* feat(tests): add node test setup and update test utilities - Introduced a new script for node test setup (`scripts/node-test-setup.mjs`) to configure snapshot serializers and resolve snapshot paths for webpack tests. - Added new npm script `test:node` in `package.json` for running node tests with specific configurations. - Updated Jest snapshots in `proxy-option.test.js.snap.webpack5` to reflect changes in test descriptions. - Refactored tests in `open-option.test.js` and `proxy-option.test.js` to utilize `jest-mock` for mocking and spying, replacing previous Jest mocking methods. - Changed `beforeAll` and `afterAll` hooks to `before` and `after` for better test isolation and readability. * refactor(tests): update snapshot test descriptions and improve test structure - Changed snapshot test descriptions to include a more structured format. - Updated snapshot assertions to use `t.assert.snapshot` for consistency. - Refactored test setup and teardown methods to use `before` and `after` from `node:test`. - Adjusted the test cases in `normalize-options.test.js` and `validate-options.test.js` for improved clarity and maintainability. * refactor(tests): update CLI option tests to use node:test and expect - Refactored multiple test files to utilize the `node:test` module for structuring tests. - Replaced direct assertions with `t.assert.snapshot` for snapshot testing. - Ensured consistency across tests by adding async context to test functions. - Updated imports to include necessary testing utilities from `node:test` and `expect`. * refactor(tests): update web socket tests to use node:test and expect assertions - Replaced traditional test structure with node:test for better test organization. - Updated snapshot assertions to use the new test framework's syntax. - Ensured all test cases in web-socket-communication.test.js, web-socket-server-url.test.js, and web-socket-server.test.js are consistent with the new structure. * refactor(tests): migrate tests to use node:test and improve snapshot organization - Updated test files to utilize the `node:test` module for better compatibility and performance. - Replaced `jest.fn()` with `jest-mock` functions for mocking in various test cases. - Enhanced snapshot descriptions for clarity and consistency across tests. - Introduced a new `jsdom-setup.js` helper to streamline JSDOM configuration for tests. - Adjusted test cases to use `t.assert.snapshot()` for snapshot assertions. - Cleaned up mock implementations and reset logic in tests for improved reliability. * refactor: migrate tests from Jest to Node's test module - Updated package.json to remove Jest dependencies and adjust test scripts for Node's test module. - Deleted globalSetupTest.js and setupTest.js as they are no longer needed. - Refactored ReactErrorBoundary.test.js to utilize real jsdom window/document. - Updated WebsocketClient.test.js to use async/await and improved test structure. - Skipped index.test.js due to dependency on Jest's mocking capabilities. - Refactored socket-helper.test.js to create a mock WebSocketClient class. - Updated log.test.js to simplify logger tests and remove unnecessary mocks. - Enhanced jsdom-setup.js to replace jsdom's WebSocket with Node's ws library for better testing of WebSocket connections. * fixup! * fixup! * fixup! * fixup! * fixup! * fixup! * fixup! * fixup! * fixup! * fixup! * fixup! * chore: update test runner * fixup! * chore: update snapshots and fix module path in API tests * fixup! * fixup! * chore: update snapshots for watchFiles option tests * refactor: remove unused Logger interface and simplify socket function parameters * fix: update snapshot serialization for Windows compatibility and adjust test helper import * fix: codecov upload * fix: ensure directories are created for test reporter destination * refactor: simplify client resolution and clean up socket tests * refactor: clean up comments and improve clarity in test files * fixup!
1 parent a7ab30f commit 4d2acaa

133 files changed

Lines changed: 12270 additions & 14796 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ jobs:
106106
cp -R tmp-client client
107107
108108
- name: Run tests for webpack version ${{ matrix.webpack-version }}
109-
run: npm run test:coverage -- --ci --shard=${{ matrix.shard }}
109+
run: npm run test:coverage -- --test-shard=${{ matrix.shard }}
110110

111111
- name: Submit coverage data to codecov
112112
uses: codecov/codecov-action@v5

eslint.config.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,23 @@ export default defineConfig([
2121
{
2222
files: ["test/**/*"],
2323
extends: [configs["universal-recommended"]],
24+
rules: {
25+
// Tests use experimental node:test APIs intentionally
26+
// (mock.module, mock.timers, snapshot.*). The package's engines field
27+
// is wider than where these are stable, so silence the linter here.
28+
"n/no-unsupported-features/node-builtins": "off",
29+
// Test callbacks (it/test/subtest arrow functions) don't need JSDoc.
30+
"jsdoc/require-jsdoc": "off",
31+
// Tests legitimately log diagnostics (retry attempts, etc.).
32+
"no-console": "off",
33+
// node:test callbacks receive `t` (TestContext) as a parameter.
34+
"id-length": "off",
35+
},
36+
},
37+
{
38+
files: ["scripts/node-test-setup.mjs", "scripts/run-tests.mjs"],
39+
rules: {
40+
"n/no-unsupported-features/node-builtins": "off",
41+
},
2442
},
2543
]);

jest.config.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

lib/Server.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3392,6 +3392,17 @@ class Server {
33923392
}
33933393

33943394
this.sockets = [];
3395+
3396+
// Force-close any remaining connections that aren't in `this.sockets`
3397+
// (HTTP keep-alive idle connections from upstream clients,
3398+
// long-poll requests, sockets that were upgraded out of `connection`
3399+
// tracking, etc). Without this, `server.close()`'s callback can hang
3400+
// indefinitely waiting for connections that never close on their own
3401+
// — a problem that surfaces in test suites that rapidly start/stop
3402+
// servers on the same port (next start hits EADDRINUSE).
3403+
// `closeAllConnections` is available since Node.js 18.2.
3404+
/** @type {S & { closeAllConnections?: () => void }} */
3405+
(this.server).closeAllConnections?.();
33953406
})
33963407
);
33973408

0 commit comments

Comments
 (0)