|
| 1 | +--- |
| 2 | +title: 'expect.configure()' |
| 3 | +head_title: 'expect.configure(options)' |
| 4 | +description: 'Configure global assertion behavior for the k6 testing library' |
| 5 | +weight: 20 |
| 6 | +--- |
| 7 | + |
| 8 | +# expect.configure() |
| 9 | + |
| 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. The original expect instance remains unchanged. |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +```javascript |
| 15 | +const configuredExpect = expect.configure(options) |
| 16 | +``` |
| 17 | + |
| 18 | +## Parameters |
| 19 | + |
| 20 | +| Parameter | Type | Description | |
| 21 | +| --- | --- | --- | |
| 22 | +| options | object | Configuration options object | |
| 23 | + |
| 24 | +### Options |
| 25 | + |
| 26 | +| Property | Type | Default | Description | |
| 27 | +| --- | --- | --- | --- | |
| 28 | +| timeout | number | `5000` | Default timeout in milliseconds for retrying assertions | |
| 29 | +| interval | number | `100` | Polling interval in milliseconds for retrying assertions | |
| 30 | +| colorize | boolean | `true` | Enable colored output in assertion messages | |
| 31 | +| display | string | `"pretty"` | Output format for assertion messages (`"pretty"` or `"inline"`) | |
| 32 | +| softMode | string | `"fail"` | Soft assertion behavior (`"fail"` or `"throw"`) | |
| 33 | + |
| 34 | +## Returns |
| 35 | + |
| 36 | +| Type | Description | |
| 37 | +| --- | --- | |
| 38 | +| Expect | A new expect instance with the specified configuration | |
| 39 | + |
| 40 | +## Description |
| 41 | + |
| 42 | +The `expect.configure()` method creates a new expect instance with custom configuration options. 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. The original expect instance remains unchanged and continues to use the default configuration. |
| 43 | + |
| 44 | +### Timeout Configuration |
| 45 | + |
| 46 | +The `timeout` option controls how long retrying assertions will wait for a condition to become true: |
| 47 | + |
| 48 | +```javascript |
| 49 | +import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_AWS_VERSION" >}}/index.js'; |
| 50 | + |
| 51 | +// Create a configured expect instance with longer timeout for slow-loading elements |
| 52 | +const slowExpect = expect.configure({ |
| 53 | + timeout: 10000, // 10 seconds |
| 54 | + interval: 500, // Check every 500ms |
| 55 | +}); |
| 56 | +``` |
| 57 | + |
| 58 | +### Display Options |
| 59 | + |
| 60 | +The `display` and `colorize` options control how assertion failures are reported: |
| 61 | + |
| 62 | +```javascript |
| 63 | +import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_AWS_VERSION" >}}/index.js'; |
| 64 | + |
| 65 | +// Create expect instance with inline output and no colors |
| 66 | +const inlineExpect = expect.configure({ |
| 67 | + display: 'inline', |
| 68 | + colorize: false, |
| 69 | +}); |
| 70 | +``` |
| 71 | + |
| 72 | +### Soft Assertion Mode |
| 73 | + |
| 74 | +The `softMode` option controls whether failed assertions stop test execution: |
| 75 | + |
| 76 | +```javascript |
| 77 | +import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_AWS_VERSION" >}}/index.js'; |
| 78 | + |
| 79 | +// 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 | +const softExpect = expect.configure({ |
| 82 | + softMode: 'throw', |
| 83 | +}); |
| 84 | +``` |
| 85 | + |
| 86 | +## Environment Variables |
| 87 | + |
| 88 | +Configuration options can also be set using environment variables: |
| 89 | + |
| 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`) | |
| 97 | + |
| 98 | +```bash |
| 99 | +# Set environment variables |
| 100 | +export K6_TESTING_TIMEOUT=10000 |
| 101 | +export K6_TESTING_SOFT_MODE=throw |
| 102 | +k6 run test.js |
| 103 | +``` |
| 104 | + |
| 105 | +## Examples |
| 106 | + |
| 107 | +### Basic Configuration |
| 108 | + |
| 109 | +```javascript |
| 110 | +import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_AWS_VERSION" >}}/index.js'; |
| 111 | + |
| 112 | +// Create a configured expect instance |
| 113 | +const myExpect = expect.configure({ |
| 114 | + timeout: 8000, |
| 115 | + interval: 200, |
| 116 | + colorize: true, |
| 117 | + display: 'pretty' |
| 118 | +}); |
| 119 | + |
| 120 | +export default function () { |
| 121 | + // Use the configured instance |
| 122 | + myExpect(response.status).toBe(200); |
| 123 | + |
| 124 | + // Original expect instance still works with defaults |
| 125 | + expect(response.status).toBe(200); |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +### Browser Testing Configuration |
| 130 | + |
| 131 | +```javascript |
| 132 | +import { browser } from 'k6/experimental/browser'; |
| 133 | +import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_AWS_VERSION" >}}/index.js'; |
| 134 | + |
| 135 | +// Create expect instance configured for browser testing with longer timeouts |
| 136 | +const browserExpect = expect.configure({ |
| 137 | + timeout: 15000, // Longer timeout for browser operations |
| 138 | + interval: 500, // Less frequent polling |
| 139 | +}); |
| 140 | + |
| 141 | +export default async function () { |
| 142 | + const page = browser.newPage(); |
| 143 | + await page.goto('https://test.k6.io'); |
| 144 | + |
| 145 | + // Will wait up to 15 seconds for element to be visible |
| 146 | + await browserExpect(page.locator('h1')).toBeVisible(); |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +### CI/CD Configuration |
| 151 | + |
| 152 | +```javascript |
| 153 | +import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_AWS_VERSION" >}}/index.js'; |
| 154 | + |
| 155 | +// Create expect instance configured for CI environment |
| 156 | +const ciExpect = expect.configure({ |
| 157 | + colorize: false, // Disable colors in CI logs |
| 158 | + display: 'inline', // inline output for CI |
| 159 | + timeout: 30000, // Longer timeout for CI environment |
| 160 | +}); |
| 161 | +``` |
| 162 | + |
| 163 | +### Development vs Production |
| 164 | + |
| 165 | +```javascript |
| 166 | +import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_AWS_VERSION" >}}/index.js'; |
| 167 | + |
| 168 | +// Different configuration based on environment |
| 169 | +const isDevelopment = __ENV.NODE_ENV === 'development'; |
| 170 | + |
| 171 | +const envExpect = expect.configure({ |
| 172 | + timeout: isDevelopment ? 5000 : 15000, |
| 173 | + colorize: isDevelopment, |
| 174 | + display: isDevelopment ? 'pretty' : 'inline', |
| 175 | + softMode: isDevelopment ? 'continue' : 'fail', |
| 176 | +}); |
| 177 | +``` |
| 178 | + |
| 179 | +### Multiple Configured Instances |
| 180 | + |
| 181 | +```javascript |
| 182 | +import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_AWS_VERSION" >}}/index.js'; |
| 183 | + |
| 184 | +export default function () { |
| 185 | + // Create specific expect instances for different scenarios |
| 186 | + const fastExpect = expect.configure({ |
| 187 | + timeout: 1000, |
| 188 | + interval: 50, |
| 189 | + }); |
| 190 | + |
| 191 | + const slowExpect = expect.configure({ |
| 192 | + timeout: 30000, |
| 193 | + softMode: 'continue', |
| 194 | + }); |
| 195 | + |
| 196 | + // Use appropriate instance for each test |
| 197 | + fastExpect(quickOperation()).toBe(true); |
| 198 | + slowExpect(slowOperation()).toBe(true); |
| 199 | + |
| 200 | + // Original expect instance still available |
| 201 | + expect(normalOperation()).toBe(true); |
| 202 | +} |
| 203 | +``` |
| 204 | + |
| 205 | +### Soft Assertion Examples |
| 206 | + |
| 207 | +```javascript |
| 208 | +import http from 'k6/http'; |
| 209 | +import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_AWS_VERSION" >}}/index.js'; |
| 210 | + |
| 211 | +// Create expect instance with soft assertions enabled |
| 212 | +const softExpect = expect.configure({ |
| 213 | + softMode: 'continue', |
| 214 | +}); |
| 215 | + |
| 216 | +export default function () { |
| 217 | + const response = http.get('https://test-api.k6.io/public/crocodiles/1/'); |
| 218 | + |
| 219 | + // These assertions will not stop test execution on failure |
| 220 | + softExpect(response.status).toBe(200); |
| 221 | + softExpect(response.json()).toHaveProperty('name'); |
| 222 | + softExpect(response.json()).toHaveProperty('age'); |
| 223 | + |
| 224 | + // Test continues even if assertions fail |
| 225 | + console.log('Test completed'); |
| 226 | +} |
| 227 | +``` |
| 228 | + |
| 229 | +## See Also |
| 230 | + |
| 231 | +- [expect()]({{< relref "./expect" >}}) - Main assertion function |
| 232 | +- [Environment Variables]({{< relref "../../../using-k6/environment-variables" >}}) - k6 environment variable usage |
0 commit comments