Skip to content

Commit 3f78547

Browse files
authored
docs: improve setupFilesAfterEnv documentation (#12846)
1 parent 39bad8c commit 3f78547

6 files changed

Lines changed: 58 additions & 123 deletions

File tree

docs/Configuration.md

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,38 +1129,23 @@ Default: `[]`
11291129

11301130
A list of paths to modules that run some code to configure or set up the testing framework before each test file in the suite is executed. Since [`setupFiles`](#setupfiles-array) executes before the test framework is installed in the environment, this script file presents you the opportunity of running some code immediately after the test framework has been installed in the environment but before the test code itself.
11311131

1132-
If you want a path to be [relative to the root directory of your project](#rootdir-string), please include `<rootDir>` inside a path's string, like `"<rootDir>/a-configs-folder"`.
1132+
In other words, `setupFilesAfterEnv` modules are meant for code which is repeating in each test file. Having the test framework installed makes Jest [globals](GlobalAPI.md), [`jest` object](JestObjectAPI.md) and [`expect`](ExpectAPI.md) accessible in the modules. For example, you can add extra matchers from [`jest-extended`](https://github.com/jest-community/jest-extended) library or call [setup and teardown](SetupAndTeardown.md) hooks:
11331133

1134-
For example, Jest ships with several plug-ins to `jasmine` that work by monkey-patching the jasmine API. If you wanted to add even more jasmine plugins to the mix (or if you wanted some custom, project-wide matchers for example), you could do so in these modules.
1134+
```js title="setup-jest.js"
1135+
const matchers = require('jest-extended');
1136+
expect.extend(matchers);
11351137

1136-
Example `setupFilesAfterEnv` array in a jest.config.js:
1138+
afterEach(() => {
1139+
jest.useRealTimers();
1140+
});
1141+
```
11371142

11381143
```js
11391144
module.exports = {
1140-
setupFilesAfterEnv: ['./jest.setup.js'],
1145+
setupFilesAfterEnv: ['<rootDir>/setup-jest.js'],
11411146
};
11421147
```
11431148

1144-
Example `jest.setup.js` file
1145-
1146-
```js
1147-
jest.setTimeout(10000); // in milliseconds
1148-
1149-
// you can even use the setup/teardown methods
1150-
beforeAll(() => {
1151-
// your code
1152-
});
1153-
beforeEach(() => {
1154-
// your code
1155-
});
1156-
afterEach(() => {
1157-
// your code
1158-
});
1159-
afterAll(() => {
1160-
// your code
1161-
});
1162-
```
1163-
11641149
### `slowTestThreshold` \[number]
11651150

11661151
Default: `5`

website/versioned_docs/version-25.x/Configuration.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -881,32 +881,31 @@ A list of paths to modules that run some code to configure or set up the testing
881881

882882
Default: `[]`
883883

884-
A list of paths to modules that run some code to configure or set up the testing framework before each test file in the suite is executed. Since [`setupFiles`](#setupfiles-array) executes before the test framework is installed in the environment, this script file presents you the opportunity of running some code immediately after the test framework has been installed in the environment but before the test code itself.
884+
:::tip
885885

886-
If you want a path to be [relative to the root directory of your project](#rootdir-string), please include `<rootDir>` inside a path's string, like `"<rootDir>/a-configs-folder"`.
886+
Renamed from `setupTestFrameworkScriptFile` in Jest 24.
887887

888-
For example, Jest ships with several plug-ins to `jasmine` that work by monkey-patching the jasmine API. If you wanted to add even more jasmine plugins to the mix (or if you wanted some custom, project-wide matchers for example), you could do so in these modules.
888+
:::
889889

890-
:::note
890+
A list of paths to modules that run some code to configure or set up the testing framework before each test file in the suite is executed. Since [`setupFiles`](#setupfiles-array) executes before the test framework is installed in the environment, this script file presents you the opportunity of running some code immediately after the test framework has been installed in the environment but before the test code itself.
891891

892-
`setupTestFrameworkScriptFile` is deprecated in favor of `setupFilesAfterEnv`.
892+
In other words, `setupFilesAfterEnv` modules are meant for code which is repeating in each test files. Having the test framework installed makes Jest [globals](GlobalAPI.md), [`jest` object](JestObjectAPI.md) and [`expect`](ExpectAPI.md) accessible in the modules. For example, you can add extra matchers from [`jest-extended`](https://github.com/jest-community/jest-extended) library or call [setup and teardown](SetupAndTeardown.md) hooks:
893893

894-
:::
894+
```js title="setup-jest.js"
895+
const matchers = require('jest-extended');
896+
expect.extend(matchers);
895897

896-
Example `setupFilesAfterEnv` array in a jest.config.js:
898+
afterEach(() => {
899+
jest.useRealTimers();
900+
});
901+
```
897902

898903
```js
899904
module.exports = {
900-
setupFilesAfterEnv: ['./jest.setup.js'],
905+
setupFilesAfterEnv: ['<rootDir>/setup-jest.js'],
901906
};
902907
```
903908

904-
Example `jest.setup.js` file
905-
906-
```js
907-
jest.setTimeout(10000); // in milliseconds
908-
```
909-
910909
### `snapshotResolver` \[string]
911910

912911
Default: `undefined`

website/versioned_docs/version-26.x/Configuration.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -905,24 +905,23 @@ Default: `[]`
905905

906906
A list of paths to modules that run some code to configure or set up the testing framework before each test file in the suite is executed. Since [`setupFiles`](#setupfiles-array) executes before the test framework is installed in the environment, this script file presents you the opportunity of running some code immediately after the test framework has been installed in the environment but before the test code itself.
907907

908-
If you want a path to be [relative to the root directory of your project](#rootdir-string), please include `<rootDir>` inside a path's string, like `"<rootDir>/a-configs-folder"`.
908+
In other words, `setupFilesAfterEnv` modules are meant for code which is repeating in each test files. Having the test framework installed makes Jest [globals](GlobalAPI.md), [`jest` object](JestObjectAPI.md) and [`expect`](ExpectAPI.md) accessible in the modules. For example, you can add extra matchers from [`jest-extended`](https://github.com/jest-community/jest-extended) library or call [setup and teardown](SetupAndTeardown.md) hooks:
909909

910-
For example, Jest ships with several plug-ins to `jasmine` that work by monkey-patching the jasmine API. If you wanted to add even more jasmine plugins to the mix (or if you wanted some custom, project-wide matchers for example), you could do so in these modules.
910+
```js title="setup-jest.js"
911+
const matchers = require('jest-extended');
912+
expect.extend(matchers);
911913

912-
Example `setupFilesAfterEnv` array in a jest.config.js:
914+
afterEach(() => {
915+
jest.useRealTimers();
916+
});
917+
```
913918

914919
```js
915920
module.exports = {
916-
setupFilesAfterEnv: ['./jest.setup.js'],
921+
setupFilesAfterEnv: ['<rootDir>/setup-jest.js'],
917922
};
918923
```
919924

920-
Example `jest.setup.js` file
921-
922-
```js
923-
jest.setTimeout(10000); // in milliseconds
924-
```
925-
926925
### `slowTestThreshold` \[number]
927926

928927
Default: `5`

website/versioned_docs/version-27.x/Configuration.md

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -936,41 +936,23 @@ Default: `[]`
936936

937937
A list of paths to modules that run some code to configure or set up the testing framework before each test file in the suite is executed. Since [`setupFiles`](#setupfiles-array) executes before the test framework is installed in the environment, this script file presents you the opportunity of running some code immediately after the test framework has been installed in the environment but before the test code itself.
938938

939-
If you want a path to be [relative to the root directory of your project](#rootdir-string), please include `<rootDir>` inside a path's string, like `"<rootDir>/a-configs-folder"`.
939+
In other words, `setupFilesAfterEnv` modules are meant for code which is repeating in each test files. Having the test framework installed makes Jest [globals](GlobalAPI.md), [`jest` object](JestObjectAPI.md) and [`expect`](ExpectAPI.md) accessible in the modules. For example, you can add extra matchers from [`jest-extended`](https://github.com/jest-community/jest-extended) library or call [setup and teardown](SetupAndTeardown.md) hooks:
940940

941-
For example, Jest ships with several plug-ins to `jasmine` that work by monkey-patching the jasmine API. If you wanted to add even more jasmine plugins to the mix (or if you wanted some custom, project-wide matchers for example), you could do so in these modules.
941+
```js title="setup-jest.js"
942+
const matchers = require('jest-extended');
943+
expect.extend(matchers);
942944

943-
Example `setupFilesAfterEnv` array in a jest.config.js:
945+
afterEach(() => {
946+
jest.useRealTimers();
947+
});
948+
```
944949

945950
```js
946951
module.exports = {
947-
setupFilesAfterEnv: ['./jest.setup.js'],
952+
setupFilesAfterEnv: ['<rootDir>/setup-jest.js'],
948953
};
949954
```
950955

951-
Example `jest.setup.js` file
952-
953-
```js
954-
jest.setTimeout(10000); // in milliseconds
955-
956-
// you can even use the setup/teardown methods
957-
beforeAll(() => {
958-
// your code
959-
});
960-
961-
beforeEach(() => {
962-
// your code
963-
});
964-
965-
afterEach(() => {
966-
// your code
967-
});
968-
969-
afterAll(() => {
970-
// your code
971-
});
972-
```
973-
974956
### `slowTestThreshold` \[number]
975957

976958
Default: `5`

website/versioned_docs/version-28.0/Configuration.md

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,38 +1129,23 @@ Default: `[]`
11291129

11301130
A list of paths to modules that run some code to configure or set up the testing framework before each test file in the suite is executed. Since [`setupFiles`](#setupfiles-array) executes before the test framework is installed in the environment, this script file presents you the opportunity of running some code immediately after the test framework has been installed in the environment but before the test code itself.
11311131

1132-
If you want a path to be [relative to the root directory of your project](#rootdir-string), please include `<rootDir>` inside a path's string, like `"<rootDir>/a-configs-folder"`.
1132+
In other words, `setupFilesAfterEnv` modules are meant for code which is repeating in each test file. Having the test framework installed makes Jest [globals](GlobalAPI.md), [`jest` object](JestObjectAPI.md) and [`expect`](ExpectAPI.md) accessible in the modules. For example, you can add extra matchers from [`jest-extended`](https://github.com/jest-community/jest-extended) library or call [setup and teardown](SetupAndTeardown.md) hooks:
11331133

1134-
For example, Jest ships with several plug-ins to `jasmine` that work by monkey-patching the jasmine API. If you wanted to add even more jasmine plugins to the mix (or if you wanted some custom, project-wide matchers for example), you could do so in these modules.
1134+
```js title="setup-jest.js"
1135+
const matchers = require('jest-extended');
1136+
expect.extend(matchers);
11351137

1136-
Example `setupFilesAfterEnv` array in a jest.config.js:
1138+
afterEach(() => {
1139+
jest.useRealTimers();
1140+
});
1141+
```
11371142

11381143
```js
11391144
module.exports = {
1140-
setupFilesAfterEnv: ['./jest.setup.js'],
1145+
setupFilesAfterEnv: ['<rootDir>/setup-jest.js'],
11411146
};
11421147
```
11431148

1144-
Example `jest.setup.js` file
1145-
1146-
```js
1147-
jest.setTimeout(10000); // in milliseconds
1148-
1149-
// you can even use the setup/teardown methods
1150-
beforeAll(() => {
1151-
// your code
1152-
});
1153-
beforeEach(() => {
1154-
// your code
1155-
});
1156-
afterEach(() => {
1157-
// your code
1158-
});
1159-
afterAll(() => {
1160-
// your code
1161-
});
1162-
```
1163-
11641149
### `slowTestThreshold` \[number]
11651150

11661151
Default: `5`

website/versioned_docs/version-28.1/Configuration.md

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,38 +1129,23 @@ Default: `[]`
11291129

11301130
A list of paths to modules that run some code to configure or set up the testing framework before each test file in the suite is executed. Since [`setupFiles`](#setupfiles-array) executes before the test framework is installed in the environment, this script file presents you the opportunity of running some code immediately after the test framework has been installed in the environment but before the test code itself.
11311131

1132-
If you want a path to be [relative to the root directory of your project](#rootdir-string), please include `<rootDir>` inside a path's string, like `"<rootDir>/a-configs-folder"`.
1132+
In other words, `setupFilesAfterEnv` modules are meant for code which is repeating in each test file. Having the test framework installed makes Jest [globals](GlobalAPI.md), [`jest` object](JestObjectAPI.md) and [`expect`](ExpectAPI.md) accessible in the modules. For example, you can add extra matchers from [`jest-extended`](https://github.com/jest-community/jest-extended) library or call [setup and teardown](SetupAndTeardown.md) hooks:
11331133

1134-
For example, Jest ships with several plug-ins to `jasmine` that work by monkey-patching the jasmine API. If you wanted to add even more jasmine plugins to the mix (or if you wanted some custom, project-wide matchers for example), you could do so in these modules.
1134+
```js title="setup-jest.js"
1135+
const matchers = require('jest-extended');
1136+
expect.extend(matchers);
11351137

1136-
Example `setupFilesAfterEnv` array in a jest.config.js:
1138+
afterEach(() => {
1139+
jest.useRealTimers();
1140+
});
1141+
```
11371142

11381143
```js
11391144
module.exports = {
1140-
setupFilesAfterEnv: ['./jest.setup.js'],
1145+
setupFilesAfterEnv: ['<rootDir>/setup-jest.js'],
11411146
};
11421147
```
11431148

1144-
Example `jest.setup.js` file
1145-
1146-
```js
1147-
jest.setTimeout(10000); // in milliseconds
1148-
1149-
// you can even use the setup/teardown methods
1150-
beforeAll(() => {
1151-
// your code
1152-
});
1153-
beforeEach(() => {
1154-
// your code
1155-
});
1156-
afterEach(() => {
1157-
// your code
1158-
});
1159-
afterAll(() => {
1160-
// your code
1161-
});
1162-
```
1163-
11641149
### `slowTestThreshold` \[number]
11651150

11661151
Default: `5`

0 commit comments

Comments
 (0)