-
Notifications
You must be signed in to change notification settings - Fork 0
Separate Vitest suites and fail listener setup fast #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { createServer } from 'node:http'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { closeTestServer, listenOnLoopback } from './listener'; | ||
|
|
||
| describe('test listener lifecycle', () => { | ||
| it('rejects immediately with the original listener error and removes its temporary listeners', async () => { | ||
| const server = createServer(); | ||
| const initialListeningListeners = server.listenerCount('listening'); | ||
| const bindError = Object.assign(new Error('listen EPERM: operation not permitted 127.0.0.1'), { | ||
| code: 'EPERM' | ||
| }); | ||
| vi.spyOn(server, 'listen').mockImplementation(() => { | ||
| queueMicrotask(() => server.emit('error', bindError)); | ||
| return server; | ||
| }); | ||
|
|
||
| await expect(listenOnLoopback(server)).rejects.toBe(bindError); | ||
| expect(server.listenerCount('error')).toBe(0); | ||
| expect(server.listenerCount('listening')).toBe(initialListeningListeners); | ||
| }); | ||
|
|
||
| it('allows cleanup after partial initialization when the server never listened', async () => { | ||
| const server = createServer(); | ||
| await expect(closeTestServer(server)).resolves.toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import type { Server } from 'node:http'; | ||
|
|
||
| /** | ||
| * Start a test HTTP server on loopback and propagate bind failures immediately. | ||
| * | ||
| * Test suites deliberately use an ephemeral port, but they must not silently | ||
| * hang when the environment denies listening sockets. The temporary listeners | ||
| * are removed whichever event settles first so later server errors retain their | ||
| * normal Node handling. | ||
| */ | ||
| export function listenOnLoopback(server: Server): Promise<number> { | ||
| return new Promise((resolve, reject) => { | ||
| const onListening = () => { | ||
| cleanup(); | ||
| const address = server.address(); | ||
| if (address && typeof address === 'object') { | ||
| resolve(address.port); | ||
| return; | ||
| } | ||
| reject(new Error('Test server started without a TCP address')); | ||
| }; | ||
| const onError = (error: Error) => { | ||
| cleanup(); | ||
| reject(error); | ||
| }; | ||
| const cleanup = () => { | ||
| server.removeListener('listening', onListening); | ||
| server.removeListener('error', onError); | ||
| }; | ||
|
|
||
| server.once('listening', onListening); | ||
| server.once('error', onError); | ||
| server.listen({ port: 0, host: '127.0.0.1' }); | ||
| }); | ||
| } | ||
|
|
||
| /** Close a test server without treating a failed startup as a second failure. */ | ||
| export function closeTestServer(server: Server): Promise<void> { | ||
| return new Promise((resolve, reject) => { | ||
| server.close((error) => { | ||
| if (error && (error as NodeJS.ErrnoException).code !== 'ERR_SERVER_NOT_RUNNING') { | ||
| reject(error); | ||
| return; | ||
| } | ||
| resolve(); | ||
| }); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.