Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0930f24

Browse files
docs: add solution for tests in component dir (#1314)
* Add docs for tests in component dir * Add file path to the example code
1 parent d8076e7 commit 0930f24

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Read the [full review here](https://clutch.co/profile/shakacode#reviews?sort_by=
7575
- [Undefined Set](docs/common-errors.md#undefined-set)
7676
- [Using TheRubyRacer](docs/common-errors.md#using-therubyracer)
7777
- [HMR](docs/common-errors.md#hmr)
78-
78+
-[Tests in component directory](docs/common-errors.md#tests-in-component-directory)
7979

8080
After reading this README file, additional information about React-Rails can be found in the Wiki page:
8181
https://github.com/reactjs/React-Rails/wiki

‎docs/common-errors.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Undefined Set](#undefined-set)
88
- [Using TheRubyRacer](#using-therubyracer)
99
- [HMR](#hmr)
10+
- [Tests in component directory](#tests-in-component-directory)
1011

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

@@ -45,3 +46,20 @@ LibV8 itself is already [beyond version 7](https://github.com/cowboyd/libv8/rele
4546
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.
4647

4748
One caveat is that currently you [cannot Server-Side Render along with HMR](https://github.com/reactjs/react-rails/issues/925#issuecomment-415469572).
49+
50+
## Tests in component directory
51+
52+
If your tests for react components reside alongside the component files in the `app/javascript/components` directory,
53+
you will get `ModuleNotFoundError` in production environment
54+
since test libraries are devDependencies.
55+
56+
To resolve this issue,
57+
you need to specify a matching pattern in `appllication.js` and `server_rendering.js`.
58+
For example, see the below code:
59+
60+
```js
61+
// app/javascript/packs/application.js
62+
const componentRequireContext = require.context('react_rails_components', true, /^(?!.*\.test)^\.\/.*$/)
63+
const ReactRailsUJS = require('react_ujs')
64+
ReactRailsUJS.useContext(componentRequireContext)
65+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /