1
1
---
2
2
title : ' expect.configure()'
3
- head_title : ' expect.configure(options)'
4
3
description : ' Configure global assertion behavior for the k6 testing library'
5
4
weight : 20
6
5
---
7
6
8
7
# expect.configure()
9
8
10
- The ` expect.configure() ` method creates a new configured ` expect ` instance with custom behavior for the k6 testing library, including timeouts, display options, and soft assertion behavior.
9
+ The ` expect.configure() ` method creates a new configured ` expect ` instance with custom behavior for the k6 testing library, including timeouts, display options, and soft assertion behavior.
11
10
12
- This new instance can be used in place of the default expect function, and will apply the specified configuration to all assertions made with it.
11
+ This new instance can be used in place of the default expect function, and will apply the specified configuration to all assertions made with it.
13
12
14
13
The imported ` expect ` instance remains unchanged and continues to use the default configuration, allowing different assertion configurations to co-exist within a test.
15
14
16
15
## Syntax
17
16
17
+ <!-- eslint-skip -->
18
+
18
19
``` javascript
19
- const configuredExpect = expect .configure (options)
20
+ const configuredExpect = expect .configure (options);
20
21
```
21
22
22
23
## Parameters
23
24
24
- | Parameter | Type | Description |
25
- | --- | --- | --- |
26
- | options | object | Configuration options object |
25
+ | Parameter | Type | Description |
26
+ | --------- | ------ | ------------------------- --- |
27
+ | options | object | Configuration options object |
27
28
28
29
### Options
29
30
30
- | Property | Type | Default | Description |
31
- | --- | --- | --- | --- |
32
- | timeout | number | ` 5000 ` | Default timeout in milliseconds for retrying assertions |
33
- | interval | number | ` 100 ` | Polling interval in milliseconds for retrying assertions |
34
- | colorize | boolean | ` true ` | Enable colorized output in assertion messages |
35
- | display | string | ` "pretty" ` | Output format for assertion messages (` "pretty" ` or ` "inline" ` ) |
36
- | softMode | string | ` "fail" ` | Soft assertion behavior (` "fail" ` or ` "throw" ` ) |
31
+ | Property | Type | Default | Description |
32
+ | -------- | ------- | ---------- | ------------------------------------------------------------ --- |
33
+ | timeout | number | ` 5000 ` | Default timeout in milliseconds for retrying assertions |
34
+ | interval | number | ` 100 ` | Polling interval in milliseconds for retrying assertions |
35
+ | colorize | boolean | ` true ` | Enable colorized output in assertion messages |
36
+ | display | string | ` "pretty" ` | Output format for assertion messages (` "pretty" ` or ` "inline" ` ) |
37
+ | softMode | string | ` "fail" ` | Soft assertion behavior (` "fail" ` or ` "throw" ` ) |
37
38
38
39
## Returns
39
40
40
- | Type | Description |
41
- | --- | --- |
41
+ | Type | Description |
42
+ | ------ | --------------------------------------------------- --- |
42
43
| Expect | A new expect instance with the specified configuration |
43
44
44
45
### Timeout configuration
@@ -50,8 +51,8 @@ import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_
50
51
51
52
// Create a configured expect instance with longer timeout for slow-loading elements
52
53
const slowExpect = expect .configure ({
53
- timeout: 10000 , // 10 seconds
54
- interval: 500 , // Check every 500ms
54
+ timeout: 10000 , // 10 seconds
55
+ interval: 500 , // Check every 500ms
55
56
});
56
57
```
57
58
@@ -77,7 +78,7 @@ The `softMode` option controls whether failed assertions stop test execution:
77
78
import { expect } from ' https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js' ;
78
79
79
80
// The default behavior of soft assertions is mark the test as failed, the `softMode` option
80
- // allows to configure soft assertions to throw an exception and fail the current iteration instead.
81
+ // allows to configure soft assertions to throw an exception and fail the current iteration instead.
81
82
const softExpect = expect .configure ({
82
83
softMode: ' throw' ,
83
84
});
@@ -87,13 +88,13 @@ const softExpect = expect.configure({
87
88
88
89
Configuration options can also be set using environment variables:
89
90
90
- | Environment Variable | Option | Description |
91
- | --- | --- | --- |
92
- | ` K6_TESTING_TIMEOUT ` | ` timeout ` | Default timeout in milliseconds |
93
- | ` K6_TESTING_INTERVAL ` | ` interval ` | Polling interval in milliseconds |
94
- | ` K6_TESTING_COLORIZE ` | ` colorize ` | Enable colored output (` true ` /` false ` ) |
95
- | ` K6_TESTING_DISPLAY ` | ` display ` | Output format (` pretty ` /` inline ` ) |
96
- | ` K6_TESTING_SOFT_MODE ` | ` softMode ` | Soft assertion mode (` fail ` /` throw ` ) |
91
+ | Environment Variable | Option | Description |
92
+ | ---------------------- | ---------- | ----------------------------------- --- |
93
+ | ` K6_TESTING_TIMEOUT ` | ` timeout ` | Default timeout in milliseconds |
94
+ | ` K6_TESTING_INTERVAL ` | ` interval ` | Polling interval in milliseconds |
95
+ | ` K6_TESTING_COLORIZE ` | ` colorize ` | Enable colored output (` true ` /` false ` ) |
96
+ | ` K6_TESTING_DISPLAY ` | ` display ` | Output format (` pretty ` /` inline ` ) |
97
+ | ` K6_TESTING_SOFT_MODE ` | ` softMode ` | Soft assertion mode (` fail ` /` throw ` ) |
97
98
98
99
``` bash
99
100
# Set environment variables
@@ -106,6 +107,8 @@ k6 run test.js
106
107
107
108
### Basic Configuration
108
109
110
+ <!-- eslint-skip -->
111
+
109
112
``` javascript
110
113
import { expect } from ' https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js' ;
111
114
@@ -114,13 +117,13 @@ const myExpect = expect.configure({
114
117
timeout: 8000 ,
115
118
interval: 200 ,
116
119
colorize: true ,
117
- display: ' pretty'
120
+ display: ' pretty' ,
118
121
});
119
122
120
123
export default function () {
121
124
// Use the configured instance
122
125
myExpect (response .status ).toBe (200 );
123
-
126
+
124
127
// Original expect instance still works with defaults
125
128
expect (response .status ).toBe (200 );
126
129
}
@@ -134,14 +137,14 @@ import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_
134
137
135
138
// Create expect instance configured for browser testing with longer timeouts
136
139
const browserExpect = expect .configure ({
137
- timeout: 15000 , // Longer timeout for browser operations
138
- interval: 500 , // Less frequent polling
140
+ timeout: 15000 , // Longer timeout for browser operations
141
+ interval: 500 , // Less frequent polling
139
142
});
140
143
141
144
export default async function () {
142
145
const page = browser .newPage ();
143
146
await page .goto (' https://test.k6.io' );
144
-
147
+
145
148
// Will wait up to 15 seconds for element to be visible
146
149
await browserExpect (page .locator (' h1' )).toBeVisible ();
147
150
}
@@ -154,9 +157,9 @@ import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_
154
157
155
158
// Create expect instance configured for CI environment
156
159
const ciExpect = expect .configure ({
157
- colorize: false , // Disable colors in CI logs
160
+ colorize: false , // Disable colors in CI logs
158
161
display: ' inline' , // inline output for CI
159
- timeout: 30000 , // Longer timeout for CI environment
162
+ timeout: 30000 , // Longer timeout for CI environment
160
163
});
161
164
```
162
165
@@ -178,6 +181,8 @@ const envExpect = expect.configure({
178
181
179
182
### Multiple Configured Instances
180
183
184
+ <!-- eslint-skip -->
185
+
181
186
``` javascript
182
187
import { expect } from ' https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js' ;
183
188
@@ -187,16 +192,16 @@ export default function () {
187
192
timeout: 1000 ,
188
193
interval: 50 ,
189
194
});
190
-
195
+
191
196
const slowExpect = expect .configure ({
192
197
timeout: 30000 ,
193
198
softMode: ' continue' ,
194
199
});
195
-
200
+
196
201
// Use appropriate instance for each test
197
202
fastExpect (quickOperation ()).toBe (true );
198
203
slowExpect (slowOperation ()).toBe (true );
199
-
204
+
200
205
// Original expect instance still available
201
206
expect (normalOperation ()).toBe (true );
202
207
}
@@ -215,12 +220,12 @@ const softExpect = expect.configure({
215
220
216
221
export default function () {
217
222
const response = http .get (' https://test-api.k6.io/public/crocodiles/1/' );
218
-
223
+
219
224
// These assertions will not stop test execution on failure
220
225
softExpect (response .status ).toBe (200 );
221
226
softExpect (response .json ()).toHaveProperty (' name' );
222
227
softExpect (response .json ()).toHaveProperty (' age' );
223
-
228
+
224
229
// Test continues even if assertions fail
225
230
console .log (' Test completed' );
226
231
}
0 commit comments