angular-cli: ERR_INVALID_STATE.TypeError('Controller is already closed') when executing unit tests in default created project for Angular 20

Which @angular/* package(s) are the source of the bug?

Don’t known / other

Is this a regression?

Yes

Description

I have updated one of my apps to angular 20 from angular 19. Now I see that unit tests using karma works but at the end of the execution I see the next error: node:internal/webstreams/readablestream:1077 throw new ERR_INVALID_STATE.TypeError(‘Controller is already closed’); ^

TypeError [ERR_INVALID_STATE]: Invalid state: Controller is already closed at ReadableStreamDefaultController.enqueue (node:internal/webstreams/readablestream:1077:13)

I also have updated angular-cli tool and I created a new project from scratch to know if maybe the issue is related with my project. But I have seen that same error appears. Also, I included Stryker in the default created project and this doesn’t work (but Stryker is other problem).

Default created project package.json: { “name”: “ang-20-test”, “version”: “0.0.0”, “scripts”: { “ng”: “ng”, “start”: “ng serve”, “build”: “ng build”, “watch”: “ng build --watch --configuration development”, “test”: “ng test” }, “private”: true, “dependencies”: { “@angular/common”: “^20.0.0”, “@angular/compiler”: “^20.0.0”, “@angular/core”: “^20.0.0”, “@angular/forms”: “^20.0.0”, “@angular/platform-browser”: “^20.0.0”, “@angular/router”: “^20.0.0”, “rxjs”: “~7.8.0”, “tslib”: “^2.3.0”, “zone.js”: “~0.15.0” }, “devDependencies”: { “@angular/build”: “^20.0.1”, “@angular/cli”: “^20.0.1”, “@angular/compiler-cli”: “^20.0.0”, “@stryker-mutator/core”: “^9.0.1”, “@stryker-mutator/karma-runner”: “^9.0.1”, “@types/jasmine”: “~5.1.0”, “jasmine-core”: “~5.7.0”, “karma”: “~6.4.0”, “karma-chrome-launcher”: “~3.2.0”, “karma-coverage”: “~2.2.0”, “karma-jasmine”: “~5.1.0”, “karma-jasmine-html-reporter”: “~2.1.0”, “typescript”: “~5.8.2” } }

I created the project using node 20.19.2. Tested on Macbook Pro 2018 (intel arc)

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw


Please provide the environment you discovered this bug in (run ng version)


Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 2
  • Comments: 32 (2 by maintainers)

Commits related to this issue

Most upvoted comments

It seems ng test --watch=false fixed the issue for me

I have singleRun: true. If I set singleRun to false, the unit tests are executed fine too. In both cases the unit tests are executed completely. The problem is that, for example, if I set singleRun to true I have the problem when it finish. If I set singleRun to false, the problem occurs when I stop the command with control + c.

================================================================================ ^CChrome Headless 137.0.0.0 (Mac OS 10.15.7) ERROR Some of your tests did a full page reload! 11 06 2025 21:20:34.786:WARN [Chrome Headless 137.0.0.0 (Mac OS 10.15.7)]: Disconnected (0 times) Client disconnected from CONNECTED state (transport close) Chrome Headless 137.0.0.0 (Mac OS 10.15.7) ERROR Disconnected Client disconnected from CONNECTED state (transport close) node:internal/webstreams/readablestream:1070 throw new ERR_INVALID_STATE.TypeError(‘Controller is already closed’); ^

TypeError [ERR_INVALID_STATE]: Invalid state: Controller is already closed at ReadableStreamDefaultController.enqueue (node:internal/webstreams/readablestream:1070:13) at /Users/username/…/projectname/node_modules/@angular/build/src/builders/karma/application_builder.js:253:28 at removeAllListeners (/Users/adriangonzalezrodrigo/Documents/Development/atiragym/node_modules/karma/lib/server.js:468:9) at /Users/username/…/projectname/node_modules/karma/lib/server.js:475:9 at Server.close (node:net:2284:9) at Object.onceWrapper (node:events:638:28) at Server.emit (node:events:536:35) at emitCloseNT (node:net:2344:8) at process.processTicksAndRejections (node:internal/process/task_queues:81:21) { code: ‘ERR_INVALID_STATE’ }

Node.js v20.19.2

I was able to reproduce the issue and the root cause is the following error “Some of your tests did a full page reload!”. I will open a PR with a fix.

Note: unless --no-watch is specified ng test will run in watch mode.

Here is a repo that reproduce the issue: https://github.com/phpbg/angular-issue-30506 You can also reproduce it with following steps (node 22.17.0):

  1. ng new angular-test
  2. cd angular-test
  3. ng generate config karma
  4. set singleRun: true within karma.config.js
  5. ng test

I had the same problem and also for some reason the test job was stuck on CI/CD pipeline. It just didn’t want to finish. Added --watch=false and the problem was gone.

It seems ng test --watch=false fixed the issue for me

This works for me. Thanks!! Do we knw why adding the watch=false it works? I don’t know if should be added by default when you create a new project from scratch using the cli tool. Or maybe a fix should be released.

Bonus: for those who are using Stryker mutator tool. The tool does not work because internally it executes ng test without watch=false. So you need to update the config adding:

  "karma": {
    "configFile": "karma.conf.js",
    "projectType": "angular-cli",
    "config": {
      "browsers": [
        "ChromeHeadless"
      ]
    },
  // add the next test arguments in your stryker.config.json
    "ngConfig": {
      "testArguments": {
        "watch": "false"
      }
    }
  },