Set up tests - uhop/tape-six GitHub Wiki

Configuring tests

To determine what tests to run and how to access them the test harness uses the following algorithm:

  1. In the current directory it tries to read tape6.json file.
  2. If it is not present, it tries to read tape6 section of package.json file.
  3. Otherwise it assumes the default value:
    {
      "tests": ["/tests/test-*.js", "/tests/test-*.mjs"],
      "cli": ["/tests/test-*.cjs"]
    }
    

When the configuration snippet is found, it is used to configure the test harness:

  1. We start with an empty list of patterns.
  2. A type-related subsection is read. It can be one of the following: node, deno, bun, or browser depending on the type of the test harness. If present, it is added to the list of patterns.
  3. If the type is not browser, a cli subsection is read. If it is present, it is added to the list of patterns.
  4. A tests subsection is read. If it is present, it is added to the list of patterns.
  5. A browser reads the importmap section. It is used to configure the browser test harness.

importmap is the standard import map used in browser tests. See importmap for more details.

Example of tape6.json or the tape6 section of package.json:

{
  "tests": ["/tests/test-*.*js"],
  "importmap": {
    "imports": {
      "tape-six": "../index.js",
      "tape-six/": "../src/"
    }
  }
}

The example above is taken from package.json of tape-six.

Example of tape6 section of package.json with tests for different environments:

{
  "tape6": {
    "node": ["/tests/node/test-*.js", "/node/tests/test-*.js"],
    "deno": ["/tests/deno/test-*.js", "/deno/tests/test-*.js"],
    "tests": ["/tests/test-*.js"],
    "importmap": {
      "imports": {
        "tape-six": "/node_modules/tape-six/index.js",
        "tape-six/": "/node_modules/tape-six/src/"
      }
    }
  }
}

Types of test files

Any JavaScript files can be used as test files. It includes files with the following extensions: .js, .cjs, .mjs.

All modern runtimes (Node, Bun, Deno) support running TypeScript files directly without transpilation. It includes files with .ts, .cts, .mts extensions. Browsers cannot handle TypeScript files directly.

Browsers can support HTML files, which can be loaded in an iframe. Otherwise they can load JavaScript files as ES modules. CommonJS modules are not supported.

Running tests

When tests are properly configured, they can be run using the following commands (a snippet from package.json):

{
  "scripts": {
    "test": "tape6 --flags FO",
    "test:node": "tape6-node --flags FO",
    "test:bun": "tape6-bun --flags FO",
    "test:deno": "tape6-deno --flags FO",
    "test:deno-alt": "deno run --allow-read --allow-env --allow-run `tape6-runner main` --flags FO",
    "test:server": "tape6-server --trace"
  }
}

The scripts above allow running tests in different environments. test:deno-alt shows how to specify detailed permissions for Deno. They can be used like that:

npm test              # Node
npm run test          # Node
npm run test:node     # Node
npm run test:bun      # Bun
npm run test:deno     # Deno
npm run test:deno-alt # Deno

npm run test:server starts a server that serves tests and the tape6 web application for browsers. You can navigate to the shown URL (by default http://localhost:3000) or use a script based on tools like Puppeteer or Playwright. An example script for Puppeteer can be found in Environment: Browsers.