Skip to content
Back to skills

Testing

ASecurity

Writing and running tests (Mocha, mocha-chain, Sinon, C8)

  • 9 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added October 5, 2026
testingtypescriptgobashsqlawstestingdebuggingapidatabase

Works with

  • vscode
  • cli
  • api

Security analysis

A100/100

Scanned November 25, 2026

npx -y skills add JacobLey/leyman --skill testing --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Testing?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Testing
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/jacobley-testing/badge)](https://www.skillsdirectory.com/skills/jacobley-testing)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: testing
description: Writing and running tests (Mocha, mocha-chain, Sinon, C8)
---

# Testing

> Config: [`../../../nx.json`](../../../nx.json) (targets: `mocha-unit-test`, `mocha-integration-test`, `vitest-unit-test`, `coverage-report`) · [`c8rc.json`](../../../leyman/configs/c8rc.json)

## Running

```bash
test-only   # build + test (no lint), stops on first failure
test-ci     # verify: lint + build + test + coverage-report — what CI runs
```

Tests run against compiled `dist/`, so a test run always rebuilds first (cached when unchanged).

To run one test, use the play button in VS Code ([`vscode-tests`](../../../leyman/vscode-tests/README.md)), or the commands in the [debugging skill](../debugging/SKILL.md#running-a-single-test-file).

## Coverage

100% lines, statements, functions and branches, enforced by `coverage-report` as the last step of `test` (`test:report`). An incomplete package fails `test`.

Coverage is per package: only the package's own tests count, not tests of packages that depend on it. Every package must fully test itself.

Checks that span packages, which no single package can test, go in a test-only package under `e2e/` (see [create-package](../create-package/SKILL.md#e2e-packages)). These have no source of their own, so no coverage report.

Packages whose job is talking to a service (e.g. leyman's [`lease-cache`](../../../tools/lease-cache/) on Valkey, and demos in `demos/`, see [create-package](../create-package/SKILL.md#demo-packages)) test against the real service, running as a devcontainer sidecar and a Dagger service, rather than a mock. A mock could only assume how the service behaves, and that behavior (atomicity, expiry) is what these packages rely on. Each test isolates its keys (e.g. by a unique prefix) rather than clearing the shared service.

The services, at the same hostnames in the devcontainer and in Dagger's `Test`:

| Service | Reached at | Isolation |
|---|---|---|
| Valkey | `valkey:6379` | A unique key prefix per test |
| Postgres | `DATABASE_URL` (`postgres://postgres:postgres@postgres:5432/postgres`) | A database of its own per test, never the shared `postgres` database. Keep each test's pool small (a few connections): tests across parallel Nx tasks share the server's `max_connections` |

AWS Emulator (DynamoDB and other AWS APIs) is no sidecar: it serves only HTTPS with a certificate for `localhost`, so each test process starts its own with [`aws-emulator-launch`](../../../tools/aws-emulator-launch/) (a dev dependency in TypeScript, a `require` with a `replace` in Go), which returns its endpoint, region, keys and CA, and stops it when done (e.g. [`idempotency-keys`' helper](../../../tools/idempotency-keys/ts/test/helpers/dynamodb.ts)). The helper catches its "could not run" error to say to rebuild the devcontainer. Add the `aws-emulator` named input to the test target, so it reruns when the emulator's version changes. It holds everything in memory, so a test run creates the tables it uses.

Without the service a test needs, it fails rather than skipping. Neither service keeps data across restarts, so a test sets up everything it relies on.

### Postgres tests

Get each test's database from [`pg-dolly`](../../../tools/pg-dolly/) (a dev dependency): a clone of a template built once per test process from the package's committed `schema.sql`, which the [`schema-snapshot`](../nx-tasks-reference/SKILL.md#schema-snapshot) target keeps up to date with its migrations. `pg-dolly` itself works with any runner; in this repo, follow its README's [mocha-chain pattern](../../../tools/pg-dolly/README.md#mocha-chain):

- `test/helpers/database.ts` connects and builds the template in a root `before` (top level of the module), and closes in its `after`. Only `before`/`after` at the top level: a top-level `beforeEach` would clone a database for every test in the package.
- It exports a `withDatabase()` fixture, called inside each suite that needs one: it clones in `beforeEach`, and keeps a failed test's database (`database.keep()`) in `afterEach`, so it can be inspected (`psql` with the printed name).
- The test connects with the package's own client code, from `database.url`, with a small pool, and closes it in `afterEach`.
- Data a test starts from is inserted by the test (or a fixture builder next to it). Don't assert values the database makes up (`now()`, generated ids) unless the test passes them in.

100% is the floor, not the goal: two `if`s are fully covered by two tests but have four paths. If code is hard to cover, first try controlling its real inputs: write fixtures to a temp dir, or run the CLI as a subprocess (coverage of child processes counts). Reach for `haywire` DI only when the real dependency can't be put into the needed state — see "Dependency injection" in the [coding-patterns skill](../coding-patterns/SKILL.md#dependency-injection-when-to-use-it-when-not-to). Never reach into internals instead.

## Layout

```
test/unit/          ← mirrors src/ (src/lib/foo.ts → test/unit/lib/foo.spec.ts)
test/integration/   ← user-level workflows (CLI runs, real file system), not per-file
test/data/          ← fixtures (any file type)
test/tsconfig.json  ← { "extends": "@leyman/configs/tsconfig.test.json", "references": [{ "path": ".." }] }
```

The reference to the package's own project is for the editor: tests import the package by name, which resolves to its `dist/`, and builds delete and rewrite `dist/`. With the reference, the language server reads `src/` instead, so a rebuild doesn't break types in open test files. `tsc -p ./test` still checks against `dist/`. e2e packages have no project of their own to reference.

Not every file needs its own spec; files exercised as a side effect of other tests are fine.

Unit tests keep Mocha's 2s timeout. Integration tests get 10s, since starting processes is slow on a loaded machine. Give a test more with `this.timeout()` when it runs something heavier, like Nx or a bundler.

Never make a test depend on how long something takes. Assert on order or state, not elapsed time: timers fire in order of expiry, and a timer callback runs only after pending promises settle.

Tests are compiled into `dist-test/` (`tsc-test`) and run against the package's real `dist/`, type-checked against its published `.d.ts`. Paths built from `import.meta.dirname` resolve inside `dist-test/`, so use `../../test/data/...` to reach source fixtures.

## Importing the code under test

- **Public API first**: import the package by its own name (`import { identifier } from 'haywire'`). This tests what consumers get.
- **Internals only when needed**: edge cases that are impractical to reach through the public API import `#internal/<path in dist>` (e.g. `#internal/lib/barrel.js`), mapped in `package.json`:

  ```json
  "imports": { "#internal/*": "./dist/*" }
  ```

  Reuse an existing `#alias` when the package already defines one for that file.
- **Test helpers and fixtures by subpath, not `../`**: `import-x/no-relative-parent-imports` rejects `../helpers.js`, so map the compiled tests and import from there (`import { api } from '#test/data/api.js'`):

  ```json
  "imports": { "#internal/*": "./dist/*", "#test/*": "./dist-test/*" }
  ```

  `tsconfig.test.json` sets `rootDir` and `outDir` so TypeScript reads `#test/...` from `test/`. `src/` likewise imports its own files outside the current folder as `#internal/...`.
- **Never import `../../src/...`**: it would load a second copy of the module (breaking `instanceof` and singletons) and skip the published types.

## Libraries

- **[`mocha-chain`](../../../tools/test-chain/mocha-chain/)** — import `suite`, `test`, `beforeEach`, etc. from here instead of using Mocha globals. How to write tests with it, and why a chained hook still runs for every test in its suite: the [`mocha-chain` skill](../mocha-chain/SKILL.md), which the package ships. Use `vitest-chain` + the `vitest-unit-test` target only for Vitest-specific packages.
- **[`@leyman/expect`](../../../leyman/expect/)** — `import { expect } from '@leyman/expect'`. Chai with `chai-as-promised` registered. Depend on it (dev) instead of `chai`.
- **`expect-type`** — `expectTypeOf(...)` for compile-time type assertions alongside runtime ones.
- **Sinon** — stubs to inject via DI, only for dependencies that actually need DI (not file system or console access). Restore after every test:

```ts
import { verifyAndRestore } from 'sinon';
import { afterEach } from 'mocha-chain';

afterEach(() => {
    verifyAndRestore();
});
```

See the "Tests" sections of the [coding-patterns skill](../coding-patterns/SKILL.md) for how to write individual tests.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in testing →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.