angular / angular-cli

CLI tool for Angular
https://cli.angular.io
MIT License
26.67k stars 11.98k forks source link

web/test-runner failing when zoneless #27706

Open cyrilletuzi opened 1 month ago

cyrilletuzi commented 1 month ago

Command

test

Is this a regression?

The previous version in which this bug was not present was

No response

Description

@angular-devkit/build-angular:web-test-runner is loading files from zone.js/testing even when in zoneless mode, thus resulting in ng test command failing.

Minimal Reproduction

Reproduction repository here. The repo is in the error case. You can uncomment polyfills in angular.json to see the working scenario.

Exception or Error

dist/test-out/browser/app.component.spec.js:

 🚧 404 network requests:
    - testing.js

 ❌ Browser tests did not start after 20000ms You can increase this timeout with the testsStartTimeout option. Check the browser logs or open the browser in debug mode for more information. 

Chrome: |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1/1 test files | 0 passed, 0 failed

Error while running tests.

Your Environment

Angular CLI: 18.0.1
Node: 20.13.1
Package Manager: npm 10.8.0
OS: darwin arm64

Angular: 18.0.0
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1800.1
@angular-devkit/build-angular   18.0.1
@angular-devkit/core            18.0.1
@angular-devkit/schematics      18.0.1
@angular/cli                    18.0.1
@schematics/angular             18.0.1
rxjs                            7.8.1
typescript                      5.4.5
zone.js                         0.14.6

Anything else relevant?

Pull Request which implemented @web/test-runner builder.

The missing testing.js from the error seems to come from this line.

The line which links it to zone.js/testing seems to be here.

What is strange is that this last file seems to have some behaviors to detect zone or zoneless, but it seems ineffective in the end, probably because of the import in the first file, which is always here.

berkon commented 1 month ago

I see the same issue even though not using zoneless. So maybe its a more general issue. I've followed various guides which in fact all state that just angular.json must be changed like this:

image

and in addition @web/test-runner must be installed. That's it. Somebody also said that @web/test-runner-core must be installed additionally to fix this error but it didn't help either.

OLEKSII-DROZDIUK commented 1 month ago

Have the same problem. It seems to me that jasmine is needed zone.js,and no doesn't matter karma runner you has or web-test-runner. So without "polyfills": ["zone.js", "zone.js/testing"] line in "test" block your tests doesnt work, mb need to await global fix from Angular team.

berkon commented 1 month ago

I think I found the solution for my problem. But it will probably not solve the initial issue mentioned here regarding the "Zoneless" approach.

Since Angular 15 they've introduced a new way of adding the polyfills. Additionally to the legacy src/polyfills.ts file, now adding the polyfills directly in angular.json like this is supported:

image

The code for the new experimental web test runner obviously only works when adding the polyfills this way. And additionally also the second entry zone.js/testing must be added to the array. Then it works as expected as far as I could see.