Skip to content

Commit f1e772b

Browse files
heitortsergentankur22
authored andcommitted
Fix negation example in assertions.md
1 parent a90f418 commit f1e772b

File tree

1 file changed

+82
-78
lines changed

1 file changed

+82
-78
lines changed

docs/sources/k6/next/using-k6/assertions.md

Lines changed: 82 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,39 @@ weight: 04
88

99
# Assertions
1010

11-
k6 provides test assertions in the form of the [`expect`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/expect) function. Assertions validate that your application behaves as expected during testing.
11+
k6 provides test assertions in the form of the [`expect`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/expect) function. Assertions validate that your application behaves as expected during testing.
1212

1313
Define assertions by passing a value to [`expect()`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/expect) and chaining it with a matcher that defines your expected outcome. The library provides expressive matchers that work with both protocol testing [HTTP/API](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/protocols) and [browser](https://grafana.com/docs/k6/<K6_VERSION>/using-k6-browser) testing scenarios.
1414

15-
The assertions API is inspired by Playwright's assertion syntax, providing a fluent interface that improves test readability and reliability.
15+
The assertions API is inspired by Playwright's assertion syntax, providing a fluent interface that improves test readability and reliability.
1616

1717
## Getting started
1818

1919
Assertions are provided by the [k6-testing library](https://jslib.k6.io). Import the library to start using assertions:
2020

2121
```javascript
2222
import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js';
23-
import { browser } from "k6/browser";
24-
import http from "k6/http";
23+
import { browser } from 'k6/browser';
24+
import http from 'k6/http';
2525

2626
export function protocolTest() {
2727
// Get the home page of k6's Quick Pizza app
28-
const response = http.get("https://quickpizza.grafana.com/");
28+
const response = http.get('https://quickpizza.grafana.com/');
2929

3030
// Simple assertions
3131
expect(response.status).toBe(200);
32-
expect(response.error).toEqual("");
32+
expect(response.error).toEqual('');
3333
expect(response.body).toBeDefined();
3434
}
3535

3636
export async function browserTest() {
3737
const page = await browser.newPage();
3838

3939
try {
40-
await page.goto("https://quickpizza.grafana.com/");
40+
await page.goto('https://quickpizza.grafana.com/');
4141

4242
// Assert the "Pizza Please" button is visible
43-
await expect(page.locator("button[name=pizza-please]")).toBeVisible();
43+
await expect(page.locator('button[name=pizza-please]')).toBeVisible();
4444
} finally {
4545
await page.close();
4646
}
@@ -50,21 +50,21 @@ export const options = {
5050
scenarios: {
5151
// Protocol tests
5252
protocol: {
53-
executor: "shared-iterations",
53+
executor: 'shared-iterations',
5454
vus: 1,
5555
iterations: 1,
56-
exec: "protocolTest",
56+
exec: 'protocolTest',
5757
},
5858

5959
// Browser tests
6060
ui: {
61-
executor: "shared-iterations",
61+
executor: 'shared-iterations',
6262
options: {
6363
browser: {
64-
type: "chromium",
64+
type: 'chromium',
6565
},
6666
},
67-
exec: "browserTest",
67+
exec: 'browserTest',
6868
},
6969
},
7070
};
@@ -84,22 +84,22 @@ import http from 'k6/http';
8484

8585
export default function () {
8686
const pizzaRequestPayload = { maxCaloriesPerSlice: 1000, mustBeVegetarian: true };
87-
const pizzaRequestHeader = {
88-
"Content-Type": "application/json",
89-
"Authorization": "Token " + "abcdef0123456789"
90-
}
87+
const pizzaRequestHeader = {
88+
'Content-Type': 'application/json',
89+
'Authorization': 'Token ' + 'abcdef0123456789',
90+
};
9191

9292
const response = http.post(
9393
`https://quickpizza.grafana.com/api/pizza`,
9494
JSON.stringify(pizzaRequestPayload),
9595
{ headers: pizzaRequestHeader }
9696
);
9797
const data = response.json();
98-
98+
9999
// These assertions evaluate immediately
100100
expect(response.status).toEqual(200);
101-
expect(response.headers["Content-Type"]).toBeDefined();
102-
expect(response.headers["Content-Type"]).toEqual("application/json");
101+
expect(response.headers['Content-Type']).toBeDefined();
102+
expect(response.headers['Content-Type']).toEqual('application/json');
103103
expect(data.pizza).toBeDefined();
104104
expect(data.pizza.name).toBeDefined();
105105
expect(data.pizza.name).not.toHaveLength(0);
@@ -114,37 +114,37 @@ export default function () {
114114
import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js';
115115
import { browser } from 'k6/browser';
116116

117-
export default async function() {
117+
export default async function () {
118118
// Open a new browser page
119-
const page = await browser.newPage()
119+
const page = await browser.newPage();
120120

121121
try {
122122
// Navigate to the quickpizza website
123-
await page.goto('https://quickpizza.grafana.com/')
123+
await page.goto('https://quickpizza.grafana.com/');
124124

125125
// Click the 'Pizza please' button
126-
await page.locator('button[name="pizza-please"]').click()
126+
await page.locator('button[name="pizza-please"]').click();
127127

128128
// Take a screenshot of the homepage, and save it to the local filesystem
129129
// so we can inspect it later if needed.
130-
await page.screenshot({ path: 'homepage.png' })
130+
await page.screenshot({ path: 'homepage.png' });
131131

132132
// Check if the pizza recipe is displayed
133-
const textContent = await pizzaRecipeIsDisplayed(page)
134-
expect(textContent).toEqual('Our recommendation:')
133+
const textContent = await pizzaRecipeIsDisplayed(page);
134+
expect(textContent).toEqual('Our recommendation:');
135135
} finally {
136-
await page.close()
136+
await page.close();
137137
}
138138
}
139139

140140
// Browsers are asynchronous, so we need to wait for the content we want to check
141141
// to be visible.
142142
async function pizzaRecipeIsDisplayed(page) {
143-
const label = await page.locator('h2[id="pizza-name"]')
144-
await label.isVisible()
145-
const textContent = (await label.textContent()).trim()
143+
const label = await page.locator('h2[id="pizza-name"]');
144+
await label.isVisible();
145+
const textContent = (await label.textContent()).trim();
146146

147-
return textContent
147+
return textContent;
148148
}
149149
```
150150

@@ -160,43 +160,43 @@ Assertions do not register metrics because they halt execution rather than colle
160160

161161
Use these for immediate evaluation of static values:
162162

163-
| Method | Description |
164-
| --- | --- |
165-
| [toBe()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobe) | Exact equality using Object.is() |
166-
| [toEqual()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/toequal) | Deep equality comparison |
167-
| [toBeTruthy()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobetruthy) | Value is truthy |
168-
| [toBeFalsy()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobefalsy) | Value is falsy |
169-
| [toBeDefined()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobedefined) | Value is not undefined |
170-
| [toBeUndefined()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobeundefined) | Value is undefined |
171-
| [toBeNull()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobenull) | Value is null |
172-
| [toBeGreaterThan()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobegreaterthan) | Numeric greater than |
173-
| [toBeGreaterThanOrEqual()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobegreaterthanorequal) | Numeric greater than or equal |
174-
| [toBeLessThan()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobelessthan) | Numeric less than |
175-
| [toBeLessThanOrEqual()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobelessthanorequal) | Numeric less than or equal |
176-
| [toBeCloseTo()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobecloseto) | Floating point comparison |
177-
| [toContain()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tocontain) | Array/string contains value |
178-
| [toContainEqual()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tocontainequal) | Array contains object with matching content |
179-
| [toHaveLength()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tohavelength) | Array/string has specific length |
180-
| [toHaveProperty()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tohaveproperty) | Object has specific property |
181-
| [toBeInstanceOf()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobeinstanceof) | Value is instance of class |
163+
| Method | Description |
164+
| --------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------- |
165+
| [toBe()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobe) | Exact equality using Object.is() |
166+
| [toEqual()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/toequal) | Deep equality comparison |
167+
| [toBeTruthy()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobetruthy) | Value is truthy |
168+
| [toBeFalsy()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobefalsy) | Value is falsy |
169+
| [toBeDefined()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobedefined) | Value is not undefined |
170+
| [toBeUndefined()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobeundefined) | Value is undefined |
171+
| [toBeNull()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobenull) | Value is null |
172+
| [toBeGreaterThan()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobegreaterthan) | Numeric greater than |
173+
| [toBeGreaterThanOrEqual()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobegreaterthanorequal) | Numeric greater than or equal |
174+
| [toBeLessThan()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobelessthan) | Numeric less than |
175+
| [toBeLessThanOrEqual()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobelessthanorequal) | Numeric less than or equal |
176+
| [toBeCloseTo()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobecloseto) | Floating point comparison |
177+
| [toContain()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tocontain) | Array/string contains value |
178+
| [toContainEqual()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tocontainequal) | Array contains object with matching content |
179+
| [toHaveLength()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tohavelength) | Array/string has specific length |
180+
| [toHaveProperty()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tohaveproperty) | Object has specific property |
181+
| [toBeInstanceOf()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/non-retrying-assertions/tobeinstanceof) | Value is instance of class |
182182

183183
[See all non-retrying assertions →](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/testing/non-retrying-assertions)
184184

185-
### Auto-retrying assertions
185+
### Auto-retrying assertions
186186

187187
Essential for browser testing with dynamic content:
188188

189-
| Method | Description |
190-
| --- | --- |
191-
| [toBeVisible()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tobevisible) | Element is visible on the page |
192-
| [toBeHidden()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tobehidden) | Element is hidden or not visible |
193-
| [toBeEnabled()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tobeenabled) | Element is enabled and interactive |
194-
| [toBeDisabled()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tobedisabled) | Element is disabled |
195-
| [toBeChecked()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tobechecked) | Checkbox or radio button is checked |
196-
| [toBeEditable()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tobeeditable) | Element is editable |
197-
| [toHaveText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tohavetext) | Element has specific text content |
198-
| [toContainText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tocontaintext) | Element contains specific text |
199-
| [toHaveValue()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tohavevalue) | Input element has specific value |
189+
| Method | Description |
190+
| --------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------ |
191+
| [toBeVisible()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tobevisible) | Element is visible on the page |
192+
| [toBeHidden()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tobehidden) | Element is hidden or not visible |
193+
| [toBeEnabled()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tobeenabled) | Element is enabled and interactive |
194+
| [toBeDisabled()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tobedisabled) | Element is disabled |
195+
| [toBeChecked()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tobechecked) | Checkbox or radio button is checked |
196+
| [toBeEditable()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tobeeditable) | Element is editable |
197+
| [toHaveText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tohavetext) | Element has specific text content |
198+
| [toContainText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tocontaintext) | Element contains specific text |
199+
| [toHaveValue()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tohavevalue) | Input element has specific value |
200200
| [toHaveAttribute()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/k6-testing/retrying-assertions/tohaveattribute) | Element has specific attribute value |
201201

202202
[See all retrying assertions →](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/jslib/testing/retrying-assertions)
@@ -208,18 +208,22 @@ Essential for browser testing with dynamic content:
208208
All assertions can be negated using `.not`:
209209

210210
```javascript
211+
import { browser } from 'k6/browser';
211212
import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js';
212213
import http from 'k6/http';
213214

214-
export default function () {
215+
export default async function () {
215216
const response = http.get('https://quickpizza.grafana.com/');
216-
217+
217218
// Negated assertions
218219
expect(response.status).not.toBe(404);
219220
expect(response.body).not.toHaveLength(0);
220221
expect(response.headers).not.toHaveProperty('error');
221-
222+
222223
// Browser negation (with await)
224+
const page = await browser.newPage();
225+
await page.goto('https://quickpizza.grafana.com/');
226+
223227
await expect(page.locator('.error-message')).not.toBeVisible();
224228
}
225229
```
@@ -234,12 +238,12 @@ import http from 'k6/http';
234238

235239
export default function () {
236240
const response = http.get('https://quickpizza.grafana.com/');
237-
241+
238242
// These will all run even if some fail
239243
expect.soft(response.status).toBe(200);
240244
expect.soft(response.headers['Content-Type']).toContain('text/html');
241245
expect.soft(response.body).toHaveLength(response.body.length);
242-
246+
243247
// Test continues and performs additional checks
244248
console.log('Test completed, checking results...');
245249
}
@@ -255,10 +259,10 @@ import http from 'k6/http';
255259

256260
export default function () {
257261
const response = http.get('https://quickpizza.grafana.com/api/pizza', {
258-
headers: { 'Content-Type': 'application/json' }
262+
headers: { 'Content-Type': 'application/json' },
259263
});
260264
const pizza = response.json();
261-
265+
262266
expect(response.status, 'API should return successful response').toBe(200);
263267
expect(pizza.name, 'Pizza should have a valid name').toBeDefined();
264268
expect(pizza.name, 'Pizza name should not be empty').not.toHaveLength(0);
@@ -277,16 +281,16 @@ import http from 'k6/http';
277281

278282
// Configure global settings
279283
const configuredExpect = expect.configure({
280-
timeout: 10000, // 10 seconds for retrying assertions
281-
interval: 500, // Retry check every 500ms (on retriable assertions)
282-
colorize: true, // Enable colored output
284+
timeout: 10000, // 10 seconds for retrying assertions
285+
interval: 500, // Retry check every 500ms (on retriable assertions)
286+
colorize: true, // Enable colored output
283287

284288
// Setting `softMode` to 'throw', will make soft assertions fail the current iteration instead of the whole test.
285-
softMode: 'fail'
289+
softMode: 'fail',
286290
});
287291

288292
export default function () {
289-
const response = http.get("https://quickpizza.grafana.com");
293+
const response = http.get('https://quickpizza.grafana.com');
290294

291295
// All assertions use these settings
292296
configuredExpect(response.status).toBe(200);
@@ -305,21 +309,21 @@ import { browser } from 'k6/browser';
305309
// Fast assertions for API testing
306310
const fastExpect = expect.configure({
307311
timeout: 2000,
308-
interval: 100
312+
interval: 100,
309313
});
310314

311315
// Slow assertions for complex browser interactions
312316
const slowExpect = expect.configure({
313317
timeout: 30000,
314318
interval: 1000,
315-
softMode: 'continue'
319+
softMode: 'continue',
316320
});
317321

318322
export default async function () {
319323
// Use appropriate expectation based on test type
320324
const response = http.get('https://quickpizza.grafana.com/');
321325
fastExpect(response.status).toBe(200);
322-
326+
323327
if (__ENV.BROWSER_TEST) {
324328
const page = await browser.newPage();
325329
await page.goto('https://quickpizza.grafana.com/');

0 commit comments

Comments
 (0)