test: check for available port #6337
Merged
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.
Pull Request Submission Checklist
are release-note ready, emphasizing
what was changed, not how.
What does this PR do?
This PR updates the test infrastructure to dynamically find an available port for the mock HTTP server used in acceptance tests (specifically those testing the
snyk sbomcommand).It replaces the previous method of relying on environment variables or hardcoded default ports with a call to the new utility function,
getAvailableServerPort, which uses theportfinderpackage to locate an open port.Key changes:
portfinderpackage as a dependency inpackage.jsonandpackage-lock.json.getAvailableServerPortintest/jest/util/getServerPort.tsto find an available port starting from a default of4000.*-sboms.spec.ts,*-options.spec.ts,sbom.spec.ts,all-projects.spec.ts,yarn-options.spec.ts,pip-options.spec.ts) to usegetAvailableServerPortand convert theirbeforeAllblocks to useasync/awaitfor proper port acquisition before starting the server.This change is aimed at improving test reliability in concurrent or CI environments where hardcoded ports might already be in use.
Where should the reviewer start?
The most significant logical changes are in:
test/jest/util/getServerPort.ts: This file contains the newgetAvailableServerPortlogic.package.jsonandpackage-lock.json: To confirm the addition ofportfinderand the update toasync.beforeAll(async () => { ... })and callinggetAvailableServerPort(process): This shows how the new utility is integrated into the tests (e.g.,test/jest/acceptance/snyk-sbom/all-projects.spec.ts).How should this be manually tested?
As this is a change to test infrastructure, no manual testing is necessary outside of running the existing test suite.
What's the product update that needs to be communicated to CLI users?
No user-facing product update is needed, as this only affects the CLI's internal testing infrastructure.
Risk assessment (Low | Medium | High)?
Low. This change is contained entirely within the test utilities and acceptance tests. It does not affect the production code or CLI functionality used by end-users. It should only improve the robustness of testing.
Any background context you want to provide?
The acceptance tests were occasionally failing in CI due to suspected port conflicts when trying to start the mock server on a hardcoded or default environment port. The use of
portfinderensures that an unused port is reliably found before attempting to listen.