Snapshot Testing

Snapshot testing in Toolproof allows you to capture the output of retrievals (like file contents, stdout, or browser elements) and verify that they match the last-known correct value.

#What are Snapshots?

Snapshots in Toolproof:

Unlike traditional assertions that require you to manually specify the expected output, snapshots let you verify that your application’s output hasn’t changed unexpectedly.

#Creating a Snapshot

Any retrieval in Toolproof can be turned into a snapshot. To create a snapshot, use the snapshot key instead of the step key:

steps:
  - snapshot: stdout
  - snapshot: The file "index.html"
  - snapshot: In my browser, the result of {js}
    js: return document.querySelector('h1').innerText;

When you first run a test with snapshots in interactive mode (-i), Toolproof will capture the output and prompt you to accept it.

After accepting, the snapshot is stored directly in your test file:

steps:
  - snapshot: stdout
    snapshot_content: |-
      ╎Hello, world!
      ╎This is a test.      
  - snapshot: The file "index.html"
    snapshot_content: |-
      ╎<html>
      ╎  <body>
      ╎    <h1>Hello world</h1>
      ╎  </body>
      ╎</html>      
  - snapshot: In my browser, the result of {js}
    js: return document.querySelector('h1').innerText;
    snapshot_content: |-
      ╎Hello world      

#Snapshot Verification

In subsequent test runs, Toolproof will:

  1. Execute the retrieval step
  2. Compare the result against the stored snapshot_content
  3. Pass the test if they match
  4. Fail the test and show a diff if they don’t match

#Updating Snapshots

When your application changes intentionally, you’ll need to update your snapshots. To update snapshots:

  1. Run Toolproof in interactive mode with the -i flag: npx toolproof -i
  2. When a snapshot mismatch occurs, Toolproof will show the diff and prompt you
  3. Press y to accept the new snapshot or N to reject it and fail the test

#Limitations

#Relationship with Extracts

While snapshots are used for verification, the extract feature is used to save output to a file without verification:

steps:
  - extract: stdout
    extract_location: "output.txt"

Unlike snapshots, extracts don’t validate content but are useful for debugging or integrating with other tools.