Skip to content

Commit 7236d69

Browse files
cpojerFacebook Github Bot 4
authored andcommitted
Update ManualMocks docs.
Summary: Closes jestjs#860 Differential Revision: D3132497 fb-gh-sync-id: 3a14b16aad86f3fc5f7a22e4dfc1a9ee14d0e944 fbshipit-source-id: 3a14b16aad86f3fc5f7a22e4dfc1a9ee14d0e944
1 parent 0fbd7a8 commit 7236d69

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

docs/ManualMocks.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ that we can build on for our tests:
6464

6565
const path = require('path');
6666

67+
const fs = jest.genMockFromModule('fs');
68+
6769
// This is a custom function that our tests can use during setup to specify
6870
// what the files on the "mock" filesystem should look like when any of the
6971
// `fs` APIs are used.
@@ -86,8 +88,10 @@ function readdirSync(directoryPath) {
8688
return mockFiles[directoryPath] || [];
8789
}
8890

89-
exports.__setMockFiles = __setMockFiles;
90-
exports.readdirSync = readdirSync;
91+
fs.__setMockFiles = __setMockFiles;
92+
fs.readdirSync = readdirSync;
93+
94+
module.exports = fs;
9195
```
9296

9397
Now we write our test:
@@ -130,7 +134,7 @@ The example mock shown here uses [`jest.genMockFromModule`](/jest/docs/api.html#
130134
to generate an automatic mock, and overrides its default behavior. This is the
131135
recommended approach, but is completely optional. If you do not want to use the
132136
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 –
134138
meaning you have to manually update them any time the module they are mocking
135139
changes. Because of this, it's best to use or extend the automatic mock when it
136140
works for your needs.

examples/manual_mocks/__mocks__/fs.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const path = require('path');
44

5+
const fs = jest.genMockFromModule('fs');
6+
57
// This is a custom function that our tests can use during setup to specify
68
// what the files on the "mock" filesystem should look like when any of the
79
// `fs` APIs are used.
@@ -24,6 +26,7 @@ function readdirSync(directoryPath) {
2426
return mockFiles[directoryPath] || [];
2527
}
2628

27-
exports.__setMockFiles = __setMockFiles;
28-
exports.readdirSync = readdirSync;
29+
fs.__setMockFiles = __setMockFiles;
30+
fs.readdirSync = readdirSync;
2931

32+
module.exports = fs;

0 commit comments

Comments
 (0)