Node 18's node:test, as a node module

Overview

node-core-test

CI

This is a user-land port of node:test, the experimental test runner introduced in Node.js 18. This module makes it available in Node.js 14 and later.

Minimal dependencies, with full test suite.

If we discover bugs in this implementation, I'm going to report them back to node core.

Differences from the core implementation:

  • Doesn't hide its own stack frames
  • Internally uses ._private property names instead of #private fields, for compatibility

Usage

const assert = require('assert')
const test = require('node-core-test')

test('synchronous passing test', t => {
  // This test passes because it does not throw an exception.
  assert.strictEqual(1, 1)
})

test('synchronous failing test', t => {
  // This test fails because it throws an exception.
  assert.strictEqual(1, 2)
})

test('asynchronous passing test', async t => {
  // This test passes because the Promise returned by the async
  // function is not rejected.
  assert.strictEqual(1, 1)
})

test('asynchronous failing test', async t => {
  // This test fails because the Promise returned by the async
  // function is rejected.
  assert.strictEqual(1, 2)
})

test('failing test using Promises', t => {
  // Promises can be used directly as well.
  return new Promise((resolve, reject) => {
    setImmediate(() => {
      reject(new Error('this will cause the test to fail'))
    })
  })
})

test('callback passing test', (t, done) => {
  // done() is the callback function. When the setImmediate() runs, it invokes
  // done() with no arguments.
  setImmediate(done)
})

test('callback failing test', (t, done) => {
  // When the setImmediate() runs, done() is invoked with an Error object and
  // the test fails.
  setImmediate(() => {
    done(new Error('callback failure'))
  })
})
$ node example.js
TAP version 13
ok 1 - synchronous passing test
  ---
  duration_ms: 0.001514889
  ...
not ok 2 - synchronous failing test
  ---
  duration_ms: 0.002878527
  failureType: 'testCodeFailure'
  error: 'Expected values to be strictly equal:\n\n1 !== 2\n'
  stack: |-
    Test.run (/Users/julian/dev/juliangruber/node-core-test/lib/test.js:347:17)
    Test.processPendingSubtests (/Users/julian/dev/juliangruber/node-core-test/lib/test.js:153:27)
    Test.postRun (/Users/julian/dev/juliangruber/node-core-test/lib/test.js:390:19)
    Test.run (/Users/julian/dev/juliangruber/node-core-test/lib/test.js:352:10)
    processTicksAndRejections (node:internal/process/task_queues:96:5)

...
(run it yourself to see the full output)
...

1..7
# tests 7
# pass 3
# fail 4
# skipped 0
# todo 0
$ echo $?

You can also run the tests using the tap CLI, or any other CLI that expects TAP output, for improved DX:

$ tap example.js

API

https://github.com/nodejs/node/blob/b476b1b91ef8715f096f815db5a0c8722b613678/doc/api/test.md

Kudos

Thank you @aduh95 for sharing the new node:test module in the @transloadit Slack.

License

MIT

