File tree Expand file tree Collapse file tree 2 files changed +12
-5
lines changed
examples/manual_mocks/__mocks__ Expand file tree Collapse file tree 2 files changed +12
-5
lines changed Original file line number Diff line number Diff line change @@ -64,6 +64,8 @@ that we can build on for our tests:
64
64
65
65
const path = require (' path' );
66
66
67
+ const fs = jest .genMockFromModule (' fs' );
68
+
67
69
// This is a custom function that our tests can use during setup to specify
68
70
// what the files on the "mock" filesystem should look like when any of the
69
71
// `fs` APIs are used.
@@ -86,8 +88,10 @@ function readdirSync(directoryPath) {
86
88
return mockFiles[directoryPath] || [];
87
89
}
88
90
89
- exports .__setMockFiles = __setMockFiles;
90
- exports .readdirSync = readdirSync;
91
+ fs .__setMockFiles = __setMockFiles;
92
+ fs .readdirSync = readdirSync;
93
+
94
+ module .exports = fs;
91
95
```
92
96
93
97
Now we write our test:
@@ -130,7 +134,7 @@ The example mock shown here uses [`jest.genMockFromModule`](/jest/docs/api.html#
130
134
to generate an automatic mock, and overrides its default behavior. This is the
131
135
recommended approach, but is completely optional. If you do not want to use the
132
136
automatic mock at all, you can simply export your own functions from the mock
133
- file. Of course, one downside to fully manual mocks is that they're manual –
137
+ file. One downside to fully manual mocks is that they're manual –
134
138
meaning you have to manually update them any time the module they are mocking
135
139
changes. Because of this, it's best to use or extend the automatic mock when it
136
140
works for your needs.
Original file line number Diff line number Diff line change 2
2
3
3
const path = require ( 'path' ) ;
4
4
5
+ const fs = jest . genMockFromModule ( 'fs' ) ;
6
+
5
7
// This is a custom function that our tests can use during setup to specify
6
8
// what the files on the "mock" filesystem should look like when any of the
7
9
// `fs` APIs are used.
@@ -24,6 +26,7 @@ function readdirSync(directoryPath) {
24
26
return mockFiles [ directoryPath ] || [ ] ;
25
27
}
26
28
27
- exports . __setMockFiles = __setMockFiles ;
28
- exports . readdirSync = readdirSync ;
29
+ fs . __setMockFiles = __setMockFiles ;
30
+ fs . readdirSync = readdirSync ;
29
31
32
+ module . exports = fs ;
You can’t perform that action at this time.
0 commit comments