Skip to content

k6 library that simplifies writing tests in a functional way by providing a simple and readable syntax for expectations

License

Notifications You must be signed in to change notification settings

simbadltd/k6-expect

Repository files navigation

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 Function Assertion Supports negation Description
that() nil Check value for null or undefined
null Check value for null
toEqual Check value for equality
bool() toBeTruthy Check value for truth
toBeFalsy Check value for falsity
collection() toBeEmpty Check array for emptiness
length Check array for length
toContain Check array for occurence of an item
num() zero Check value for zero
between Check value for a hit in the interval (inclusive)
greaterThan Check that value is greater
greaterThanOrEqual Check that value is greater or equal
lessThan Check that value is less
lessThanOrEqual Check that value is less or equal
str() toBeEmpty Check value for emptiness
regex Check that value matches the pattern
toContain Check value for occurence of a string
response() validJson Check that response contains valid json
success Check that response has successful status (200-299)
status Check that response has status specified
ok Check response for 200 OK
accepted Check response for 202 ACCEPTED
noContent Check response for 204 NO CONTENT
badRequest Check response for 400 BAD REQUEST
unauthorized Check response for 401 UNAUTHORIZED
forbidden Check response for 403 FORBIDDEN
notFound Check response for 404 NOT FOUND
length Check response body length

LICENSE

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

About

k6 library that simplifies writing tests in a functional way by providing a simple and readable syntax for expectations

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published