Skip to content

Commit 3b49259

Browse files
authored
Merge pull request #2010 from grafana/assertions
Document assertions & testing library
2 parents 20c726b + 4787cf4 commit 3b49259

Some content is hidden

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

50 files changed

+3399
-20
lines changed

docs/sources/k6/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ cascade:
2222
JSLIB_AWS_VERSION: 0.14.0
2323
JSLIB_PYROSCOPE_VERSION: 1.0.2
2424
JSLIB_TEMPO_VERSION: 1.0.1
25+
JSLIB_TESTING_VERSION: 0.5.0
2526
versioned: true
2627
versioned_next: true
2728
---
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
title: 'testing'
3+
description: 'The k6 testing library provides test assertion capabilities for both protocol and browser testing.'
4+
weight: 00
5+
---
6+
7+
# testing
8+
9+
The k6 testing library provides assertion capabilities for both protocol and browser testing, and draws inspiration from Playwright's test API design. The entire library is centered around the [`expect()`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/expect) function, which can be configured for convenience.
10+
11+
{{< admonition type="note" >}}
12+
The k6 testing library source code is available on [GitHub](https://github.com/grafana/k6-jslib-testing).
13+
{{< /admonition >}}
14+
15+
## Features
16+
17+
- **Playwright-inspired assertions**: API designed with patterns inspired by Playwright's testing approach
18+
- **[Protocol and browser testing](#demo)**: Works with both HTTP/API testing and browser automation
19+
- **[Auto-retrying assertions](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/expect#retrying-assertions)**: Automatically retry assertions until they pass or timeout
20+
- **[Soft assertions](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/expect#soft-assertions)**: Continue test execution even after assertion failures
21+
- **[Configurable timeouts](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/configure)**: Customizable timeout and polling intervals
22+
23+
## Usage
24+
25+
To use the testing library in your k6 script, import it in your tests directly from the jslib repository:
26+
27+
<!-- md-k6:skip -->
28+
29+
```javascript
30+
import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js';
31+
```
32+
33+
## Demo
34+
35+
### Protocol Testing
36+
37+
<!-- md-k6:skip -->
38+
39+
```javascript
40+
import { check } from 'k6';
41+
import http from 'k6/http';
42+
import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js';
43+
44+
export default function () {
45+
const response = http.get('https://test-api.k6.io/public/crocodiles/1/');
46+
47+
// Traditional k6 check
48+
check(response, {
49+
'status is 200': (r) => r.status === 200,
50+
});
51+
52+
// Using expect assertions
53+
expect(response.status).toBe(200);
54+
expect(response.json()).toHaveProperty('name');
55+
}
56+
```
57+
58+
### Browser Testing
59+
60+
<!-- md-k6:skip -->
61+
62+
```javascript
63+
import { browser } from 'k6/experimental/browser';
64+
import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js';
65+
66+
export const options = {
67+
scenarios: {
68+
ui: {
69+
executor: 'shared-iterations',
70+
options: {
71+
browser: {
72+
type: 'chromium',
73+
},
74+
},
75+
},
76+
}
77+
};
78+
79+
export default async function () {
80+
const page = browser.newPage();
81+
82+
await page.goto('https://test.k6.io');
83+
84+
// Auto-retrying assertions
85+
await expect(page.locator('h1')).toBeVisible();
86+
await expect(page.locator('h1')).toHaveText('Welcome to the k6 test site');
87+
}
88+
```
89+
90+
## Configuration
91+
92+
Create configured `expect` instances for custom behavior:
93+
94+
<!-- md-k6:skip -->
95+
96+
```javascript
97+
import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js';
98+
99+
// Create configured expect instance
100+
const myExpect = expect.configure({
101+
timeout: 10000, // Default timeout for retrying assertions
102+
interval: 200, // Polling interval for retrying assertions
103+
colorize: true, // Enable colored output
104+
display: 'pretty', // Output format
105+
softMode: 'fail' // Soft assertion behavior
106+
});
107+
```
108+
109+
## Assertion types
110+
111+
The testing library provides two types of assertions:
112+
113+
### [Non-Retrying Assertions](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/testing/expect/non-retrying-assertions/)
114+
115+
Synchronous assertions that evaluate immediately. These are ideal for testing static values, API responses, and scenarios where the expected condition should be true at the moment of evaluation.
116+
117+
### [Retrying Assertions]((https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/testing/expect/retrying-assertions/)
118+
119+
Asynchronous assertions that automatically retry until conditions become true or timeout. These are suitable for browser testing, dynamic content, and scenarios where conditions may change over time.
120+
121+
## API Reference
122+
123+
| Function | Description |
124+
| --- | --- |
125+
| [expect()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/testing/expect) | Main assertion function |
126+
| [expect.configure()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/testing/configure) | Create configured expect instances |
127+
| [Non-Retrying Assertions](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/testing/non-retrying-assertions) | Synchronous assertions for immediate evaluation |
128+
| [Retrying Assertions](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/testing/retrying-assertions) | Asynchronous assertions for dynamic content |

0 commit comments

Comments
 (0)