@tapjs/core
pluggable core of node-tap
Last updated a year ago by isaacs .
BlueOak-1.0.0 · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @tapjs/core 
SYNC missed versions from official npm registry.

@tapjs/core

This is the pluggable core of node-tap.

The TestBase class has the basic flow-control and child-test aspects of a tap Test object, but only the t.pass() and t.fail() assertions.

All other assertions and features are added via plugins.

Writing Plugins

Tap plugins are a function that takes a Test object and an options object, and returns an object that is used as the extension.

For example, to add a isString method to all tests, you could define a plugin like this:

import { TestBase, TapPlugin, AssertionOpts } from '@tapjs/core'
export const plugin: TapPlugin = t => {
  return {
    isString: (
      s: any,
      msg: string = 'expect string',
      extra: AssertionOpts = {}
    ) => {
      // note: 'this' here is the plugin object
      if (typeof s === 'string') {
        return t.pass(msg)
      } else {
        return t.fail(msg, {
          ...extra,
          expect: 'string',
          actual: typeof s,
        })
      }
    }
}

The object returned by a plugin can be any sort of thing. If you want to use a class with private properties, that's totally fine as well. Whatever type is expected as the second argument will be combined with the built-in TestBaseOpts interface, and required when tests are instantiated.

import { TestBase, TapPlugin, AssertionOpts } from '@tapjs/core'
import { cleanup, render } from '@testing-library/react'
import { ReactElement } from 'react'
import { RenderResult } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
class ReactTest {
  #result: RenderResult
  constructor(node: ReactElement) {
    this.#result = render(node)
  }
  findByText(text: string) {
    return this.#result.findByText(text)
  }
  // add other helpful methods here...
}
export const plugin: TapPlugin = (t, { node: ReactElement }) => {
  return new ReactTest(node)
}

When loaded, this plugin would make it so that every test must supply a { node: ReactElement } option, and would have a t.findByText() method.

Loading Plugins

The easiest way to load plugins is by running:

tap plugin add <package or local module>

This will also regenerate the Test class, so types are kept in sync.

Remove plugins with tap plugin remove <plugin>.

Specifying Plugins Manually

Plugins are specified in the plugins array of the root tap config. That is, either .taprc in the project directory, or the "tap" stanza in the package.json file.

Whenever the plugins option is changed, you must run tap generate to generate the Test class properly. This is done automatically on demand when running tap or tap run [...files], but its often a good idea to do so before writing tests so that editor hinting is accurate.

Built-In plugins

Out of the box, tap comes with the following plugins loaded:

  • @tapjs/core/plugin/before-each Adds t.beforeEach(fn)
  • @tapjs/core/plugin/after-each Adds t.afterEach(fn)
  • @tapjs/core/plugin/stdin Adds t.stdin()
  • @tapjs/core/plugin/spawn Adds t.spawn()
  • @tapjs/asserts All other assertions, like t.match(), t.type(), t.has(), and so on.
  • @tapjs/snapshot Providing the t.matchSnapshot() method.
  • @tapjs/fixture Providing the t.testdir() method.
  • @tapjs/mock Providing the t.mock() method.

To prevent loading any of these plugins, you can include them in the plugins config, prefixed with a !. For example, if you wanted to replace t.mock() with a different mocking plugin, you could do this:

{
  "tap": {
    "plugins": ["!@tapjs/mock", "my-mock-plugin"]
  }
}

Plugin Collisions

The first plugin in a list that provides a given method or property will be the one that "wins", as far as the object presented in test code is concerned.

However, within a given plugin, it only sees itself and the TestBase object it's been given. For example, if returning an object constructed from a class defined in the plugin, this will refer to that object, always.

// first-plugin
export const plugin = (t: TestBase) => {
  return {
    // this is the first plugin to register this value
    // so this is what shows up on the Test object
    myVal: 4,
    getFirstPluginVal() {
      return this.myVal // always returns 4
    },
    // this is the first plugin to register this method
    // so this is what shows up on the Test object
    getFour() {
      return 4
    },
  }
}
// second-plugin
export const plugin = (t: TestBase) => {
  return {
    // user will never see this, because first-plugin registered it
    myVal: 5,
    getSecondPluginValue() {
      return this.myVal // always returns 5
    },
    // overridden, this isn't the 'getFour' that the user will see
    getFour() {
      return 'four'
    },
  }
}

Then in the test:

import t from 'tap'
console.log(t.myVal) // 4, not 5
console.log(t.getFour()) // 4, not 'four'
console.log(t.getFirstPluginVal()) // 4
console.log(t.getSecondPluginVal()) // 5

Accessing the Constructed Plugged-In Test Object

If you need access to the constructed Test object, you can get that after the initial plugin load, via t.t. However, it will be undefined until all plugins are done loading.

// my-plugin.ts
export const plugin = (t: TestBase) => {
  // here, t.t === undefined
  return {
    someMethod() {
      // here, t.t is the object with all the plugins applied
    },
  }
}

Current Tags

  • 4.0.0                                ...           latest (6 months ago)
  • 1.0.0                                ...           pre (a year ago)

68 Versions

  • 4.0.0                                ...           6 months ago
  • 3.0.3                                ...           6 months ago
  • 3.0.2                                ...           6 months ago
  • 3.0.1                                ...           6 months ago
  • 3.0.0                                ...           6 months ago
  • 2.1.6                                ...           6 months ago
  • 2.1.5                                ...           6 months ago
  • 2.1.4                                ...           7 months ago
  • 2.1.3                                ...           7 months ago
  • 2.1.2                                ...           7 months ago
  • 2.1.1                                ...           7 months ago
  • 2.1.0                                ...           7 months ago
  • 2.0.1                                ...           7 months ago
  • 2.0.0                                ...           7 months ago
  • 1.5.4                                ...           7 months ago
  • 1.5.3                                ...           8 months ago
  • 1.5.2                                ...           9 months ago
  • 1.5.1                                ...           10 months ago
  • 1.5.0                                ...           a year ago
  • 1.4.6                                ...           a year ago
  • 1.4.5                                ...           a year ago
  • 1.4.4                                ...           a year ago
  • 1.4.3                                ...           a year ago
  • 1.4.2                                ...           a year ago
  • 1.4.1                                ...           a year ago
  • 1.4.0                                ...           a year ago
  • 1.3.10                                ...           a year ago
  • 1.3.9                                ...           a year ago
  • 1.3.8                                ...           a year ago
  • 1.3.7                                ...           a year ago
  • 1.3.6                                ...           a year ago
  • 1.3.5                                ...           a year ago
  • 1.3.4                                ...           a year ago
  • 1.3.3                                ...           a year ago
  • 1.3.2                                ...           a year ago
  • 1.3.1                                ...           a year ago
  • 1.3.0                                ...           a year ago
  • 1.2.0                                ...           a year ago
  • 1.1.2                                ...           a year ago
  • 1.1.1                                ...           a year ago
  • 1.1.0                                ...           a year ago
  • 1.0.3                                ...           a year ago
  • 1.0.2                                ...           a year ago
  • 1.0.1                                ...           a year ago
  • 1.0.0                                ...           a year ago
  • 0.0.0-22                                ...           a year ago
  • 0.0.0-21                                ...           a year ago
  • 0.0.0-20                                ...           a year ago
  • 0.0.0-19                                ...           a year ago
  • 0.0.0-18                                ...           a year ago
  • 0.0.0-17                                ...           a year ago
  • 0.0.0-16                                ...           a year ago
  • 0.0.0-15                                ...           a year ago
  • 0.0.0-14                                ...           a year ago
  • 0.0.0-13                                ...           a year ago
  • 0.0.0-12                                ...           a year ago
  • 0.0.0-11                                ...           a year ago
  • 0.0.0-10                                ...           a year ago
  • 0.0.0-9                                ...           a year ago
  • 0.0.0-8                                ...           a year ago
  • 0.0.0-7                                ...           a year ago
  • 0.0.0-6                                ...           a year ago
  • 0.0.0-5                                ...           a year ago
  • 0.0.0-4                                ...           a year ago
  • 0.0.0-3                                ...           a year ago
  • 0.0.0-2                                ...           a year ago
  • 0.0.0-1                                ...           a year ago
  • 0.0.0-0                                ...           a year ago
Maintainers (2)
Downloads
Total 2
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (10)
Dev Dependencies (0)
None
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE