From 9cf3863a814c5ad768ffaa54405632590ad15615 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Wed, 4 Oct 2023 20:44:35 +0330 Subject: [PATCH 1/2] Add docs for tests in component dir --- README.md | 2 +- docs/common-errors.md | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3506f1ba..088ed3b4 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 3521e7a1..752fe63c 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,19 @@ 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 +const componentRequireContext = require.context('react_rails_components', true, /^(?!.*\.test)^\.\/.*$/) +const ReactRailsUJS = require('react_ujs') +ReactRailsUJS.useContext(componentRequireContext) +``` From 93e720ccb6ba47f98d84b860bc52d2a03d96e67a Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Wed, 4 Oct 2023 20:57:19 +0330 Subject: [PATCH 2/2] Add file path to the example code --- docs/common-errors.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/common-errors.md b/docs/common-errors.md index 752fe63c..e0d1a02e 100644 --- a/docs/common-errors.md +++ b/docs/common-errors.md @@ -58,6 +58,7 @@ you need to specify a matching pattern in `appllication.js` and `server_renderin 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)