EXAMPLES/snapshot-testing

Snapshot Testing

Demonstrates snapshot assertions for verifying scan output. Shows how to annotate snapshot files with region tags for extracting documentation snippets.

meta-yml8 FILEStestingsnapshotsadvanced

Snapshot Testing

Demonstrates snapshot assertions for verifying scan output.

What This Shows

  • Using the snapshot assertion type to compare actual output against a reference file
  • First-run auto-creation of snapshot files
  • Updating snapshots with functional-examples test -u
  • Multi-step test definitions (scan → verify → cleanup)

Sub-Examples

Greeting Function

/**
 * Generate a greeting message.
 */
export function greet(name: string): string {
  return `Hello, ${name}!`;
}
text
1Test definition with snapshot assertions:
2
3```json title="examples/greeting/package.json#snapshot-test"
4  "functional-examples": {
5    "title": "Greeting Function",
6    "test": [
7      {
8        "name": "scan output matches snapshot",
9        "steps": [
10          {
11            "command": "bash ../../scan.sh",
12            "assertions": {
13              "exitCode": 0
14            }
15          },
16          {
17            "command": "true",
18            "assertions": {
19              "snapshot": {
20                "path": "../../output.txt",
21                "snapshot": "../../__snapshots__/scan-output.txt"
22              }
23            }
24          }
25        ]
26      },
27      {
28        "name": "cleanup",
29        "options": {
30          "command": "rm -f ../../output.txt"
31        },
32        "assertions": {
33          "exitCode": 0
34        }
35      }
36    ]
37  },

Running

bash
1bash scan.sh          # Scan and save output to output.txt

How It Works

  1. scan.sh runs functional-examples scan and redirects output to output.txt
  2. The snapshot assertion compares output.txt against __snapshots__/scan-output.txt
  3. On first run, the snapshot is auto-created from the actual output
  4. On subsequent runs, mismatches fail the test — run functional-examples test -u to update

Found 2 example(s)

ID Title Files Extractor

snapshot-greeting Greeting Function 1 javascript-extractor greeting-example Greeting Function 2 javascript-extractor


All Example Files

FILE EXPLORER
README.md
1# Snapshot Testing
2
3Demonstrates snapshot assertions for verifying scan output.
4
5## What This Shows
6
7- Using the `snapshot` assertion type to compare actual output against a reference file
8- First-run auto-creation of snapshot files
9- Updating snapshots with `functional-examples test -u`
10- Multi-step test definitions (scan → verify → cleanup)
11
12## Sub-Examples
13
14### Greeting Function
15
16<%= region('greet') %>
17
18Test definition with snapshot assertions:
19
20<%= region('snapshot-test') %>
21
22## Running
23
24```bash
25bash scan.sh          # Scan and save output to output.txt
26```
27
28## How It Works
29
301. `scan.sh` runs `functional-examples scan` and redirects output to `output.txt`
312. The `snapshot` assertion compares `output.txt` against `__snapshots__/scan-output.txt`
323. On first run, the snapshot is auto-created from the actual output
334. On subsequent runs, mismatches fail the test — run `functional-examples test -u` to update
34