1.2.3 • Published 2 years ago

k6-expect v1.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

k6-expect

NPM   CI  

k6 library that simplifies writing tests in functional way by providing a simple and jest-like syntax for expectations.

Usage

Get started

export default function () {
    describe("Check", t => {
        const r = http.get("https://jsonplaceholder.typicode.com/users/10");
        
        // specialized and type-safe assertions 
        // ... for k6 http response 
        t.expect(response(r, x => x.ok(), x => x.validJson()))
        // ... for primitives
          .and("Id", num(r.json("id"), x => x.toEqual(10)))
          .and("Name", str(r.json("name"), x => x.not().toBeEmpty()))
          .and("Phone number", str(r.json("phone"), x => x.regex("\\d{3}-\\d{3}-\\d{4}")))
          .and("Geolocation", num(r.json("address.geo.lat"), x => x.lessThan(0)))
          .and("Company", str(r.json("company.name"), x => x.toContain("LLC")));
    });
}

Output:

     █ User check
       ✓ https://jsonplaceholder.typicode.com/users/10 is 200
       ✓ https://jsonplaceholder.typicode.com/users/10 responded with valid json
       ✓ Id is 10
       ✓ Name is not empty
       ✓ Phone number matches '\d{3}-\d{3}-\d{4}' pattern
       ✓ Geolocation is less than 0
       ✓ Company contains 'LLC'

For more information, check this examples.

Pass custom context

export class FooContext implements TestSuiteContext {
  breakOnFirstAssert: boolean;
  
  constructor() {
    this.breakOnFirstAssert = true;
  }

  sanitizeUrl(url: string): string {
    return "";
  }

  customFunction() { /* custom logic */ }
}

let fooContext = new FooContext();

export default function () {
    describe("Check", t => {
        // Test logic
        // ...
        // Access to custom logic
        t.context!.customFunction();
    }, fooContext);
}

Typescript integration

Based on k6-template-typescript.

package.json:

{
  "devDependencies": {
    "k6-expect": "X.X.X"
  }
}

webpack.config.js:

// ...
module.exports = {
  // ...
    externals: [
        function ({context, request}, c) {
            if (request.startsWith('k6') || request.startsWith('https://')) {
                return request === 'k6-expect' ? c() : c(null, 'commonjs ' + request);
            }
            return c();
        },
    ],
  // ...
}

Assertions table

Access FunctionAssertionSupports negationDescription
that()nilCheck value for null or undefined
nullCheck value for null
toEqualCheck value for equality
bool()toBeTruthyCheck value for truth
toBeFalsyCheck value for falsity
collection()toBeEmptyCheck array for emptiness
lengthCheck array for length
toContainCheck array for occurence of an item
num()zeroCheck value for zero
betweenCheck value for a hit in the interval (inclusive)
greaterThanCheck that value is greater
greaterThanOrEqualCheck that value is greater or equal
lessThanCheck that value is less
lessThanOrEqualCheck that value is less or equal
str()toBeEmptyCheck value for emptiness
regexCheck that value matches the pattern
toContainCheck value for occurence of a string
response()validJsonCheck that response contains valid json
successCheck that response has successful status (200-299)
statusCheck that response has status specified
okCheck response for 200 OK
acceptedCheck response for 202 ACCEPTED
noContentCheck response for 204 NO CONTENT
badRequestCheck response for 400 BAD REQUEST
unauthorizedCheck response for 401 UNAUTHORIZED
forbiddenCheck response for 403 FORBIDDEN
notFoundCheck response for 404 NOT FOUND
lengthCheck response body length

LICENSE

Distributed under the MIT License. See LICENSE for more information.

1.2.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.15

3 years ago

0.0.14

3 years ago

0.0.13

3 years ago

0.0.12

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago