@testing-library/user-event version: 12.0.11jest-environment-jsdom-fourteen)I'm having some issues with the following test-snippet (copied from the readme with some minor modification):
test('type', () => {
render(<textarea />);
userEvent.type(screen.getByRole('textbox'), 'hey');
expect(screen.getByRole('textbox')).toHaveValue('hey');
});
When I run this, it fails with the following output:

Whatever text i input, it chucks the first input-character at the end, (i.e. 123 -> 231).
I'm guessing this is not the expected outcome - or if so, what am I missing? 馃榿
I'm using fireEvent.change(screen.getByRole('textbox'), { target: { value: 'hey' } }) as a workaround for now.
I have reproduced this issue: https://codesandbox.io/s/pedantic-maxwell-wu50t?file=/src/__tests__/index.js
I'm guessing it's something odd with React because we absolutely have tests for this:
I'm not sure what the problem is, but if someone wants to investigate and figure out what we're doing wrong that would be great.
I just noticed this:
@testing-library/[email protected]" has unmet peer dependency "@testing-library/dom@>=7.16.0".
Might this be related?
I can take a look at this later.
This bug was introduced in #322 (version 11.3.1)
this issue is like #346. The problem is that when the textarea has no default value both selectionStart and selectionEnd are undefined. I have tried locally in the past but this issue not happens. It happens only in codesandbox.
We can add a check at the beginning of type function, but it's very odd
working example
diff --git a/src/type.js b/src/type.js
index 0bee11e..632dc97 100644
--- a/src/type.js
+++ b/src/type.js
@@ -65,6 +65,15 @@ async function typeImpl(
}
return value
}
+
+ if (
+ typeof currentElement().selectionStart === 'undefined' ||
+ typeof currentElement().selectionEnd === 'undefined'
+ ) {
+ currentElement().setSelectionRange(0, 0)
+ }
+
const setSelectionRange = ({newValue, newSelectionStart}) => {
// if we *can* change the selection start, then we will if the new value
// is the same as the current value (so it wasn't programatically changed
Is there any way we can alter the code in other places so we don't actually set the selection range?
Unfortunately I think it'll be hard to test this out until we can reproduce it locally. As far as I'm aware this bug only reproduces on Code Sandbox, but I don't think it supports linking packages.
Should we file this and #346 (which appears to still be an issue) upstream in JSDOM instead?
This issue and #346 have been fixed in JSDOM, but Code Sandbox's Jest support is still hard coded to use JSDOM 4.0.0, which doesn't have the fix: https://github.com/codesandbox/codesandbox-client/pull/1812/files#diff-3fcdeb2de813a974b29d8610b234c7f2R51
There are two ways to solve this:
textarea is broken, so making it work more like input could help)If anyone's curious, I've reproduced this issue using jest Code Sandbox, without any React or Testing Library packages: https://codesandbox.io/s/code-sandbox-bug-with-old-jsdom-version-9gcnp?file=/index.test.js
I think codesandbox will be making an effort to address some of these issues soon.
Do we want to close this to track it upstream instead of patching user-event? I already opened an issue at codesandbox/codesandbox-client#4530.
Yeah, I don't want to worry about supporting old versions of JSDOM
The issue has been resolved on codesandbox so this example is now working 馃槃
I can get this issue resolved on the codesandbox, but when i download the project from codesandbox and run locally, i still get this issue.
Is this still expected? Thanks in advance.
Code Sandbox has now pinned JSDOM 16, but create-react-app (which is used when downloading the sandbox) is still stuck on JSDOM 14. You can work around this by installing jest-environment-jsdom-sixteen and adding --env jest-environment-jsdom-sixteen to the test script until CRA is updated with Jest 26 support.
Hey guys, just stumbled on this. Is there a solution for this, or do I just need to install and set jest-environment-jsdom-sixteen?
You can check your jsdom version with npm ls jsdom or yarn why jsdom (make sure to use the same package manager as your project). If it's not at least 16, you'll likely want to use jest-environment-jsdom-sixteen.
Thanks @nickmccurdy, it solved this issue. Currently having the issue of #387, but that's another problem...
Most helpful comment
Code Sandbox has now pinned JSDOM 16, but create-react-app (which is used when downloading the sandbox) is still stuck on JSDOM 14. You can work around this by installing
jest-environment-jsdom-sixteenand adding--env jest-environment-jsdom-sixteento the test script until CRA is updated with Jest 26 support.