npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

k6-expect

v1.2.3

Published

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

Downloads

40

Readme

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.