Skip to content

Jest tests not allowed in components directory. #1095

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

Closed
radglob opened this issue Dec 2, 2020 · 1 comment · Fixed by #1314
Closed

Jest tests not allowed in components directory. #1095

radglob opened this issue Dec 2, 2020 · 1 comment · Fixed by #1314
Assignees
Labels

Comments

@radglob
Copy link

radglob commented Dec 2, 2020

I recently added react-rails to an existing project, and production builds are failing if there are Jest test files in the /components directory.

Steps to reproduce

With the example structure:

app/javascript/components:
  component.jsx
  __tests__:
    component.test.jsx

where component.test.jsx imports react-test-renderer.

Run:

export NODE_ENV=production RAILS_ENV=production
bundle install --deployment --quiet --without test development && yarn install --silent
bundle exec rails assets:precompile --silent

I get a ModuleNotFoundError in component.test.jsx because it cannot find react-test-renderer.
This makes sense to me because react-test-renderer is in my devDependencies and should not have been installed.
However, I have other components and related test files outside of the components directory that do not get this failure.

Removing the test file from components fixes the error. This seems easily resolved if I was able to set an ignore pattern for react-rails to ignore test files.

Happy to provide more detail here.

System configuration

Sprockets or Webpacker version: Webpacker 4.3.0
React-Rails version: 2.6.1
Rect_UJS version: 2.6.0
Rails version: 5.2.1
Ruby version: 2.5.7

@radglob radglob changed the title Jest tests not allowed in components library. Jest tests not allowed in components directory. Dec 2, 2020
@ahangarha ahangarha added the bug label Jan 5, 2023
@RyanofWoods
Copy link

RyanofWoods commented Jul 10, 2023

I have also encountered this problem. To avoid this, I passed in custom regexp torequire.context in application.js, and server_rendering.js. It uses a negative lookahead to skip test files like so:

const componentRequireContext = require.context('react_rails_components', true, /^(?!.*\.test)^\.\/.*$/)
const ReactRailsUJS = require('react_ujs')
ReactRailsUJS.useContext(componentRequireContext)

The regexp is may look overly complicated, but it's based on the default ands needs to be able to match quite a few cases:

Must match for example:
./parentDirectory
./parentDirectory/
./parentDirectory/index
./parentDirectory/index.tsx
./parentDirectory/child
./parentDirectory/child/
./parentDirectory/child/index
./parentDirectory/child/index.ts

Should not match:
./parentDirectory/index.test.tsx

It should match the directories and files without the extension, as react-rails requires all these keys for react_component. Otherwise, a TypeError: Illegal constructor error will occur on page load.

A recommendation of mine would be to either update the readme.md to include this Gotcha, or we update the default in the generator.

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

Successfully merging a pull request may close this issue.

3 participants