Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: reintroduce import/require usage for user-event #1143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nickserv
Copy link
Member

It seems like when the docs for user-event 14 were released, the docs on import/require usage were removed. However, this reintroduces issues like testing-library/user-event#485 if you don't already know how to destructure the default export from the CJS module. I fixed this by copying my docs from user-event 13 (testing-library/user-event#495).

@nickserv nickserv requested a review from ph-fritsche August 22, 2022 05:24
@netlify
Copy link

netlify bot commented Aug 22, 2022

Deploy Preview for testing-library ready!

Name Link
🔨 Latest commit a45ced8
🔍 Latest deploy log https://app.netlify.com/sites/testing-library/deploys/630312f60e8cae0008ef16c7
😎 Deploy Preview https://deploy-preview-1143--testing-library.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Copy link
Member

@ph-fritsche ph-fritsche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels redundant. But if it helps someone, it probably doesn't hurt to add it.

@nickserv
Copy link
Member Author

nickserv commented Aug 22, 2022

I know, but without this there isn't any mention on how to safely use user-event with CJS, and since this issue doesn't apply to other Testing Library packages, I don't think it would be obvious.

If you have any suggestions on how to simplify this while still safely importing user-event, I'm open to it. Otherwise, I think it would be a good idea to merge.

Comment on lines +31 to +37
```js
import userEvent from '@testing-library/user-event'

// or

const {default: userEvent} = require('@testing-library/user-event')
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should use Tabs here too. (Backslash to make the suggestion display.)

Suggested change
```js
import userEvent from '@testing-library/user-event'
// or
const {default: userEvent} = require('@testing-library/user-event')
```
<Tabs defaultValue="esm" values={[
{ label: 'In EcmaScript Module', value: 'esm' },
{ label: 'In CommonJS', value: 'cjs' },
]}>
<TabItem value="esm">
```js
import userEvent from '@testing-library/user-event'
\```
</TabItem>
<TabItem value="cjs">
```js
const {default: userEvent} = require('@testing-library/user-event')
\```
</TabItem>
</Tabs>

Copy link
Member Author

@nickserv nickserv Aug 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking it would be nice to show both variants by default in case users don't understand the difference between EcmaScript and CommonJS modules without seeing code. I also can't seem to get this to parse with const {default: userEvent}, even though it's valid ES6.


// or

const {default: userEvent} = require('@testing-library/user-event')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be automatically happen when you write import userEvent from '@testing-library/user-event' and let it transpile by Babel?
This seems like a step backwards regarding progress of ES6 modules in the ecosystem so I would encourage user-event to get this right. You probably want to ship ES6 modules and CommonJS in module and main entrypoints respectively.

Copy link
Member Author

@nickserv nickserv Aug 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be automatically happen when you write import userEvent from '@testing-library/user-event' and let it transpile by Babel?

Yes, but not everyone uses Babel. Specifically, Testing Library Recorder Extension needs to use CJS modules for now because it can't introduce configuration to enable Babel or Jest's experimental ES modules (which in my experience do not work well with that project's dependencies).

I edited the documentation to fix this previously, but it wasn't included in the new docs for version 14. I also don't want to confuse users if they try to type a user-event import manually when editing tests from the extension.

You probably want to ship ES6 modules and CommonJS in module and main entrypoints respectively.

user-event does do this. Are you suggesting there is a build configuration issue with user-event that can be solved without changing the docs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot import userEvent after installing user-event
3 participants