esdev test

Find and run test files with the assertion vocabulary provided by runtime:test. Both .test. and .spec. files are discovered in the supported JavaScript and TypeScript extensions.

Shell
esdev test                    # every *.test.* and *.spec.*
esdev test math               # ...whose path contains "math"
esdev test --file=x.test.ts   # exactly one file

JSX in tests

A test file may be .jsx or .tsx. Which JSX it is comes from the project's jsx section, or from the file's own @jsxImportSource/@jsx pragma. There is no default: JSX with nothing said about it is refused rather than compiled for a framework nobody named.

JSON
{ "jsx": { "importSource": "preact" } }

The same setting compiles the bundle, so a component means the same thing in a test and in a build.

The project's plugins resolve, load and compile every module a test loads, as in a build, before the jsx setting does: a framework's .jsx, a virtual module, a stylesheet or an image means the same thing in a test. See Where plugins run.

DOM tests

Pass --dom to give each test file a fresh, in-memory DOM: document, elements, events, forms, selectors, styles, custom elements, Shadow DOM and MutationObserver. It is available to tests only; ordinary esdev runs do not get these globals.

Shell
esdev test --dom

For a test that needs layout or anything else only a real browser has, run the same file with --browser. See DOM testing and Browser testing.

Test guide

  • Writing tests — assertions, helpers, and table-driven cases.

  • Running and isolating tests — filters, process boundaries, and hangs.

  • Snapshots — recorded values and files, reviewed and updated.

  • Fixtures — what tests need, set up and torn down with test.extend.

  • Tags — label tests, run them by tag, and give a tag its options.

  • Type tests — expectTypeOf and assertType, checked by TypeScript.

  • Benchmarks — measure and compare with esdev bench.

  • Screenshots — compare an element's pixels with a reference image, in browser runs.

  • Mocks and fake timers — mock and clock.

  • Configuration — setup modules, time limits and the JSON reporter.

  • Global setup — a database or server started once for the whole run.

  • Reporters — JSON, JUnit XML, TAP and dots, for CI.

  • Coverage — which statements, branches, functions and lines the tests ran.

  • Imports — extensions, aliases, tsconfig.json paths and CommonJS packages, resolved as the build resolves them.

  • DOM testing — the in-memory DOM behind --dom.

  • Browser testing — the same files in a real browser.

Exits non-zero if any file fails.

Last updated on
Edit this page