Error in click handler should break a test

This issue has been tracked since 2022-09-21.

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

core

Is this a regression?

Yes

Description

Currently, when a listener produces an error during the test execution, it doesn't affect the outcome of the test: the error is reported to the ErrorHandler and it logs it into the console.

See similar bugs #16821 and #46213.

Even if I patch the ErrorHandler to throw an error instead of just logging it the error will be swallowed without any reaction of TestBed and the test itself gets green.

Here the relevant parts:

function executeListenerWithErrorHandling(
lView: LView, context: {}|null, listenerFn: (e?: any) => any, e: any): boolean {
try {
profiler(ProfilerEvent.OutputStart, context, listenerFn);
// Only explicitly returning false from a listener should preventDefault
return listenerFn(e) !== false;
} catch (error) {
handleError(lView, error);
return false;
} finally {
profiler(ProfilerEvent.OutputEnd, context, listenerFn);
}
}

Please provide a link to a minimal reproduction of the bug

https://github.com/CSchulz/angular-dom-event-bug

Please provide the exception or error you saw

console.error
    ERROR Error: doStuff produced this error
        at MyComponent.doStuff (example-app-v13\src\app\exception.spec.ts:9:11)
        at MyComponent_Template_button_click_0_listener (ng:///MyComponent.js:10:22)
        at executeListenerWithErrorHandling (example-app-v13\node_modules\@angular\core\fesm2020\core.mjs:15045:16)
        at wrapListenerIn_markDirtyAndPreventDefault (example-app-v13\node_modules\@angular\core\fesm2020\core.mjs:15083:22)
        at HTMLButtonElement.<anonymous> (example-app-v13\node_modules\@angular\platform-browser\fesm2020\platform-browser.mjs:462:38)
        at _ZoneDelegate.Object.<anonymous>._ZoneDelegate.invokeTask (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:443:35)
        at ProxyZoneSpec.Object.<anonymous>.ProxyZoneSpec.onInvokeTask (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:3861:43)
        at _ZoneDelegate.Object.<anonymous>._ZoneDelegate.invokeTask (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:442:64)
        at Object.onInvokeTask (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:321:104)
        at _ZoneDelegate.Object.<anonymous>._ZoneDelegate.invokeTask (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:442:64)
        at Object.onInvokeTask (example-app-v13\node_modules\@angular\core\fesm2020\core.mjs:25621:33)
        at _ZoneDelegate.Object.<anonymous>._ZoneDelegate.invokeTask (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:442:64)
        at Zone.Object.<anonymous>.Zone.runTask (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:214:51)
        at ZoneTask.Object.<anonymous>.ZoneTask.invokeTask [as invoke] (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:525:38)
        at invokeTask (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:1727:22)
        at globalCallback (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:1758:31)
        at HTMLButtonElement.globalZoneAwareCallback (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:1794:20)
        at HTMLButtonElement.callTheUserObjectsOperation (example-app-v13\node_modules\jsdom\lib\jsdom\living\generated\EventListener.js:26:30)
        at innerInvokeEventListeners (example-app-v13\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:340:25)
        at invokeEventListeners (example-app-v13\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:276:3)
        at HTMLButtonElementImpl._dispatch (example-app-v13\node_modules\jsdom\lib\jsdom\living\events\EventTarget-impl.js:223:9)
        at fireAnEvent (example-app-v13\node_modules\jsdom\lib\jsdom\living\helpers\events.js:18:36)
        at HTMLButtonElementImpl.click (example-app-v13\node_modules\jsdom\lib\jsdom\living\nodes\HTMLElement-impl.js:79:5)
        at HTMLButtonElement.click (example-app-v13\node_modules\jsdom\lib\jsdom\living\generated\HTMLElement.js:108:34)
        at example-app-v13\src\app\exception.spec.ts:19:12
        at _ZoneDelegate.Object.<anonymous>._ZoneDelegate.invoke (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:409:30)
        at ProxyZoneSpec.Object.<anonymous>.ProxyZoneSpec.onInvoke (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:3830:43)
        at _ZoneDelegate.Object.<anonymous>._ZoneDelegate.invoke (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:408:56)
        at Zone.Object.<anonymous>.Zone.run (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:169:47)
        at Object.wrappedFunc (example-app-v13\node_modules\zone.js\bundles\zone-testing-bundle.umd.js:4330:34)
        at Promise.then.completed (example-app-v13\node_modules\jest-circus\build\utils.js:333:28)
        at new Promise (<anonymous>)
        at callAsyncCircusFn (example-app-v13\node_modules\jest-circus\build\utils.js:259:10)
        at _callCircusTest (example-app-v13\node_modules\jest-circus\build\run.js:277:40)
        at processTicksAndRejections (node:internal/process/task_queues:96:5)
        at _runTest (example-app-v13\node_modules\jest-circus\build\run.js:209:3)
        at _runTestsForDescribeBlock (example-app-v13\node_modules\jest-circus\build\run.js:97:9)
        at _runTestsForDescribeBlock (example-app-v13\node_modules\jest-circus\build\run.js:91:9)
        at run (example-app-v13\node_modules\jest-circus\build\run.js:31:3)
        at runAndTransformResultsToJestFormat (example-app-v13\node_modules\jest-circus\build\legacy-code-todo-rewrite\jestAdapterInit.js:135:21)

      at defaultErrorLogger (node_modules/@angular/core/fesm2020/core.mjs:6509:13)
      at ErrorHandler.handleError (node_modules/@angular/core/fesm2020/core.mjs:6556:9)
      at handleError (node_modules/@angular/core/fesm2020/core.mjs:11009:34)
      at executeListenerWithErrorHandling (node_modules/@angular/core/fesm2020/core.mjs:15048:9)
      at wrapListenerIn_markDirtyAndPreventDefault (node_modules/@angular/core/fesm2020/core.mjs:15083:22)
      at HTMLButtonElement.<anonymous> (node_modules/@angular/platform-browser/fesm2020/platform-browser.mjs:462:38)
      at _ZoneDelegate.Object.<anonymous>._ZoneDelegate.invokeTask (node_modules/zone.js/bundles/zone-testing-bundle.umd.js:443:35)

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

Angular CLI: 13.3.9
Node: 16.14.2
Package Manager: yarn 1.22.19
OS: win32 x64

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

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1303.9
@angular-devkit/build-angular   13.3.9
@angular-devkit/core            13.3.9
@angular-devkit/schematics      13.3.9
@angular/cli                    13.3.9
@schematics/angular             13.3.9
rxjs                            7.5.6
typescript                      4.5.5

Anything else?

No response

runnyren wrote this answer on 2022-09-26

Interesting

More Details About Repo
Owner Name angular
Repo Name angular
Full Name angular/angular
Language TypeScript
Created Date 2014-09-18
Updated Date 2022-09-30
Star Count 84091
Watcher Count 3064
Fork Count 22233
Issue Count 1203

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date