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

@axel669/aegis

v0.2.7

Published

Test runner for NodeJS and browsers

Downloads

31

Readme

Aegis

Test runner for NodeJS (and browsers soon)

Installation

yarn add @axel669/aegis

API

Test Files

//  Import whatever resources needed, the glob automatically ignores
//  node_modules folders.
import fetch from "node-fetch"

import source from "../source.js"

//  Export a "test" function from each file to be run, optionally async.
//  First argument is an object that contains an Assert and Section function.
//  Second argument is an object that is shared between every test function and
//  and can be modified by the hook functions described later.
export async function test({Assert, Section}, shared) {
    const test = Math.random()
    const obj = { test }
    const list = [1, 2, 3, 4]

    //  Mark sections for the report to group results in.
    Section("modules pls work")

    //  Assertions can be chained, see next section for details.
    Assert(obj)
        `test`.gt(0)
        `test`.lt(1)
    Assert(source)
        .neq(null)

    //  A file can have as many section as it wants, but sections cannot
    //  be nested.
    Section `cool stuff`

    Assert(list)
        .includes(3)
        .contains(3)
        ("length").eq(10)

    Section("postman echo")
    const res = await fetch("https://postman-echo.com/get")
    const result = await res.json()

    Assert(result)
        .has("args")
        ("args")(Object.keys)("length").eq(0)
}

Assert

The assert function takes a single argument with the value to use for the checks that follow. It returns an object that can either be called as a function or have comparison functions called.

//  value to use for ops
Assert(100)
    //  check if the value is equal to something else
    .eq(100)

Assert({ a: 10, b: 12.5 })
    //  calling the result as a function will get the named value
    //  comparison functions can be called on the resulting value, and the
    //  chain will go back to the original value for the next calls
    ("a").eq(10)
    //  the chainable function can also be used with tagged template literals
    //  as a shorthand for property access
    `b`.near({ target: 12.5, delta: 0.01 })
    .has("a")

Assert({ first: 0, second: 1, third: 2 })
    //  passing a function instead of a string will call the function and pass
    //  the value as the only argument, and use the result for the next part
    //  of the chain
    (Object.keys)`length`.eq(3)
    (Object.values).includes(0)

Built-in Assertions

  • eq
  • neq
  • lt
  • gt
  • lte
  • gte
  • near
  • isnan
  • isfinite
  • includes
  • has

Custom Assertions

Custom assertions can be defined in the setup hook, by adding keys to the assertions argument that is passed to the hook.

Assertions take the form of (value, target) => bool, where value is the current value of the Assert() and target is the argument passed to the assertion function in the test (Assert(value).custom(target)).

export const setup = (shared, assertions) => {
    assertions.sqof = (value, target) => value === (target ** 2)
    //  maybe this one will become standard, why isn't it?
    assertions.between = (value, options) => (
        value >= options.min
        && value <= options.max
    )
}

Section

The Section function marks a section in a file that will be used when outputting the results of assertions. Can either be called as a normal function with a string argument for the section name, or as a tagged template function because it looks pretty cool.

package.json Usage

For running files that use es6 moduler syntax or commonjs only works in node 12+ aegis <glob pattern> [hooks file]

For running files that don't use e6 module syntax works in node <12 aegis-cjs <glob pattern> [hooks file]

{
    ...,
    "scripts": {
        "test-modules-only": "aegis test/**/*.test.mjs",
        "test-cjs-only": "aegis-cjs test/**/*.test.js",
        "test-all": "aegis \"test/**/*.test.{js,mjs}\" test/hooks.js"
    },
    ...
}

Hooks

//  setup is run before all tests begin and is given an object that is shared
//  between all test runs, and sent to the teardown hook at the end.
exports.setup = function(shared) {
    console.log("Running tests")
    console.log("=".repeat(60))
}

//  beforeFile/afterFile are called before and after a file's test has been
//  run, after the file's test has been loaded.
exports.beforeFile = function(filename) {
    console.log("running", filename)
}

exports.afterFile = function(filename) {
}

//  teardown is run after all tests have completed, but before the results
//  have been reported. Should be used to cleanup resources that were shared
//  from setup.
exports.teardown = function(shared) {
    console.log("=".repeat(60))
}