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

@zorax/plug

v0.0.3

Published

plugin system for zora

Downloads

59

Readme

@zorax/plug

Plugin system for Zora

Usage

import { createHarness } from '@zorax/plug'

const options = { defaultFooMsg = 'should be foo' }

const plugins = [{
  name: 'foo',

  description: 'adds a foo assertion',

  test(t, harness) {
    const { options: defaultFooMsg } = harness

    t.foo = (actual, msg = defaultFooMsg) =>
      t.eq(actual, 'foo', msg)
  }
}]

const harness = createHarness(options, plugins)

harness.test('foo', t => {
  t.foo('foo')
})

harness.report()

createHarnessFactory

import { createHarnessFactory } from '@zorax/plug'

const defaultPlugins = [...]

const defaultOptions = {...}

export const createHarness = createHarnessFactory(
  defaultOptions,
  defaultPlugins,
)

Plugins

A plugin is a plain object with a name, and optional hooks.

Example

const myPlugin = {
  name: 'my plugin',

  description: 'optional description',

  // --- hooks --
  //
  // - in the order they are called
  //
  // - the 1st argument is the target obj (expected to be mutated by the hook)
  //
  // - t is the test context (can be test context, proxy or harness)
  // - z is the proxy (can be proxy or harness)
  // - h is the root harness (can be the same as t and/or z)
  //
  test(t, z, h) => {},
  harness(z, h) => {},
  init(h) => {},
  decorateTest(t, z, h) => {},
  decorateHarness(z, h) => {},
  decorateInit(h) => {},

  // - report hook -
  //
  // These hooks will be called before report of the root harness, even if
  // plugged from a child proxy (i.e. when the tests begins being executed).
  //
  // If the report hook returns a function, then it will be called after report
  // of the root harness, that is after the tests have completed and the result
  // reported.
  //
  report(h, args) => h => {
    // before report
    return () => {
      // after report
    }
  },
}

Hooks

  • test
  • harness
  • init
  • decorateTest
  • decorateHarness
  • decorateInit
  • report

Lifecycle

Plugins are passed the different context objects that are created during the tests lifecycle (harness, proxy, test context), and they are free to augment or modify them by directly mutating the object (e.g. adding properties, wrapping methods, etc.).

There is an additional "decorate phase" where plugins are not allowed to change existing references anymore (e.g. by wrapping a method). This phase is intended for adding decorators to methods themselves (e.g. add test.only, we need to be sure that the test function reference won't change).

Hooks order

Given plugins [pg1, pg2], hooks are called in the following order:

# mutate phase
pg1.test -> pg1.harness -> pg1.init ->
pg2.test -> pg2.harness -> pg2.init ->

# decorate phase
pg1.decorateTest -> pg1.decorateHarness -> pg1.decorateInit ->
pg2.decorateTest -> pg2.decorateHarness -> pg2.decorateInit

Type of targets

Not all types of test contexts go through all hooks.

  • the root test harness goes through all hooks: test, harness, init

  • harness proxies (returned by plug) go through: test, harness

  • and test contexts (created by test) go through: test

Arguments

Hooks of the different stages are called with the following arguments:

  • test hooks are called with (target, proxy, harness)

  • harness hooks are called with (target, harness)

  • init hooks are called with (harness) (which is also the target in this case)

Notes

  • plugins must mutate the target object that is passed to them; the return value of the hook is ignored

  • a plugin with a init stage (i.e. init or decorateInit hooks) can't be added with plug; it can only be added at harness creation

  • zorax.plug attaches a harness' options object and plugins array to the harness as harness.options and harness.plugins

Recipes

Access harness options

{
  test(t, { options: { ... }}) {},
  harness(z, { options: { ... }}) {},
  init(h) {
    const { options: { ... } } = h
  },
}

Skip decorating harness & proxies in test hook

{
  test(t, z, h) {
    if (t === z) return
    if (t === h) return
    // do your things
  }
}