Skip to content

Commit

Permalink
docs: add solution for tests in component dir (#1314)
Browse files Browse the repository at this point in the history
* Add docs for tests in component dir

* Add file path to the example code
  • Loading branch information
ahangarha authored Oct 6, 2023
1 parent d8076e7 commit 0930f24
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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)
```

0 comments on commit 0930f24

Please sign in to comment.