Inconsistant and invalid Events Typing using cypress
See original GitHub issueCurrent behavior:
Testing a code who used event instanceof KeyboardEvent
, I’ve seen some strange behavior, digging up, it seems that there is some strange inconsistencies between the events triggered and also into the way they are sent to the eventListener
.
Desired behavior:
Consistent behavior:
.type
.trigger({..., type: 'keydown'})
.get('#element').then(element.dispatchEvent(new KeyboardEvent('keydown', ...)))
Should all send a KeyboardEvent type event.
And the eventListener:
element.addEventListener('keydown', (event) => (event instanceof KeyboardEvent));
Should always return true.
Steps to reproduce: (app code and test code)
I’ve tested three different methods: cy.type
, cy.trigger
, cy.get('#element').then( element.dispatchEvent)
.
And two different eventListener
for each test:
- One directly into a <script> inside the html page itself
- One created into the test itself using
cy.get -> addEventListener
You can find MWE here: https://github.com/avallete/cypress-test-tiny-event-bug
Simply run:
npm install
npm run cypress:open
# Then run the test and check the console.log inside the debuging tools.
# Each test is a specific case the 2 first show that event is type Event instead of KeyboardEvent.
# The third test show the inconsistency of the same eventListener if it's declared inside the test or outside (into the html <script>).
Versions
3.6.1, Linux, Chromium 78 / Electron 73
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:17 (7 by maintainers)
Top Results From Across the Web
Changelog - Cypress Documentation
Fixed an issue with .type() where click events could be fired on the incorrect target element because the target focus changed within a...
Read more >Cypress does not always executes click on element
So basically, when Cypress executes the click function, it triggers all those events but somehow my component behave the way that it is ......
Read more >How to write end-to-end tests with Cypress and Node.js
Although for the sake of this tutorial, we're using Cypress on a public ... Then, we simulate a change event by typing in...
Read more >How to test for accessibility with Cypress - Deque Systems
Let's review how to create test cases, integrate and use axe to check for accessibility violations with Cypress, and enhance accessibility ...
Read more >Flaky Cypress Tests - What They Are & How To Deal With ...
The system clock is a type of data that cannot be known in advance. And, there are several tests that depend on this...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m running into this issue because I have some Rust code which uses typed bindings to DOM APIs. The bindings provide a number of type definitions generated from WebIDL, and it’s possible to cast between JS types. By default those casts use
instanceof
checks before returning to catch common bugs.I have event handler helpers that take a typed callback and perform the conversion under the hood for the user. Elsewhere there are definitions mapping strings like
"keydown"
to types so that the callbacks always have the correct type. This works quite nicely “in production” so far but breaks when using cypress to enter text due to this issue.I’ve had mixed results with removing the checks as it seems the bindings may rely on prototypes for doing their property lookups. The easiest fix that’d allow me to use cypress would be to emit an event with the correct prototype in its chain.
EDIT: I should also note that https://github.com/cypress-io/cypress/pull/8255 will address the most common cases.
There are 3 problems in this issue:
Event
-> `KeyboardEvent: Actually, this is also the solution for the #6125. I need to investigate what’s the real problem in that code. And it’ll be fixed with it. => #8255cy.window()
issue: I think it should be written in FAQ, cy.window(), and/or The Test Runner. => cypress-io/cypress-documentation#3073.trigger
issue: it should be treated separately.