Skip to content

fix(@angular/build): sort discovered unit-test files for deterministic builds - #34337

Open
jv3n wants to merge 1 commit into
angular:mainfrom
jv3n:fix/unit-test-deterministic-test-order
Open

jv3n wants to merge 1 commit into
angular:mainfrom
jv3n:fix/unit-test-deterministic-test-order

Conversation

@jv3n

@jv3n jv3n commented Oct 13, 2026 •

Copy link
Copy Markdown

PR Checklist

PR Type

  • Bugfix

What is the current behavior?

Issue Number: #34335

findTests returns the test files in the order tinyglobby yields them. The glob crawls directories asynchronously, so the order changes between runs: on a project with 255 spec files, 8 consecutive calls returned 8 different orders (same set of files). That order feeds the build entry points and rootFiles, so two ng test builds of the same sources produce different chunks: imports come out reordered, the content-hashed chunk names change, and the change cascades to every importer (79 to 89 of 593 output files renamed between two runs). This defeats Vitest 5's fsModuleCache, which is keyed on file content.

What is the new behavior?

The discovered test files are sorted before being turned into entry points. With that change, three consecutive builds of the same project produced byte-identical test bundles (0 of 593 files differing, --dump-virtual-files output compared with diff -r), and Vitest's module cache was fully reused across runs.

A findTests spec checks the result is sorted. The crawl order can happen to be sorted on a small fixture, so it guards against a regression rather than reproducing the race on every run.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Measured with @angular/build 22.2.2, Vitest 5.0.4, Node 24, on macOS; the same non-determinism shows up in AOT and JIT test builds, with and without --coverage, and with NG_BUILD_MAX_WORKERS=1.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the findTests function to sort the resolved test files, ensuring deterministic build outputs regardless of the asynchronous directory crawling order. A new unit test suite has also been added to verify this sorting behavior. The review feedback suggests providing an explicit, locale-independent comparison function to Array.prototype.sort() to avoid potential linting errors and guarantee strict determinism across different environments.

Comment thread packages/angular/build/src/builders/unit-test/test-discovery.ts
@jv3n
jv3n force-pushed the fix/unit-test-deterministic-test-order branch from 7af73a2 to 72f963a Compare October 13, 2026 17:30
@alan-agius4 alan-agius4 added target: patch This PR is targeted for the next patch release action: merge The PR is ready for merge by the caretaker labels Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: @angular/build target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unit-test builder (Vitest): test bundle output is not deterministic, which defeats Vitest 5 fsModuleCache

2 participants