You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 22, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+14-7Lines changed: 14 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
-
## Easy automock your react components
1
+
## Easy automock your external react components
2
2
3
-
Do you like write component tests? Do you like react-test-renderer more than shallow renderer? Do you feel sometimes that it would be great to combine both shallow renderer and react-test-renderer to prevent rendering inner components? Do you feel headcache when writing tests for something like [styled-components](https://www.styled-components.com/) ?
3
+
Do you like write component tests? Do you like react-test-renderer more than shallow renderer? Do you feel sometimes that it would be great to combine both shallow renderer and react-test-renderer to prevent rendering inner components? Do you feel headcache when writing tests for something like [styled-components](https://www.styled-components.com/) ? Are you bored to write numerous ```jest.mock()``` calls to mock your component?
4
4
5
-
If answer for these all questions is "Yes" then you come to the right place.
5
+
If the answer for any of these questions is "Yes" then you come to the right place.
6
6
7
7
8
8
Much often, you want to mock/don't render external components. Almost always these external components are being imported through import statement ```import A from "./a"```. And very often you want to full render the inner (helpers or styled) components:
@@ -90,15 +90,22 @@ Ideally, you want this snapshot:
90
90
91
91
```
92
92
93
-
This is achievable by mocking components using ```jest.mock()``` but this is boring and repetetive task, especially when you have to mock many components.
93
+
This is achievable by mocking components using ```jest.mock()``` but this is boring and repetetive task, especially when you have to mock many components:
94
+
95
+
```jsx
96
+
jest.mock("../AnotherComp", () =>"AnotherComp");
97
+
jest.mock("../Button", () =>"Button");
98
+
jest.mock("../SomeComp", () => ({ SomeComp:"SomeComp" })); // very ugly for named exports
99
+
// etc...
100
+
```
94
101
95
102
Finally, now you can just use ```jest-mock-external-components```:
// will automatically mock <Button /> and <AnotherComp />
102
109
103
110
// Use react-test-renderer
104
111
constt=create(<Component />);
@@ -122,4 +129,4 @@ Add to your ```.babelrc``` / ```.babelrc.js``` / ```babel.config.js```
122
129
```
123
130
124
131
## Caveats
125
-
After mocking, at beginning of the test file the ```jest.resetModules()``` will be called. Make sure you won't set internal state somewhere before (for example in setupFiles/setupTestFrameworkFile)
132
+
After mocking, at beginning of the test file the ```jest.resetModules()``` will be called. Make sure you are not setting internal state somewhere before (for example in setupFiles/setupTestFrameworkFile)
0 commit comments