question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

userEvent.type with empty string causes a warning about an unhandled promise rejection

See original GitHub issue
  • @testing-library/user-event version: 13.0.16
  • Testing Framework and version: jest 26.6.0 (create-react-app)
  • DOM Environment:
    • jsdom: 16.5.2
    • jest-environment-jsdom: 26.6.2

Relevant code or config

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

test('user event empty text', async () => {
  render(
    <>
      <label htmlFor='input'>Input</label>
      <input id='input' />
    </>,
  );

  userEvent.type(screen.getByLabelText('Input'), '');
});

What you did:

I used userEvent.type with an empty string.

What happened:

Running the tests with jest (npm test, is a CRA project) causes node.js to emit this warning:

(node:48415) UnhandledPromiseRejectionWarning: Error: Expected key descriptor but found "undefined" in ""

Tested with node.js 12.21.0 and 14.16.0.

The warning seems to be an unhandled rejected promise, so an actual error was thrown.

The warning was not emitted using user-event 12 (the default that came with CRA).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
JohnAlbincommented, Jun 18, 2022

FYI, I had the same problem. Thanks for the info.

In my case, I had a helper function that passed the text as a variable to the type method; e.g. userEvent.type(element, text)

I solved this by modifying my code to type a TAB character if the given string was empty. userEvent.type(element, text || '{tab}')

And that worked great because it removed focus from the element (and then triggered the “required” error message on my form.)

3reactions
InExtremaRescommented, Mar 30, 2021

@ph-fritsche I caught this error in a suite that tested input validation. There was a set of strings to be used as inputs and the expected messages the use would see, something like this:

[
  ['abc', 'Too short'],
  ['&ab', 'No symbols allowed'],
  ['', 'Required'], // <-----------
 // ... an so on...
]

The input field is required so clicking it and leave it empty should mark it as such.

I understand this can be accomplish but other means, like userType.click then validate without typing. In fact, after see the error just reported I change the code to filter out falsy input values.

However I don’t see why passing an empty string is an error. As I said I expected such behaviour to be equivalent to click the input and then do nothing. Besides, the error is very cryptic, I thought “what key descriptor are you talking about?”, I didn’t realize until I saw your PR. Is that error really expected even if I never used { o [?

Read more comments on GitHub >

github_iconTop Results From Across the Web

userEvent.type with empty string causes a warning about ...
userEvent.type with empty string causes a warning about an unhandled promise rejection.
Read more >
Why clear method not exist on @testing-library/user-event
It looks like those types are missing two of the documented methods - toggleSelectOptions isn't there either. They're both there in the latest ......
Read more >
Head First Java
features, memory management, and best of all—the promise of portability. We'll take a quick dip and write some code, compile it, and run...
Read more >
sentry/browser
1, {"version":3,"file":"bundle.es6.min.js","sources":["../../types/src/loglevel.ts","../../types/src/session.ts","../../types/src/severity.ts","../.
Read more >
invalid options object. dev server has been initialized using an ...
const { createProxyMiddleware } = require('http-proxy-middleware'); module.exports = function(app) { app.use( '/api', createProxyMiddleware({ target: 'http:// ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found