Skip to content

The unit-test builder doesn't re-run test suites on changes to non-spec files #32159

Description

@brandonroberts

Command

test

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

Storybook has support for running component tests using Vitest. When trying to build a POC with the unit-test builder, I ran into two issues.

  • Storybook can't transform the stories because the compiled code leads to file with just animport as its contents.
  • Making changes to files that aren't in the include array doesn't trigger tests to be re-run.

I was able to work around the code import issue with a plugin that re-exports the chunks from compiled files
https://github.com/brandonroberts/angular-storybook-vitest/blob/main/vitest-storybook-base.config.ts#L16

Minimal Reproduction

https://github.com/brandonroberts/angular-storybook-vitest

  1. Clone and install dependencies
  2. Run npm run test-storybook
  3. Note the tests pass
  4. Make a change in one of the src/stories/*.stories.ts files
  5. Note the build re-compiles but the tests aren't re-run.

The test-storybook target excludes the stories files because of the import issue so they are transpiled as-is.

https://github.com/brandonroberts/angular-storybook-vitest/blob/main/angular.json#L94

Exception or Error

No exception

Your Environment

Angular v21

Anything else relevant?

No response

Activity

  1. added theissue type on Jan 5, 2026
  2. added 2 commits that reference this issue on Mar 27, 2026
    dd1280c
    20599e7
  3. maruthang commented on Mar 27, 2026

    @maruthang
    Contributor

    Hi, I'd like to work on this issue.

    Root cause: In packages/angular/build/src/builders/unit-test/runners/vitest/executor.ts, the entryPointToTestFile map only contains .spec.ts files. When non-test files (services, components) change during watch mode, they're not in the map, and getModuleSpecifications() is never called for them — so no dependent tests are re-run.

    Proposed fix: In the else branch (when a modified file isn't in the test map), call vitest.getModuleSpecifications() on the absolute output file path. Vitest's module graph correctly traverses backwards to find all dependent test specifications. This is consolidated into a single loop for clarity.

    Submitting a PR now.

  4. added a commit that references this issue on Mar 27, 2026
    165d521
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions