Skip to content
Back to skills

Mocha Chain

ASecurity

Writing Mocha tests with mocha-chain (hooks that return typed context to later hooks and tests, which hooks may chain from which, and why a hook's side effects still reach every test in its suite)

  • 9 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 9, 2026
documentationgogit

Security analysis

A100/100

Scanned November 9, 2026

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

Installs into .claude/skills of the current project.

Are you the author of Mocha Chain?

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

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

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: mocha-chain
description: Writing Mocha tests with mocha-chain (hooks that return typed context to later hooks and tests, which hooks may chain from which, and why a hook's side effects still reach every test in its suite)
---

# mocha-chain

`mocha-chain` mirrors Mocha's TDD interface (`suite`, `before`, `beforeEach`, `test`, `afterEach`, `after`) and adds typed context: an object a hook returns is shallow-merged into the context passed to whatever is chained from it. Import them from `mocha-chain` instead of using Mocha's globals; `mocha` stays a dev dependency, as the runner. Reference: the package's [README](https://github.com/JacobLey/leyman/blob/main/tools/test-chain/mocha-chain/README.md).

```ts
import { before, suite } from 'mocha-chain';

suite('Users', () => {
    const withDb = before(async () => ({ db: await connect() }));
    const withUser = withDb.beforeEach(() => ({ user: fakeUser() }));

    withUser.test('Inserts a user', async ({ db, user }) => {
        expect(await db.insert(user)).to.equal(true);
    });

    withDb.after(async ({ db }) => {
        await db.close();
    });
});
```

## Rules

- **The chain passes context; the suite decides which hooks run.** `withA.beforeEach(fn)` is a plain Mocha `beforeEach` of the suite it's called in: it runs before every test there, including tests chained from other handles, which just don't receive its context. A hook with side effects only some tests want (inserting rows, starting a server) goes in a nested `suite` with those tests.
- A hook returns a plain object (or a `Promise` of one), or nothing. Return `{ db }`, never a class instance: only own enumerable properties are copied, and an instance fails with `HookContextError`. An arrow function without braces returns what its call returns, so use braces when a hook shouldn't add context.
- Return a new key rather than one an earlier hook returned: the later value wins at runtime, but the type is the intersection of both.
- What chains from what follows when hooks run: `before` chains to anything; `beforeEach` to `beforeEach`, `test` and `afterEach`; `afterEach` only to `afterEach`; `after` only to `after`. A hook that would run before the one it chains from fails with `HookOrderError`.
- Never declare a test or hook inside another test or hook: Mocha ignores it silently, `mocha-chain` throws.
- Everything else is Mocha's: `this` as the test or hook (use `function`, typed `this: Context`, for `this.timeout()`), a second `done` parameter, named hooks, `.only` and `.skip`.

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 documentation →

Comments

Loading comments…

Newsletter

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