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: add solution for tests in component dir #1314

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Read the [full review here](https://clutch.co/profile/shakacode#reviews?sort_by=
- [Undefined Set](docs/common-errors.md#undefined-set)
- [Using TheRubyRacer](docs/common-errors.md#using-therubyracer)
- [HMR](docs/common-errors.md#hmr)

- [Tests in component directory](docs/common-errors.md#tests-in-component-directory)

After reading this README file, additional information about React-Rails can be found in the Wiki page:
https://github.com/reactjs/React-Rails/wiki
Expand Down
18 changes: 18 additions & 0 deletions docs/common-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [Undefined Set](#undefined-set)
- [Using TheRubyRacer](#using-therubyracer)
- [HMR](#hmr)
- [Tests in component directory](#tests-in-component-directory)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand Down Expand Up @@ -45,3 +46,20 @@ LibV8 itself is already [beyond version 7](https://github.com/cowboyd/libv8/rele
Check out [Enabling Hot Module Replacement (HMR)](https://github.com/shakacode/shakapacker/blob/master/docs/react.md#enabling-hot-module-replacement-hmr) in Shakapacker documentation.

One caveat is that currently you [cannot Server-Side Render along with HMR](https://github.com/reactjs/react-rails/issues/925#issuecomment-415469572).

## Tests in component directory

If your tests for react components reside alongside the component files in the `app/javascript/components` directory,
you will get `ModuleNotFoundError` in production environment
since test libraries are devDependencies.

To resolve this issue,
you need to specify a matching pattern in `appllication.js` and `server_rendering.js`.
For example, see the below code:

```js
// app/javascript/packs/application.js
const componentRequireContext = require.context('react_rails_components', true, /^(?!.*\.test)^\.\/.*$/)
const ReactRailsUJS = require('react_ujs')
ReactRailsUJS.useContext(componentRequireContext)
```