bug(cdk/testing): TypeError: Cannot redefine property: defaultPrevented
Description
I recently moved to using harness testing to test opening and closing of dialogs in my app. I have multiple pages, each may have several scenarios where a confirmation/warning dialog is shown to the user and I have multiple tests for these scenarios on different spec files. Different tests fail depending on the order they run in (jasmine randomness), but the one thing that is consistent to all failed scenarios is the error appearing in the console:
TypeError: Cannot redefine property: defaultPrevented
Which stems from testbed/fake-events/event-objects#createKeyboardEvent.
After seeing this issue on webcomponents I made the following change in my local sources and all tests passed:
Object.defineProperty(event, 'defaultPrevented', { configurable: true, get: () => true });
I was unable to reproduce the scenario in stackblitz (all tests pass, no errors in console), but I'm not duplicating my full environment so I may be missing the trigger to this issue.
I suspect these issues are related to closing the dialog, as the Harness closes the dialog using the ESCAPE key. You can see in the stackblitz example I provided that I expect the number of open dialogs to be 0 after closing. In my failing tests, they sometimes fail that expectation.
Just to make sure I cover all bases, I noticed that when I added the dialog harness, I had to use harnesses for all other material components I was using on the page (table, button) or else tests would fail (sorry, I didn't document that issue, I "fixed it" and moved on). I don't know if this is related or not, but I thought that this dependency was a little curious.
Setup
Dependencies (partial): "@angular/cdk": "~9.2.1", "@angular/compiler": "~9.1.2", "@angular/core": "~9.1.2", "@angular/material": "^9.2.1",
Dev Dependencies (partial): "@angular-devkit/build-angular": "~0.901.1", "@angular/cli": "^9.1.1", "@angular/compiler-cli": "~9.1.2", "jasmine-core": "^3.5.0", "jasmine-spec-reporter": "^5.0.1", "karma": "^5.0.2", "karma-chrome-launcher": "^3.1.0", "karma-coverage-istanbul-reporter": "^2.1.1", "karma-jasmine": "^3.1.1", "karma-jasmine-html-reporter": "^1.5.3", "ts-node": "~7.0.0", "tslint": "~5.15.0", "typescript": "~3.8.3"
Environment: Google Chrome Version 81.0.4044.138 (Official Build) (64-bit) Mac OS Catalina Version 10.15.4
Thanks @ayeletdn for this issue. The dependency issue you mentioned for the harnesses seems unrelated, but definitely interesting. When you get a chance, that might be good to extract into a separate issue.
For the defaultPrevented issue. I wasn't able to reproduce this on my end in any standard harness close interactions, but I figured that calling preventDefault multiples times for these synthetic events, causes the error. So I'm guessing your application calls preventDefault somewhere, and this now conflicts.
Do you have a stacktrace that you could share for the cannot redeclare error? Either way, this issue already uncovered a bug in the synthetically created keyboard events.
Thanks @devversion for your quick response! I will make a point of getting back to the dependency issue soon. See attached below traces.
P.S. An additional note that may help? There's a single test which I can disable and gets all the tests to pass consistently, as far as I can tell. But there's nothing special about that test. It is very similar to the test on my aforementioned stackblitz named "should close dialog using Close button and increment". The main difference being the afterClose action being calling a service I spied on at the beginning of test, rather than expecting a counter to increment.
This is the full error message:
Uncaught TypeError: Cannot redefine property: defaultPrevented
at Function.defineProperty (<anonymous>)
at KeyboardEvent.event.preventDefault (testbed.js:176)
at SafeSubscriber._next (dialog.js:743)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
at SafeSubscriber.next (Subscriber.js:122)
at Subscriber._next (Subscriber.js:72)
at Subscriber.next (Subscriber.js:49)
at FilterSubscriber._next (filter.js:33)
at FilterSubscriber.next (Subscriber.js:49)
at Subject.next (Subject.js:39)
And this is the full stack trace.
event.preventDefault | @ | testbed.js:176
-- | -- | --
| (anonymous) | @ | dialog.js:743
| __tryOrUnsub | @ | Subscriber.js:183
| next | @ | Subscriber.js:122
| _next | @ | Subscriber.js:72
| next | @ | Subscriber.js:49
| _next | @ | filter.js:33
| next | @ | Subscriber.js:49
| next | @ | Subject.js:39
| OverlayKeyboardDispatcher._keydownListener | @ | overlay.js:1037
| invokeTask | @ | zone-evergreen.js:399
| push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvokeTask | @ | zone-testing.js:323
| invokeTask | @ | zone-evergreen.js:398
| runTask | @ | zone-evergreen.js:167
| invokeTask | @ | zone-evergreen.js:480
| invokeTask | @ | zone-evergreen.js:1621
| globalZoneAwareCallback | @ | zone-evergreen.js:1658
| dispatchEvent | @ | testbed.js:203
| dispatchKeyboardEvent | @ | testbed.js:218
| typeInElement | @ | testbed.js:304
| (anonymous) | @ | testbed.js:434
| fulfilled | @ | tslib.es6.js:71
| invoke | @ | zone-evergreen.js:364
| push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke | @ | zone-testing.js:292
| invoke | @ | zone-evergreen.js:363
| run | @ | zone-evergreen.js:123
| (anonymous) | @ | zone-evergreen.js:857
| invokeTask | @ | zone-evergreen.js:399
| push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvokeTask | @ | zone-testing.js:323
| invokeTask | @ | zone-evergreen.js:398
| runTask | @ | zone-evergreen.js:167
| drainMicroTaskQueue | @ | zone-evergreen.js:569
| invokeTask | @ | zone-evergreen.js:484
| ZoneTask.invoke | @ | zone-evergreen.js:469
| timer | @ | zone-evergreen.js:2552
| setTimeout (async) | |
| scheduleTask | @ | zone-evergreen.js:2573
| scheduleTask | @ | zone-evergreen.js:385
| push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onScheduleTask | @ | zone-testing.js:311
| scheduleTask | @ | zone-evergreen.js:378
| scheduleTask | @ | zone-evergreen.js:210
| scheduleMacroTask | @ | zone-evergreen.js:233
| scheduleMacroTaskWithCurrentZone | @ | zone-evergreen.js:1134
| (anonymous) | @ | zone-evergreen.js:2586
| proto.<computed> | @ | zone-evergreen.js:1449
| hostReportError | @ | hostReportError.js:2
| __tryOrUnsub | @ | Subscriber.js:191
| next | @ | Subscriber.js:122
| _next | @ | Subscriber.js:72
| next | @ | Subscriber.js:49
| _next | @ | filter.js:33
| next | @ | Subscriber.js:49
| next | @ | Subject.js:39
| OverlayKeyboardDispatcher._keydownListener | @ | overlay.js:1037
| invokeTask | @ | zone-evergreen.js:399
| push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvokeTask | @ | zone-testing.js:323
| invokeTask | @ | zone-evergreen.js:398
| runTask | @ | zone-evergreen.js:167
| invokeTask | @ | zone-evergreen.js:480
| invokeTask | @ | zone-evergreen.js:1621
| globalZoneAwareCallback | @ | zone-evergreen.js:1658
| dispatchEvent | @ | testbed.js:203
| dispatchKeyboardEvent | @ | testbed.js:218
| typeInElement | @ | testbed.js:304
| (anonymous) | @ | testbed.js:434
| fulfilled | @ | tslib.es6.js:71
| invoke | @ | zone-evergreen.js:364
| push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke | @ | zone-testing.js:292
| invoke | @ | zone-evergreen.js:363
| run | @ | zone-evergreen.js:123
| (anonymous) | @ | zone-evergreen.js:857
| invokeTask | @ | zone-evergreen.js:399
| push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvokeTask | @ | zone-testing.js:323
| invokeTask | @ | zone-evergreen.js:398
| runTask | @ | zone-evergreen.js:167
| drainMicroTaskQueue | @ | zone-evergreen.js:569
| invokeTask | @ | zone-evergreen.js:484
| ZoneTask.invoke | @ | zone-evergreen.js:469
| timer | @ | zone-evergreen.js:2552
| setTimeout (async) | |
| scheduleTask | @ | zone-evergreen.js:2573
| scheduleTask | @ | zone-evergreen.js:385
| scheduleTask | @ | zone-evergreen.js:210
| scheduleMacroTask | @ | zone-evergreen.js:233
| scheduleMacroTaskWithCurrentZone | @ | zone-evergreen.js:1134
| (anonymous) | @ | zone-evergreen.js:2586
| proto.<computed> | @ | zone-evergreen.js:1449
| (anonymous) | @ | zone-testing.js:973
| invoke | @ | zone-evergreen.js:364
| run | @ | zone-evergreen.js:123
| push../node_modules/zone.js/dist/zone-testing.js.AsyncTestZoneSpec._finishCallbackIfDone | @ | zone-testing.js:972
| push../node_modules/zone.js/dist/zone-testing.js.AsyncTestZoneSpec.onInvoke | @ | zone-testing.js:1037
| push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke | @ | zone-testing.js:289
| invoke | @ | zone-evergreen.js:363
| runGuarded | @ | zone-evergreen.js:133
| runInTestZone | @ | zone-testing.js:1154
| (anonymous) | @ | zone-testing.js:1092
| invoke | @ | zone-evergreen.js:364
| push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke | @ | zone-testing.js:292
| invoke | @ | zone-evergreen.js:363
| run | @ | zone-evergreen.js:123
| runInTestZone | @ | zone-testing.js:542
| (anonymous) | @ | zone-testing.js:558
| attempt | @ | jasmine.js?fbc7eaf33…48d4e7b4dc180c:6373
| QueueRunner.run | @ | jasmine.js?fbc7eaf33…48d4e7b4dc180c:6406
| runNext | @ | jasmine.js?fbc7eaf33…48d4e7b4dc180c:6325
| next | @ | jasmine.js?fbc7eaf33…48d4e7b4dc180c:6332
| (anonymous) | @ | jasmine.js?fbc7eaf33…48d4e7b4dc180c:6226
| invokeTask | @ | zone-evergreen.js:399
| runTask | @ | zone-evergreen.js:167
| drainMicroTaskQueue | @ | zone-evergreen.js:569
| Promise.then (async) | |
| scheduleMicroTask | @ | zone-evergreen.js:552
| scheduleTask | @ | zone-evergreen.js:388
| scheduleTask | @ | zone-evergreen.js:210
| scheduleMicroTask | @ | zone-evergreen.js:230
| (anonymous) | @ | zone-testing.js:573
| (anonymous) | @ | jasmine.js?fbc7eaf33…48d4e7b4dc180c:6422
| channel.port1.onmessage
We had same problem on our project. I couldn't reproduce it with running small amount of tests, but if we run all our ~700 unit tests this problem appears on tests with MatDialogHarness.close(). I have replaced all these places with clicking on close button and this error no longer appears.
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.