|
| 1 | +--- |
| 2 | +title: "expect" |
| 3 | +excerpt: "Functional testing and specifying robust expectations with k6" |
| 4 | +--- |
| 5 | + |
| 6 | +The `expect` module is a JavaScript library that simplifies specifying expecatation about the responses from the target system. The design of the `expect` library was inspired by ava, Jest and Jasmine. If you already know one of these frameworks, using this library should be very simple. |
| 7 | + |
| 8 | +This library is especially useful for: |
| 9 | + - Functional testing, where many asserts are needed |
| 10 | + - Stress testing, where the System Under Test is failing and the test code needs to stay robust. |
| 11 | + - Load testing, where the failures of the System Under Test need to be robustly collected for analysis |
| 12 | + |
| 13 | + |
| 14 | +<Blockquote mod='warning'> |
| 15 | + |
| 16 | +#### This library is rapidly evolving. |
| 17 | + |
| 18 | +This library is stable enough to be useful, but pay attention to the new versions released in jslib.k6.io. |
| 19 | + |
| 20 | +This documentation is for the last version only. If you discover that some of the code below does not work, it most likely means that you are using an older version. |
| 21 | + |
| 22 | +</Blockquote> |
| 23 | + |
| 24 | +## Installation |
| 25 | +There's nothing to install. This library is hosted on [jslib](https://jslib.k6.io/) and can be imported in the k6 script directly. |
| 26 | + |
| 27 | +<CodeGroup labels={[]}> |
| 28 | + |
| 29 | +```javascript |
| 30 | +import { describe } from 'https://jslib.k6.io/expect/0.0.4/index.js'; |
| 31 | +``` |
| 32 | + |
| 33 | +</CodeGroup> |
| 34 | + |
| 35 | +Alternatively, you can use a copy of this file stored locally. |
| 36 | + |
| 37 | +## Simple example |
| 38 | + |
| 39 | +Let's get started by writing a test for a hypothetical HTTP API that should return a JSON array of objects. |
| 40 | + |
| 41 | +First, create a `mytest.js` k6 script file. |
| 42 | + |
| 43 | + |
| 44 | +<CodeGroup labels={[]}> |
| 45 | + |
| 46 | +```javascript |
| 47 | +import { describe } from 'https://jslib.k6.io/expect/0.0.4/index.js'; |
| 48 | +import http from 'k6/http'; |
| 49 | + |
| 50 | +export default function testSuite() { |
| 51 | + describe('Basic API test', (t) => { |
| 52 | + let response = http.get("https://test-api.k6.io/public/crocodiles") |
| 53 | + |
| 54 | + t.expect(response.status).as("API status code").toEqual(200); |
| 55 | + }) |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +</CodeGroup> |
| 60 | + |
| 61 | +If you are familiar with k6, this is similar to using the built-in `group` and `check` functionality but with different names. |
| 62 | + |
| 63 | +When you run this test with `k6 run mytest.js` the result should look similar to this: |
| 64 | + |
| 65 | +``` |
| 66 | +█ Basic API test |
| 67 | + ✓ response status is 200. |
| 68 | +``` |
| 69 | + |
| 70 | +This basic example is not very exciting because the same result can be achieved with `group` and `check`, so let's move on to more interesting examples. |
| 71 | + |
| 72 | +### Chain of checks |
| 73 | + |
| 74 | +When writing integration tests and performance test, it's often necessary to execute conditional checks. For example, you may want to inspect the JSON body only when the http response is 200. If it's 500, the body is not relevant and should not be inspected. |
| 75 | + |
| 76 | +It's possible to chain checks using the `.and()` function, as shown below. |
| 77 | + |
| 78 | +<CodeGroup labels={[]}> |
| 79 | + |
| 80 | +```javascript |
| 81 | +import { describe } from 'https://jslib.k6.io/expect/0.0.4/index.js'; |
| 82 | +import http from 'k6/http'; |
| 83 | + |
| 84 | +export default function testSuite() { |
| 85 | + |
| 86 | + describe('Fetch a list of public crocodiles', (t) => { |
| 87 | + let response = http.get("https://test-api.k6.io/public/crocodiles") |
| 88 | + |
| 89 | + t.expect(response.status).as("response status").toEqual(200) |
| 90 | + .and(response).toHaveValidJson() |
| 91 | + .and(response.json().length).as("number of crocs").toBeGreaterThan(5); |
| 92 | + }) |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +</CodeGroup> |
| 97 | + |
| 98 | +When you run this test with `k6 run mytest.js`, the result should look similar to this: |
| 99 | + |
| 100 | +The above script should result in the following being printed after execution: |
| 101 | + |
| 102 | +``` |
| 103 | +█ Fetch a list of public crocodiles |
| 104 | + ✓ response status is 200. |
| 105 | + ✓ https://test-api.k6.io/public/crocodiles has valid json response |
| 106 | + ✓ number of crocs is greater than 5 |
| 107 | +``` |
| 108 | + |
| 109 | +More advanced examples can be found in the [examples section](/examples/functional-testing) |
| 110 | + |
| 111 | + |
| 112 | +| Function | Description | |
| 113 | +| -------- | ----------- | |
| 114 | +| [describe(name, function)](/javascript-api/jslib/expect/describe-name-function) | Entry point for creating tests. | |
| 115 | +| [expect(value)](/javascript-api/jslib/expect/expect-value) | expect(value) sets the value to be used in comparison by the next function in the chain | |
| 116 | +| [and(value)](/javascript-api/jslib/expect/and-value) | and(value) is similar to expect(value), but can be used in a chain. | |
| 117 | +| [as(alias)](/javascript-api/jslib/expect/as-string) | as(alias) sets a textual representation of the value passed to `expect` or `and`. | |
| 118 | +| [toEqual(value)](/javascript-api/jslib/expect/toequal-expectedvalue) | The `.toEqual(expectedValue)` is similar to `===` | |
| 119 | +| [toBeGreaterThan(expectedValue)](/javascript-api/jslib/expect/tobegreaterthan-expectedvalue) | Use to verify that `received` > `expected` | |
| 120 | +| [toBeGreaterThanOrEqual(expectedValue)](/javascript-api/jslib/expect/tobegreaterthanorequal-expectedvalue) | Use to verify that `received` >= `expected` | |
| 121 | +| [toBeLessThan(expectedValue)](/javascript-api/jslib/expect/tobelessthan-expectedvalue) | Use to verify that `received` < `expected` | |
| 122 | +| [toBeLessThanOrEqual(expectedValue)](/javascript-api/jslib/expect/tobelessthanorequal-expectedvalue) | Use to verify that `received` <= `expected` | |
| 123 | +| [toBeBetween(from, to)](/javascript-api/jslib/expect/tobebetween-from-to) | Use to verify that expected value is within range. | |
| 124 | +| [toBeTruthy()](/javascript-api/jslib/expect/tobetruthy) | Use `.toBeTruthy` when you don't care what a value is and you want to ensure a value is true in a boolean context. | |
| 125 | +| [toHaveValidJson()](/javascript-api/jslib/expect/tohavevalidjson) | Use to verify that the http response has a valid JSON body. | |
| 126 | + |
| 127 | + |
0 commit comments