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

Ariakit can't be imported when using Create React App and Jest #2400

Closed
Eldor901 opened this issue May 11, 2023 · 5 comments · Fixed by #2487 or #2489
Closed

Ariakit can't be imported when using Create React App and Jest #2400

Eldor901 opened this issue May 11, 2023 · 5 comments · Fixed by #2487 or #2489

Comments

@Eldor901
Copy link

Eldor901 commented May 11, 2023

Current behavior

image

Steps to reproduce the bug

https://github.com/Eldor901/Ariakit-jest

Expected behavior

Jest tests should not fail

Workaround

Eject and update the moduleFileExtensions option to include cjs.

Possible solutions

We can potentially fix this on our build script by including the .cjs extension on the package.json files inside the proxy folders:

main: join(prefix, mainDir, moduleName),
module: join(prefix, moduleDir, moduleName),

The same way we do here for the main package.json:

main: prod ? join(cjsDir, "index.cjs") : join(sourceDir, "index.ts"),
module: prod ? join(esmDir, "index.js") : join(sourceDir, "index.ts"),

@diegohaz
Copy link
Member

Could you please provide a link with the reproduction code (repository, CodeSandbox, StackBlitz)?

Anyway, I don't recommend using JSDOM (if you're using it with Jest) to test components that use Ariakit or any other accessible component library. You will need a real browser to test most interactions.

@Eldor901
Copy link
Author

Could you please provide a link with the reproduction code (repository, CodeSandbox, StackBlitz)?

Anyway, I don't recommend using JSDOM (if you're using it with Jest) to test components that use Ariakit or any other accessible component library. You will need a real browser to test most interactions.

@diegohaz https://github.com/Eldor901/Ariakit-jest.

@Eldor901
Copy link
Author

Is there a specific reason why you don't recommend JSDom and Jest for testing? I think Jest (with JSDom) is one of the most popular ways to write unit tests for JavaScript applications.

@diegohaz diegohaz changed the title Test cases that include areakit components can't be run with Jest Ariakit can't be imported when using Create React App and Jest May 14, 2023
@diegohaz
Copy link
Member

Thanks for the reproduction.

Looks like it's an issue with Create React App that ships with a custom moduleFileExtensions option that doesn't include cjs. There's an open PR on the CRA repo that would fix this: facebook/create-react-app#12605

The last CRA release was over a year ago, and it's super outdated. I don't recommend it for new projects. If you want to keep using it, I suggest ejecting (which would allow you to update the Jest config). Or just use Vite.

@diegohaz
Copy link
Member

Is there a specific reason why you don't recommend JSDom and Jest for testing? I think Jest (with JSDom) is one of the most popular ways to write unit tests for JavaScript applications.

You can use JSDOM, but you'll likely see a lot of act warnings from React. That's because Ariakit performs some actions asynchronously. JSDOM doesn't flush them automatically as the browser does, so you'll have to do extra work before and after triggering events.

We do use JSDOM to test Ariakit components internally, but we have an internal @ariakit/test package that makes JSDOM behave more like a real browser.

I recommend using a more robust testing framework like Playwright to test your components and pages.

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