Skip to content

ERR_INVALID_STATE.TypeError('Controller is already closed') when executing unit tests in default created project for Angular 20 #30506

Closed

Description

@adriangonzalezrodrigo

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

Activity

  1. JeanMeche commented on Jun 10, 2025

    @JeanMeche
    Member

    Which node version are you running it from ? v20 requires ^20.19.0 || ^22.12.0 || ^24.0.0

  2. adriangonzalezrodrigo commented on Jun 10, 2025

    @adriangonzalezrodrigo
    Author

    I tried with v20.19.2 and 22.16.0 doing a clean "install" (removing node_modules and package-lock.json)

  3. transferred this issue fromangular/angularon Jun 10, 2025
  4. 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
  5. phpbg commented on Jun 11, 2025

    @phpbg

    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...

  6. adriangonzalezrodrigo commented on Jun 11, 2025

    @adriangonzalezrodrigo
    Author

    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

  7. xidedix commented on Jun 12, 2025

    @xidedix

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

  8. adriangonzalezrodrigo commented on Jun 13, 2025

    @adriangonzalezrodrigo
    Author

    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"
          }
        }
      },
    
  9. sysmat commented on Jun 13, 2025

    @sysmat

    Same here

    • Node.js v22.16.0
    • ng cli 20.0.2
    • karama: singleRun: false, frameworks: ['jasmine', '@angular-devkit/build-angular'],
    • angular: "aot": true,
    • error:
    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
    
  10. sysmat commented on Jun 13, 2025

    @sysmat
    • I try ng test --watch=false --progress=false --browsers=ChromeHeadlessCI --code-coverage but the same error
  11. adriangonzalezrodrigo commented on Jun 13, 2025

    @adriangonzalezrodrigo
    Author

    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:

    // Karma configuration file, see link for more information
    // https://karma-runner.github.io/1.0/config/configuration-file.html
    
    module.exports = function (config) {
      config.set({
        basePath: '',
        frameworks: ['jasmine', '@angular-devkit/build-angular'],
        plugins: [
          require('karma-jasmine'),
          require('karma-chrome-launcher'),
          require('karma-coverage'),
          require('karma-mocha-reporter'),
        ],
        client: {
          jasmine: {
            // you can add configuration options for Jasmine here
            // the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html
            // for example, you can disable the random execution with `random: false`
            // or set a specific seed with `seed: 4321`
          },
          clearContext: false, // leave Jasmine Spec Runner output visible in browser
        },
        jasmineHtmlReporter: {
          suppressAll: true, // removes the duplicated traces
        },
        customLaunchers: {
          ChromeHeadless: {
            base: 'Chrome',
            flags: [
              '--headless',
              '--disable-gpu',
              '--disable-translate',
              '--disable-extensions',
              // Without a remote debugging port, Google Chrome exits immediately.
              '--no-sandbox',
              '--remote-debugging-port=9222',
              '--js-flags="--max_old_space_size=4096"',
            ],
          },
        },
        coverageReporter: {
          dir: require('path').join(__dirname, './coverage/projectname'),
          subdir: '.',
          reporters: [{ type: 'html' }, { type: 'lcovonly' }, { type: 'text' }, { type: 'text-summary' }],
        },
        reporters: ['mocha'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['ChromeHeadless'],
        singleRun: true,
        restartOnFileChange: true,
        concurrency: Infinity,
      });
    };
    
    
  12. ms-dosx86 commented on Jun 17, 2025

    @ms-dosx86
    Contributor

    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.

  13. redaaax commented on Jun 17, 2025

    @redaaax

    cant fix it, do you know why this is happening?

  14. dmtkomkov commented on Jun 26, 2025

    @dmtkomkov
  15. dmtkomkov commented on Jun 26, 2025

    @dmtkomkov
  16. 22 remaining items

  17. added theissue type on Jul 4, 2025
  18. added a commit that references this issue on Jul 4, 2025
    8c5cae5
  19. self-assigned this
    on Jul 4, 2025
  20. added 2 commits that reference this issue on Jul 4, 2025
    56aea48
    cbca278
  21. added 2 commits that reference this issue on Jul 8, 2025
    49676c8
    b67fdfd
  22. maks-humeniuk commented on Jul 9, 2025

    @maks-humeniuk

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

    @xidedix, that worked for me as well, thanks!

    However, I wonder why this isn't the same as adding watch: false in karma.conf.js.

  23. angular-automatic-lock-bot commented on Aug 12, 2025

    @angular-automatic-lock-bot

    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.

  24. locked and limited conversation to collaborators on Aug 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions