You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ERR_INVALID_STATE.TypeError('Controller is already closed') when executing unit tests in default created project for Angular 20 #30506
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).
changed the title [-]ERR_INVALID_STATE.TypeError('Controller is already closed') when executing unit tests in default created project[/-][+]ERR_INVALID_STATE.TypeError('Controller is already closed') when executing unit tests in default created project for Angular 20[/+]on Jun 10, 2025
Hi,
do you have singleRun: truein your karma configuration? I am experiencing the same error on angular 19, when migrating to esbuild with this config option...
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'
}
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"
}
}
},
Chrome Headless 137.0.0.0 (Linux x86_64) ERROR
An error was thrown in afterAll
error properties: Object({ longStack: 'TypeError: this.handler.handle is not a function
at http://localhost:9876/darwin_arm64-fastbuild-ST-46c76129e412/bin/packages/common/http/src/client.ts:683:57
at doInnerSub (node_modules/rxjs/src/internal/operators/mergeInternals.ts:71:15)
at outerNext (node_modules/rxjs/src/internal/operators/mergeInternals.ts:53:58)
at OperatorSubscriber._next (node_modules/rxjs/src/internal/operators/OperatorSubscriber.ts:70:13)
at OperatorSubscriber.next (node_modules/rxjs/src/internal/Subscriber.ts:71:12)
at _Observable._subscribe (node_modules/rxjs/src/internal/observable/innerFrom.ts:78:18)
at _Observable._trySubscribe (node_modules/rxjs/src/internal/Observable.ts:235:19)
at node_modules/rxjs/src/internal/Observable.ts:225:18
at errorContext (http:// ...
at http://localhost:9876/darwin_arm64-fastbuild-ST-46c7[612](https://git.arnes.si/portal-hub/urejanje-dns/dns-wc/-/jobs/1153169#L612)9e412/bin/packages/common/http/src/client.ts:683:57
at doInnerSub (node_modules/rxjs/src/internal/operators/mergeInternals.ts:71:15)
at outerNext (node_modules/rxjs/src/internal/operators/mergeInternals.ts:53:58)
at OperatorSubscriber._next (node_modules/rxjs/src/internal/operators/OperatorSubscriber.ts:70:13)
at OperatorSubscriber.next (node_modules/rxjs/src/internal/Subscriber.ts:71:12)
at _Observable._subscribe (node_modules/rxjs/src/internal/observable/innerFrom.ts:78:18)
at _Observable._trySubscribe (node_modules/rxjs/src/internal/Observable.ts:235:19)
at node_modules/rxjs/src/internal/Observable.ts:225:18
at errorContext (node_modules/rxjs/src/internal/util/errorContext.ts:29:5)
at _Observable.subscribe (node_modules/rxjs/src/internal/Observable.ts:211:5)
at ____________________Elapsed_96_ms__At__Fri_Jun_13_2025_05_23_51_GMT_0000__Coordinated_Universal_Time_ (http://localhost)
at Object.onScheduleTask (node_modules/zone.js/fesm2015/zone-testing.js:1954:26)
at _ZoneDelegate.scheduleTask (node_modules/zone.js/fesm2015/zone.js:411:51)
at Object.onScheduleTask (node_modules/zone.js/fesm2015/zone.js:273:65)
at _ZoneDelegate.scheduleTask (node_modules/zone.js/fesm2015/zone.js:411:51)
at ZoneImpl.scheduleTask (node_modules/zone.js/fesm2015/zone.js:207:43)
at ZoneImpl.scheduleMacroTask (node_modules/zone.js/fesm2015/zone.js:230:25)
at scheduleMacroTaskWithCurrentZone (node_modules/zone.js/fesm2015/zone.js:723:25)
Chrome Headless 137.0.0.0 (Linux x86_64): Executed 103 of 319 ERROR (1.171 secs / 0.63 secs)
Chrome Headless 137.0.0.0 (Linux x86_64): Executed 104 of 319 DISCONNECTED (1 min 1.189 secs / 0.637 secs)
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)
at /builds/portal-hub/urejanje-dns/dns-wc/frontend/node_modules/@angular/build/src/builders/karma/application_builder.js:253:28
at removeAllListeners (/builds/portal-hub/urejanje-dns/dns-wc/frontend/node_modules/karma/lib/server.js:468:9)
at /builds/portal-hub/urejanje-dns/dns-wc/frontend/node_modules/karma/lib/server.js:475:9
at Server.close (node:net:2358:9)
at Object.onceWrapper (node:events:632:28)
at Server.emit (node:events:518:28)
at emitCloseNT (node:net:2418:8)
at process.processTicksAndRejections (node:internal/process/task_queues:89:21) {
code: 'ERR_INVALID_STATE'
}
Node.js v22.16.0
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.
Activity
JeanMeche commentedon Jun 10, 2025
Which node version are you running it from ? v20 requires
^20.19.0 || ^22.12.0 || ^24.0.0adriangonzalezrodrigo commentedon Jun 10, 2025
I tried with v20.19.2 and 22.16.0 doing a clean "install" (removing node_modules and package-lock.json)
[-]ERR_INVALID_STATE.TypeError('Controller is already closed') when executing unit tests in default created project[/-][+]ERR_INVALID_STATE.TypeError('Controller is already closed') when executing unit tests in default created project for Angular 20[/+]phpbg commentedon Jun 11, 2025
Hi,
do you have
singleRun: truein your karma configuration? I am experiencing the same error on angular 19, when migrating to esbuild with this config option...adriangonzalezrodrigo commentedon Jun 11, 2025
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
xidedix commentedon Jun 12, 2025
It seems
ng test --watch=falsefixed the issue for meadriangonzalezrodrigo commentedon Jun 13, 2025
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:
sysmat commentedon Jun 13, 2025
Same here
singleRun: false,frameworks: ['jasmine', '@angular-devkit/build-angular'],"aot": true,sysmat commentedon Jun 13, 2025
ng test --watch=false --progress=false --browsers=ChromeHeadlessCI --code-coveragebut the same erroradriangonzalezrodrigo commentedon Jun 13, 2025
In case this can help to someone. Config for Angular 20:
Command in package.json:
"test": "ng test --watch=false --code-coverage true",karma.conf.js:
ms-dosx86 commentedon Jun 17, 2025
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=falseand the problem was gone.redaaax commentedon Jun 17, 2025
cant fix it, do you know why this is happening?
dmtkomkov commentedon Jun 26, 2025
dmtkomkov commentedon Jun 26, 2025
22 remaining items
fix(@angular/build): resolve "Controller is already closed" error in …
fix(@angular/build): resolve "Controller is already closed" error in …
fix(@angular/build): resolve "Controller is already closed" error in …
fix(@angular/build): resolve "Controller is already closed" error in …
fix(@angular/build): resolve "Controller is already closed" error in …
maks-humeniuk commentedon Jul 9, 2025
@xidedix, that worked for me as well, thanks!
However, I wonder why this isn't the same as adding
watch: falseinkarma.conf.js.angular-automatic-lock-bot commentedon Aug 12, 2025
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.