Skip to content

Commit 7b857d5

Browse files
authored
docs: move codeblock title to props & use line highlight (#11991)
1 parent 87a25b0 commit 7b857d5

File tree

107 files changed

+539
-1036
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+539
-1036
lines changed

docs/BypassingModuleMocks.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ Jest allows you to mock out whole modules in your tests, which can be useful for
77

88
Consider writing a test case for this `createUser` function:
99

10-
```javascript
11-
// createUser.js
10+
```javascript title="createUser.js"
1211
import fetch from 'node-fetch';
1312

1413
export const createUser = async () => {

docs/CodeTransformation.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ While `babel-jest` by default will transpile TypeScript files, Babel will not ve
138138

139139
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.
140140

141-
```js
142-
// fileTransformer.js
141+
```js title="fileTransformer.js"
143142
const path = require('path');
144143

145144
module.exports = {
@@ -149,9 +148,7 @@ module.exports = {
149148
};
150149
```
151150

152-
```js
153-
// jest.config.js
154-
151+
```js title="jest.config.js"
155152
module.exports = {
156153
transform: {
157154
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':

docs/Configuration.md

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ Jest's configuration can be defined in the `package.json` file of your project,
1616

1717
Or through JavaScript:
1818

19-
```js
20-
// jest.config.js
19+
```js title="jest.config.js"
2120
// Sync object
2221
/** @type {import('@jest/types').Config.InitialOptions} */
2322
const config = {
@@ -36,8 +35,7 @@ module.exports = async () => {
3635

3736
Or through TypeScript (if `ts-node` is installed):
3837

39-
```ts
40-
// jest.config.ts
38+
```ts title="jest.config.ts"
4139
import type {Config} from '@jest/types';
4240

4341
// Sync object
@@ -73,8 +71,7 @@ These options let you control Jest's behavior in your `package.json` file. The J
7371

7472
You can retrieve Jest's default options to expand them if needed:
7573

76-
```js
77-
// jest.config.js
74+
```js title="jest.config.js"
7875
const {defaults} = require('jest-config');
7976
module.exports = {
8077
// ...
@@ -99,8 +96,7 @@ This option tells Jest that all imported modules in your tests should be mocked
9996

10097
Example:
10198

102-
```js
103-
// utils.js
99+
```js title="utils.js"
104100
export default {
105101
authorize: () => {
106102
return 'token';
@@ -404,9 +400,7 @@ Test files are normally ignored from collecting code coverage. With this option,
404400

405401
For example, if you have tests in source files named with `.t.js` extension as following:
406402

407-
```javascript
408-
// sum.t.js
409-
403+
```javascript title="sum.t.js"
410404
export function sum(a, b) {
411405
return a + b;
412406
}
@@ -464,17 +458,15 @@ _Note: While code transformation is applied to the linked setup-file, Jest will
464458

465459
Example:
466460

467-
```js
468-
// setup.js
461+
```js title="setup.js"
469462
module.exports = async () => {
470463
// ...
471464
// Set reference to mongod in order to close the server during teardown.
472465
global.__MONGOD__ = mongod;
473466
};
474467
```
475468

476-
```js
477-
// teardown.js
469+
```js title="teardown.js"
478470
module.exports = async function () {
479471
await global.__MONGOD__.stop();
480472
};
@@ -735,8 +727,7 @@ Custom reporter modules must define a class that takes a `GlobalConfig` and repo
735727

736728
Example reporter:
737729

738-
```js
739-
// my-custom-reporter.js
730+
```js title="my-custom-reporter.js"
740731
class MyCustomReporter {
741732
constructor(globalConfig, options) {
742733
this._globalConfig = globalConfig;
@@ -816,8 +807,7 @@ For example, if you want to respect Browserify's [`"browser"` field](https://git
816807
}
817808
```
818809

819-
```js
820-
// resolver.js
810+
```js title="resolver.js"
821811
const browserResolve = require('browser-resolve');
822812

823813
module.exports = browserResolve.sync;
@@ -1303,8 +1293,7 @@ Example:
13031293

13041294
Sort test path alphabetically.
13051295

1306-
```js
1307-
// testSequencer.js
1296+
```js title="testSequencer.js"
13081297
const Sequencer = require('@jest/test-sequencer').default;
13091298

13101299
class CustomSequencer extends Sequencer {

docs/Es6ClassMocks.md

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ ES6 classes are constructor functions with some syntactic sugar. Therefore, any
1111

1212
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`.
1313

14-
```javascript
15-
// sound-player.js
14+
```javascript title="sound-player.js"
1615
export default class SoundPlayer {
1716
constructor() {
1817
this.foo = 'bar';
@@ -24,8 +23,7 @@ export default class SoundPlayer {
2423
}
2524
```
2625

27-
```javascript
28-
// sound-player-consumer.js
26+
```javascript title="sound-player-consumer.js"
2927
import SoundPlayer from './sound-player';
3028

3129
export default class SoundPlayerConsumer {
@@ -90,9 +88,7 @@ it('We can check if the consumer called a method on the class instance', () => {
9088

9189
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.
9290

93-
```javascript
94-
// __mocks__/sound-player.js
95-
91+
```javascript title="__mocks__/sound-player.js"
9692
// Import this named export into your test file:
9793
export const mockPlaySoundFile = jest.fn();
9894
const mock = jest.fn().mockImplementation(() => {
@@ -104,8 +100,7 @@ export default mock;
104100

105101
Import the mock and the mock method shared by all instances:
106102

107-
```javascript
108-
// sound-player-consumer.test.js
103+
```javascript title="sound-player-consumer.test.js"
109104
import SoundPlayer, {mockPlaySoundFile} from './sound-player';
110105
import SoundPlayerConsumer from './sound-player-consumer';
111106
jest.mock('./sound-player'); // SoundPlayer is now a mock constructor
@@ -198,8 +193,7 @@ If you define an ES6 class using the same filename as the mocked class in the `_
198193

199194
For the contrived example, the mock might look like this:
200195

201-
```javascript
202-
// __mocks__/sound-player.js
196+
```javascript title="__mocks__/sound-player.js"
203197
export default class SoundPlayer {
204198
constructor() {
205199
console.log('Mock SoundPlayer: constructor was called');
@@ -297,9 +291,7 @@ jest.mock('./sound-player', () => {
297291

298292
The manual mock equivalent of this would be:
299293

300-
```javascript
301-
// __mocks__/sound-player.js
302-
294+
```javascript title="__mocks__/sound-player.js"
303295
// Import this named export into your test file
304296
export const mockPlaySoundFile = jest.fn();
305297
const mock = jest.fn().mockImplementation(() => {
@@ -326,8 +318,7 @@ beforeEach(() => {
326318

327319
Here's a complete test file which uses the module factory parameter to `jest.mock`:
328320

329-
```javascript
330-
// sound-player-consumer.test.js
321+
```javascript title="sound-player-consumer.test.js"
331322
import SoundPlayer from './sound-player';
332323
import SoundPlayerConsumer from './sound-player-consumer';
333324

docs/GettingStarted.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ yarn add --dev babel-jest @babel/core @babel/preset-env
8989

9090
Configure Babel to target your current version of Node by creating a `babel.config.js` file in the root of your project:
9191

92-
```javascript
93-
// babel.config.js
92+
```javascript title="babel.config.js"
9493
module.exports = {
9594
presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
9695
};
@@ -102,8 +101,7 @@ _The ideal configuration for Babel will depend on your project._ See [Babel's do
102101

103102
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.
104103

105-
```javascript
106-
// babel.config.js
104+
```javascript title="babel.config.js"
107105
module.exports = api => {
108106
const isTest = api.env('test');
109107
// You can use isTest to determine what presets and plugins to use.
@@ -116,8 +114,7 @@ module.exports = api => {
116114

117115
> 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:
118116
119-
```javascript
120-
// jest.config.js
117+
```javascript title="jest.config.js"
121118
module.exports = {
122119
transform: {},
123120
};
@@ -160,12 +157,12 @@ yarn add --dev @babel/preset-typescript
160157

161158
Then add `@babel/preset-typescript` to the list of presets in your `babel.config.js`.
162159

163-
```diff
164-
// babel.config.js
160+
```javascript title="babel.config.js"
165161
module.exports = {
166162
presets: [
167163
['@babel/preset-env', {targets: {node: 'current'}}],
168-
+ '@babel/preset-typescript',
164+
// highlight-next-line
165+
'@babel/preset-typescript',
169166
],
170167
};
171168
```

docs/JestObjectAPI.md

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@ Jest configuration:
2525

2626
Example:
2727

28-
```js
29-
// utils.js
28+
```js title="utils.js"
3029
export default {
3130
authorize: () => {
3231
return 'token';
3332
},
3433
};
3534
```
3635

37-
```js
38-
// __tests__/disableAutomocking.js
36+
```js title="__tests__/disableAutomocking.js"
3937
import utils from '../utils';
4038

4139
jest.disableAutomock();
@@ -65,8 +63,7 @@ Returns the `jest` object for chaining.
6563
6664
Example:
6765

68-
```js
69-
// utils.js
66+
```js title="utils.js"
7067
export default {
7168
authorize: () => {
7269
return 'token';
@@ -75,8 +72,7 @@ export default {
7572
};
7673
```
7774

78-
```js
79-
// __tests__/enableAutomocking.js
75+
```js title="__tests__/enableAutomocking.js"
8076
jest.enableAutomock();
8177

8278
import utils from '../utils';
@@ -102,8 +98,7 @@ This is useful when you want to create a [manual mock](ManualMocks.md) that exte
10298

10399
Example:
104100

105-
```js
106-
// utils.js
101+
```js title="utils.js"
107102
export default {
108103
authorize: () => {
109104
return 'token';
@@ -112,8 +107,7 @@ export default {
112107
};
113108
```
114109

115-
```js
116-
// __tests__/createMockFromModule.test.js
110+
```js title="__tests__/createMockFromModule.test.js"
117111
const utils = jest.createMockFromModule('../utils').default;
118112
utils.isAuthorized = jest.fn(secret => secret === 'not wizard');
119113

@@ -147,8 +141,7 @@ Creates a new property with the same primitive value as the original property.
147141

148142
Example:
149143

150-
```js
151-
// example.js
144+
```js title="example.js"
152145
module.exports = {
153146
function: function square(a, b) {
154147
return a * b;
@@ -178,8 +171,7 @@ module.exports = {
178171
};
179172
```
180173

181-
```js
182-
// __tests__/example.test.js
174+
```js title="__tests__/example.test.js"
183175
const example = jest.createMockFromModule('./example');
184176

185177
test('should run example code', () => {
@@ -220,11 +212,11 @@ test('should run example code', () => {
220212

221213
Mocks a module with an auto-mocked version when it is being required. `factory` and `options` are optional. For example:
222214

223-
```js
224-
// banana.js
215+
```js title="banana.js"
225216
module.exports = () => 'banana';
217+
```
226218

227-
// __tests__/test.js
219+
```js title="__tests__/test.js"
228220
jest.mock('../banana');
229221

230222
const banana = require('../banana'); // banana will be explicitly mocked.

docs/JestPlatform.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,15 @@ Module used for parallelization of tasks. Exports a class `JestWorker` that take
123123

124124
### Example
125125

126-
```javascript
127-
// heavy-task.js
128-
126+
```javascript title="heavy-task.js"
129127
module.exports = {
130128
myHeavyTask: args => {
131129
// long running CPU intensive task.
132130
},
133131
};
134132
```
135133

136-
```javascript
137-
// main.js
138-
134+
```javascript title="main.js"
139135
async function main() {
140136
const worker = new Worker(require.resolve('./heavy-task.js'));
141137

docs/ManualMocks.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ When a manual mock exists for a given module, Jest's module system will use that
4040
4141
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.
4242

43-
```javascript
44-
// FileSummarizer.js
43+
```javascript title="FileSummarizer.js"
4544
'use strict';
4645

4746
const fs = require('fs');
@@ -58,8 +57,7 @@ exports.summarizeFilesInDirectorySync = summarizeFilesInDirectorySync;
5857

5958
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:
6059

61-
```javascript
62-
// __mocks__/fs.js
60+
```javascript title="__mocks__/fs.js"
6361
'use strict';
6462

6563
const path = require('path');
@@ -96,8 +94,7 @@ module.exports = fs;
9694

9795
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:
9896

99-
```javascript
100-
// __tests__/FileSummarizer-test.js
97+
```javascript title="__tests__/FileSummarizer-test.js"
10198
'use strict';
10299

103100
jest.mock('fs');

0 commit comments

Comments
 (0)