Comments
  • Pull latest code from upstream

    Pull latest code from upstream

    I've pulled the latest version of all files from nodejs/node, changed the require calls, run (and upgrade) the linter. Tests are ~not~ passing, ~I must have missed something, opening as draft for now.~ tested on Node.js 14.19.0 and Node.js 19.0.0-pre :)

    opened by aduh95 5
  • 3.2.0 fails in node 14

    3.2.0 fails in node 14

    See eg https://github.com/juliangruber/parse-apache-directory-index/pull/28

    /Users/julian/dev/juliangruber/parse-apache-directory-index/node_modules/test/lib/internal/abort_controller.js:4
      AbortController,
      ^
    
    ReferenceError: AbortController is not defined
        at Object.<anonymous> (/Users/julian/dev/juliangruber/parse-apache-directory-index/node_modules/test/lib/internal/abort_controller.js:4:3)
    

    I wonder why tests didn't catch this. Is it because we pass our own AbortController implementation?

    bug 
    opened by juliangruber 4
  • fix: make test runner work even if there is no `AbortSignal` support

    fix: make test runner work even if there is no `AbortSignal` support

    Fixes a breaking change for Node.js v14.x that was introduced in 3.2.0. This also skips some test/message that rely on AbortSignal support. I suggest we land this and release in a semver-patch, then we revert and create a new semver-major. Thoughts?

    Fixes: https://github.com/nodejs/node-core-test/issues/35

    opened by aduh95 3
  • Pull latest commits from node core

    Pull latest commits from node core

    I've cherry-picked the commit the have landed on nodejs/node@main, applied the lint fixes (because we're using standard here for some reason), added some tweaks to make the tests pass on 14.x, and amended the commit messages to comply with Conventional Commit.

    NOTICE: This should be landed in fast-forward mode, not squashed.

    opened by aduh95 2
  • chore(main): release 3.2.1

    chore(main): release 3.2.1

    :robot: I have created a release beep boop

    3.2.1 (2022-08-03)

    Bug Fixes

    • make test runner work even if there is no AbortSignal support (#36) (cb2e4fd)

    This PR was generated with Release Please. See documentation.

    autorelease: tagged 
    opened by github-actions[bot] 1
  • chore(main): release 3.2.0

    chore(main): release 3.2.0

    :robot: I have created a release beep boop

    3.2.0 (2022-08-01)

    Features

    • add before/after/each hooks (4ed5d1f)
    • add support for boolean values for concurrency option (ba8fd71)
    • graceful termination on --test only (4071052)
    • pass signal on timeout (3814bf0)
    • recieve and pass AbortSignal (558abfc)
    • validate concurrency option (f875da2)
    • validate timeout option (cf78656)

    Bug Fixes

    • do not report an error when tests are passing (10146fa)
    • doc: add missing test runner option (b8bc3be)
    • doc: copyedit README.md (1401584)
    • doc: fix typos in test.md (8c95f07)
    • empty pending tests queue post running (2f02171)
    • fix top level describe queuing (c6f554c)
    • test: ensure all tests are run and fix failing ones (#33) (215621e)

    This PR was generated with Release Please. See documentation.

    autorelease: tagged 
    opened by github-actions[bot] 1
  • chore(main): release 3.1.0

    chore(main): release 3.1.0

    :robot: I have created a release beep boop

    3.1.0 (2022-07-20)

    Features

    • add Subtest to tap protocol output (fc0256b)
    • cancel on termination (826048c)
    • expose describe and it (e29cd3f)
    • support timeout for tests (5b6851f)

    Bug Fixes

    • catch errors thrown within describe (4ca48af)
    • ci: fix package name in release-please.yml (#25) (c132f7e)
    • doc: improve test runner timeout docs (751ffc6)
    • fix it concurrency (0a81cfc)
    • wait for stderr and stdout to complete (bee4a6a)

    This PR was generated with Release Please. See documentation.

    autorelease: tagged 
    opened by github-actions[bot] 1
  • test_runner_output test missing

    test_runner_output test missing

    https://github.com/nodejs/node/commits/main/test/message/test_runner_output.out https://github.com/nodejs/node/commits/main/test/message/test_runner_output.js

    These files are missing from this repository, @aduh95 do you know if that was an oversight or they were consciously left out?

    question 
    opened by juliangruber 1
  • chore(main): release 3.0.1

    chore(main): release 3.0.1

    opened by github-actions[bot] 1
  • test: tweak test:lint command + add test:lint:fix

    test: tweak test:lint command + add test:lint:fix

    This PR updates the lint:test command, removing the --fix flag from it, and adds a test:lint:fix command, which has the same functionality as the old lint:test.

    Others might have differing opinions, but in my experience I really don't usually want my default test command (that's used by other commands and is often something people run immediately when they clone/are trying to make a PR) to be making code changes.

    Figured I'd throw up a quick PR for this. If including --fix is a desired approach, feel free to close this PR.

    opened by bnb 1
  • chore(main): release 3.0.0

    chore(main): release 3.0.0

    :robot: I have created a release beep boop

    3.0.0 (2022-06-02)

    ⚠ BREAKING CHANGES

    • move to nodejs org (#9)

    Features

    Code Refactoring


    This PR was generated with Release Please. See documentation.

    autorelease: tagged 
    opened by github-actions[bot] 1
  • fix: improve types

    fix: improve types

    Various improvements to the TypeScript types:

    • Add TestOptions.only
    • Add done param to TestFn
    • Update test ReturnType to Promise<void>
    • Remove ItContext and replace param with in it fn type with done
    • Add exported hooks
    • Change TestContext.test to typeof test
    • Added TestContext.runOnly, TestContext.beforeEach, TestContext.afterEach, TestContext.name
    • Formatfile with prettier + prettier-plugin-jsdoc
    • Make all exported fn params optional

    I tried re-using the types from @types/node but these also seem incomplete/incorrect at places (e.g. SuiteFn) so instead based these changes on the README of this package.

    Suggestions for further improvements:

    • Add describe.skip describe.todo it.skip it.skip
    • Open PR to improve @types/node and sync?

    If these are welcome, happy to contribute another PR addressing those.

    opened by hongaar 2
  • Extra stack traces

    Extra stack traces

    This module behaves notably differently when it comes to printing stack traces when test groups fail.

    Here is a simple test:

    const assert = require('assert')
    const test = require('node:test')
    
    test('parent test', async t => {
      await t.test('sub test', async t => {
        assert(false)
      })
    })
    

    Node core (v18.8.0):

    $ node example.js
    TAP version 13
    # Subtest: parent test
        # Subtest: sub test
        not ok 1 - sub test
          ---
          duration_ms: 0.00944469
          failureType: 'testCodeFailure'
          error: |-
            The expression evaluated to a falsy value:
    
              assert(false)
    
          code: 'ERR_ASSERTION'
          stack: |-
            TestContext.<anonymous> (/Users/julian/dev/nodejs/node-core-test/example.js:8:5)
            Test.runInAsyncScope (node:async_hooks:203:9)
            Test.run (node:internal/test_runner/test:483:25)
            Test.start (node:internal/test_runner/test:410:17)
            TestContext.test (node:internal/test_runner/test:114:20)
            TestContext.<anonymous> (/Users/julian/dev/nodejs/node-core-test/example.js:7:11)
            Test.runInAsyncScope (node:async_hooks:203:9)
            Test.run (node:internal/test_runner/test:483:25)
            Test.start (node:internal/test_runner/test:410:17)
            Test.test (node:internal/test_runner/harness:155:18)
          ...
        1..1
    not ok 1 - parent test
      ---
      duration_ms: 0.011879383
      failureType: 'subtestsFailed'
      error: '1 subtest failed'
      code: 'ERR_TEST_FAILURE'
      ...
    1..1
    # tests 1
    # pass 0
    # fail 1
    # cancelled 0
    # skipped 0
    # todo 0
    # duration_ms 0.049244309
    

    This module:

    TAP version 13
    # Subtest: parent test
        # Subtest: sub test
        not ok 1 - sub test
          ---
          duration_ms: 0.011840078
          failureType: 'testCodeFailure'
          error: |-
            The expression evaluated to a falsy value:
    
              assert(false)
    
          code: 'ERR_ASSERTION'
          stack: |-
            TestContext.<anonymous> (/Users/julian/dev/nodejs/node-core-test/example.js:8:5)
            Test.runInAsyncScope (node:async_hooks:202:9)
            exports.ReflectApply (/Users/julian/dev/nodejs/node-core-test/lib/internal/per_context/primordials.js:32:56)
            Test.run (/Users/julian/dev/nodejs/node-core-test/lib/internal/test_runner/test.js:485:25)
            Test.start (/Users/julian/dev/nodejs/node-core-test/lib/internal/test_runner/test.js:412:17)
            TestContext.test (/Users/julian/dev/nodejs/node-core-test/lib/internal/test_runner/test.js:116:20)
            TestContext.<anonymous> (/Users/julian/dev/nodejs/node-core-test/example.js:7:11)
            Test.runInAsyncScope (node:async_hooks:202:9)
            exports.ReflectApply (/Users/julian/dev/nodejs/node-core-test/lib/internal/per_context/primordials.js:32:56)
            Test.run (/Users/julian/dev/nodejs/node-core-test/lib/internal/test_runner/test.js:485:25)
          ...
        1..1
    not ok 1 - parent test
      ---
      duration_ms: 0.015167603
      failureType: 'subtestsFailed'
      error: '1 subtest failed'
      code: 'ERR_TEST_FAILURE'
      stack: |-
        exports.ErrorCaptureStackTrace (/Users/julian/dev/nodejs/node-core-test/lib/internal/per_context/primordials.js:18:53)
        __node_internal_captureLargerStackTrace (/Users/julian/dev/nodejs/node-core-test/lib/internal/errors.js:311:5)
        new NodeError (/Users/julian/dev/nodejs/node-core-test/lib/internal/errors.js:238:5)
        Test.postRun (/Users/julian/dev/nodejs/node-core-test/lib/internal/test_runner/test.js:546:17)
        Test.run (/Users/julian/dev/nodejs/node-core-test/lib/internal/test_runner/test.js:513:10)
        process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      ...
    1..1
    # tests 1
    # pass 0
    # fail 1
    # cancelled 0
    # skipped 0
    # todo 0
    # duration_ms 0.122793632
    

    The stack trace from parent test shouldn't be shown. I haven't yet looked into why this is happening.

    bug 
    opened by juliangruber 0
  • Use same linter as node

    Use same linter as node

    • https://github.com/nodejs/node/blob/2fe4e9473fb50630326754886dee1d301a1a661e/Makefile#L1340-L1375
    • https://github.com/nodejs/node/blob/2fe4e9473fb50630326754886dee1d301a1a661e/.eslintrc.js
    • https://github.com/nodejs/node/tree/2fe4e9473fb50630326754886dee1d301a1a661e/tools/eslint-rules
    • https://github.com/nodejs/node/blob/2fe4e9473fb50630326754886dee1d301a1a661e/tools/node_modules/eslint-plugin-node-core/index.js
    enhancement 
    opened by juliangruber 3
Releases(v3.2.1)
  • v3.2.1(Aug 3, 2022)

  • v3.2.0(Aug 1, 2022)

    3.2.0 (2022-08-01)

    Features

    • add before/after/each hooks (4ed5d1f)
    • add support for boolean values for concurrency option (ba8fd71)
    • graceful termination on --test only (4071052)
    • pass signal on timeout (3814bf0)
    • recieve and pass AbortSignal (558abfc)
    • validate concurrency option (f875da2)
    • validate timeout option (cf78656)

    Bug Fixes

    • do not report an error when tests are passing (10146fa)
    • doc: add missing test runner option (b8bc3be)
    • doc: copyedit README.md (1401584)
    • doc: fix typos in test.md (8c95f07)
    • empty pending tests queue post running (2f02171)
    • fix top level describe queuing (c6f554c)
    • test: ensure all tests are run and fix failing ones (#33) (215621e)
    Source code(tar.gz)
    Source code(zip)
  • v3.1.0(Jul 20, 2022)

  • v3.0.1(Jun 15, 2022)

  • v3.0.0(Jun 2, 2022)

  • v2.0.0(May 1, 2022)

    • Pull latest code from upstream (#7) d6d706e
    • run GHA on pull requests (#8) 1c85b1b
    • add .vscode to .gitignore f370f7d
    • make file structure closer to Node.js' (#6) a3a9be7
    • ci: add node 18 (#5) 48ed727
    • docs: fix link 960aea8

    https://github.com/juliangruber/node-core-test/compare/v1.4.0...v2.0.0

    Source code(tar.gz)
    Source code(zip)
  • v1.4.0(Apr 18, 2022)

    • test_runner: add initial CLI runner (#4) f405a27
    • docs: fix reference c7c5229

    https://github.com/juliangruber/node-core-test/compare/v1.3.0...v1.4.0

    Source code(tar.gz)
    Source code(zip)
  • v1.3.0(Apr 16, 2022)

    • test_runner: support only tests (#3) dd77ccb e518123
    • add typescript dev dependency 76a33c6

    https://github.com/juliangruber/node-core-test/compare/v1.2.0...v1.3.0

    Source code(tar.gz)
    Source code(zip)
  • v1.2.0(Apr 7, 2022)

    • Publish typescript definitions (#2) 0922a50
    • docs ab8be46 c877fae

    • git ignore .nyc_output 0d0f3d5
    • update package-lock.json 566ca1d
    • standard 6663bdb

    https://github.com/juliangruber/node-core-test/compare/v1.1.1...v1.2.0

    Source code(tar.gz)
    Source code(zip)
  • v1.1.1(Mar 26, 2022)

    • standard ea94228
    • docs 6970779 227e987 eccc37b d2d86fd
    • add message test 23f8759 1758e9d a9cb1f6 896983d
    • fix matcher implementation 1758e9d
    • move reference comments 1433d4b
    • fix missing error code 896983d
    • port parallel tests a2cfd7a

    https://github.com/juliangruber/node-core-test/compare/v1.1.0...v1.1.1

    Source code(tar.gz)
    Source code(zip)
  • v1.1.0(Mar 24, 2022)

  • v1.0.0(Mar 23, 2022)

    • fix ci (temporarily) fb86a80
    • create .npmignore d9b864d
    • docs 9461022
    • add basic passing test ffe6e8e
    • standard 7d90537

    https://github.com/juliangruber/node-core-test/compare/23bf748941a2c157098f5f0d2d4671bdae1cfafa...v1.0.0

    Source code(tar.gz)
    Source code(zip)
Owner
Julian Gruber
Julian Gruber
Jester is a test-generation tool to create integration test code.

Code Generator for Integration Tests Introduction Welcome to Jester: An easy-to-use web application that helps you create and implement integration te

OSLabs Beta 54 Dec 12, 2022
Userland module that implements the module path mapping that Node.js does with "exports" in package.json

exports-map Userland module that implements the module path mapping that Node.js does with "exports" in package.json npm install exports-map Usage co

Mathias Buus 9 May 31, 2022
Fintoc.js ES Module - Use the Fintoc widget as an ES module

Fintoc.js ES Module Use the Fintoc widget as an ES module. Installation Install using npm! (or your favourite package manager) # Using npm npm install

Fintoc 6 May 13, 2022
Template Repository for making your own budder Module. CORE is not included, this is just for the module.

A quick copy of the "How to make your own module" section Check out the official budderAPI repository Template Repository for making your own budder M

Logic 2 Apr 3, 2022
A module federation SDK which is unrelated to tool chain for module consumer.

hel-micro, 模块联邦sdk化,免构建、热更新、工具链无关的微模块方案 Demo hel-loadash codesandbox hel-loadash git Why hel-micro 如何使用远程模块 仅需要一句npm命令即可载入远程模块,查看下面例子线上示例 1 安装hel-micr

腾讯TNTWeb前端团队 319 Jan 3, 2023
Inter Process Communication Module for node supporting Unix sockets, TCP, TLS, and UDP. Giving lightning speed on Linux, Mac, and Windows. Neural Networking in Node.JS

Inter Process Communication Module for node supporting Unix sockets, TCP, TLS, and UDP. Giving lightning speed on Linux, Mac, and Windows. Neural Networking in Node.JS

Node IPC 43 Dec 9, 2022
A list of helpful front-end related questions you can use to interview potential candidates, test yourself or completely ignore.

Front-end Developer Interview Questions This repository contains a number of front-end interview questions that can be used when vetting potential can

H5BP 56.1k Jan 4, 2023
A simple playground to create and test your Katas in Typescript.

Kata Playground TS A simple playground to create and test your Katas in Typescript. A code kata is an exercise in programming which helps programmers

Willian Justen 23 Jan 20, 2022
Brittle TAP test framework

brittle tap à la mode A fullstack TAP test runner built for modern times. API Initializers To create a test the exported test method can be used in a

David Mark Clements 72 Dec 14, 2022
10lift Applicant Test Senior Front End Development

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: ya

Barış ÖZDEMİRCİ 2 Sep 27, 2021
A stupid simple way to test

@asdgf A stupid simple way to test. Packages @asdgf/core The core of the test framework, and nothing else @asdgf/cli CLI tool to run tests in node, us

Pascal Schilp 16 Nov 17, 2022
This repository contains a basic example on how to set up and run test automation jobs with CircleCI and report results to Testmo.

CircleCI test automation example This repository contains a basic example on how to set up and run test automation jobs with CircleCI and report resul

Testmo 2 Dec 23, 2021
Mamera is a stupidly silly app developed to test CapacitorJS. It can be found on the iOS App Store.

Mamera This repo is focused on mobile app development for iOS. Although you may be able to build to Android from this repo, this ReadMe was written fo

Jamel 7 Mar 30, 2022
Example-browserstack-reporting - This repository contains an example of running Selenium tests and reporting BrowserStack test results, including full CI pipeline integration.

BrowserStack reporting and Selenium test result example This repository contains an example of running Selenium tests and reporting BrowserStack test

Testmo 1 Jan 1, 2022
Test for client-side script injection via NFTs

Rektosaurus A test suite to check for client-side script injection via NFTs. Overview NFTs contain a variety of metadata and content that gets process

Bernhard Mueller 42 Jun 28, 2022
This is a test parser which can automatically parse the tests in from websites like codeforces, codechef, atcoder etc.

✔ Sublime test parser This is a test parser which can automatically parse the tests in from websites like codeforces, codechef, atcoder etc. See how i

Prakhar Rai 15 Aug 6, 2022
This Project is made with HTML5, CSS3, ReactJS, Axios, MetaMask, thirdweb, Rinkeby Test Network, Web 3.0 Technologies, and OpenSea API.

Abstract Collections This Project is made with HTML5, CSS3, ReactJS, Axios, MetaMask, thirdweb, Rinkeby Test Network, Web 3.0 Technologies, and OpenSe

Shobhit Gupta 34 Jan 4, 2023
This tool allows you to test your chains.json file to see if your chains are available, syncing, or in sync.

Chains Tester This tool allows you to test your chains.json file to see if your chains are available, syncing, or in sync. This is an open source tool

Jorge S. Cuesta 9 Nov 4, 2022