diff --git a/README.md b/README.md index 3506f1ba1..088ed3b4a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docs/common-errors.md b/docs/common-errors.md index 3521e7a19..e0d1a02e7 100644 --- a/docs/common-errors.md +++ b/docs/common-errors.md @@ -7,6 +7,7 @@ - [Undefined Set](#undefined-set) - [Using TheRubyRacer](#using-therubyracer) - [HMR](#hmr) +- [Tests in component directory](#tests-in-component-directory) @@ -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) +```