Skip to content
jaetaskPublic

About

Expect tests for TypeScript. Run real code, approve the output, never hand-write an assertion again.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

snap

Expect tests for TypeScript. Each case is an input file. snap renders it to Markdown and compares the result with an approved name.expect.md beside it. A person reads and approves each expect file, so the test is the real output, not a second copy of the logic.

// expect/snap.ts
import { snap, table } from "@jaetask/snap"

const failed = await snap([{
  dir: "expect/cases",
  render: (input) => table(["x"], [[JSON.parse(input).x]]),
}])
process.exit(failed === 0 ? 0 : 1)
  • snap compares every case. A changed output fails and prints the diff.
  • snap --update writes the new expect files. Review them in the pull request.
  • A case named error.* must throw. Its message is the approved output.
  • An expect file without an input fails. --update removes it.
  • Each case runs twice. Output that differs between the runs fails.

render may return a promise. table(headers, rows) writes a Markdown table. Failure output needs diff on PATH.

Install

npm i -D @jaetask/snap

Or from GitHub at a tag: pnpm add -D github:jaetask/snap#v0.1.1. That builds on install, so pnpm 10 needs "@jaetask/snap": true under allowBuilds in pnpm-workspace.yaml. Node 22 or later.

A version tag (v0.1.1) publishes that version to npm through the publish workflow and npm trusted publishing, so no npm token is stored.

Credits

With thanks to the React team. snap follows the idea of the snap test runner they built for the React Compiler: fixtures in, approved Markdown out, reviewed like code. This is a separate, smaller implementation, not their code.

MIT

About

Expect tests for TypeScript. Run real code, approve the output, never hand-write an assertion again.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages