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
Copy file name to clipboardExpand all lines: docs/CodeTransformation.md
+2-5Lines changed: 2 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -138,8 +138,7 @@ While `babel-jest` by default will transpile TypeScript files, Babel will not ve
138
138
139
139
Importing images is a way to include them in your browser bundle, but they are not valid JavaScript. One way of handling it in Jest is to replace the imported value with its filename.
Copy file name to clipboardExpand all lines: docs/Es6ClassMocks.md
+7-16Lines changed: 7 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,8 +11,7 @@ ES6 classes are constructor functions with some syntactic sugar. Therefore, any
11
11
12
12
We'll use a contrived example of a class that plays sound files, `SoundPlayer`, and a consumer class which uses that class, `SoundPlayerConsumer`. We'll mock `SoundPlayer` in our tests for `SoundPlayerConsumer`.
13
13
14
-
```javascript
15
-
// sound-player.js
14
+
```javascript title="sound-player.js"
16
15
exportdefaultclassSoundPlayer {
17
16
constructor() {
18
17
this.foo='bar';
@@ -24,8 +23,7 @@ export default class SoundPlayer {
24
23
}
25
24
```
26
25
27
-
```javascript
28
-
// sound-player-consumer.js
26
+
```javascript title="sound-player-consumer.js"
29
27
importSoundPlayerfrom'./sound-player';
30
28
31
29
exportdefaultclassSoundPlayerConsumer {
@@ -90,9 +88,7 @@ it('We can check if the consumer called a method on the class instance', () => {
90
88
91
89
Create a [manual mock](ManualMocks.md) by saving a mock implementation in the `__mocks__` folder. This allows you to specify the implementation, and it can be used across test files.
92
90
93
-
```javascript
94
-
// __mocks__/sound-player.js
95
-
91
+
```javascript title="__mocks__/sound-player.js"
96
92
// Import this named export into your test file:
97
93
exportconstmockPlaySoundFile=jest.fn();
98
94
constmock=jest.fn().mockImplementation(() => {
@@ -104,8 +100,7 @@ export default mock;
104
100
105
101
Import the mock and the mock method shared by all instances:
@@ -102,8 +101,7 @@ _The ideal configuration for Babel will depend on your project._ See [Babel's do
102
101
103
102
Jest will set `process.env.NODE_ENV` to `'test'` if it's not set to something else. You can use that in your configuration to conditionally setup only the compilation needed for Jest, e.g.
104
103
105
-
```javascript
106
-
// babel.config.js
104
+
```javascript title="babel.config.js"
107
105
module.exports=api=> {
108
106
constisTest=api.env('test');
109
107
// You can use isTest to determine what presets and plugins to use.
@@ -116,8 +114,7 @@ module.exports = api => {
116
114
117
115
> Note: `babel-jest` is automatically installed when installing Jest and will automatically transform files if a babel configuration exists in your project. To avoid this behavior, you can explicitly reset the `transform` configuration option:
Copy file name to clipboardExpand all lines: docs/ManualMocks.md
+3-6Lines changed: 3 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,8 +40,7 @@ When a manual mock exists for a given module, Jest's module system will use that
40
40
41
41
Here's a contrived example where we have a module that provides a summary of all the files in a given directory. In this case, we use the core (built in) `fs` module.
Since we'd like our tests to avoid actually hitting the disk (that's pretty slow and fragile), we create a manual mock for the `fs` module by extending an automatic mock. Our manual mock will implement custom versions of the `fs` APIs that we can build on for our tests:
60
59
61
-
```javascript
62
-
// __mocks__/fs.js
60
+
```javascript title="__mocks__/fs.js"
63
61
'use strict';
64
62
65
63
constpath=require('path');
@@ -96,8 +94,7 @@ module.exports = fs;
96
94
97
95
Now we write our test. Note that we need to explicitly tell that we want to mock the `fs` module because it’s a core Node module:
0 commit comments