Snapshots
Snapshot testing is valuable for verifying the output of commands or scripts over time. By capturing and comparing the "snapshot" of the output at different stages, you can easily spot unintended changes or regressions. This way, it helps maintain the expected behavior while modifications are being made, making the verification process more efficient and reliable.
assert_match_snapshot
assert_match_snapshot "actual"
Reports an error if actual
does not match the existing snapshot file associated with the current test function. If no such file exists, a new one is created with the provided value.
TIP
You can update the snapshot by deleting it and running its test again.
function test_success() {
assert_match_snapshot "$(ls)"
}
function test_failure() {
assert_match_snapshot "$(date)"
}
Running snapshot_test.sh
✎ Snapshot: Success
✎ Snapshot: Failure
Tests: 2 snapshot, 2 total
Assertions: 2 snapshot, 2 total
Some snapshots created
Running snapshot_test.sh
✓ Passed: Success
✗ Failed: Failure
Expected to match the snapshot
Mon Jul 27 [-13:37:46-]{+13:37:49+} UTC 1987
Tests: 1 passed, 1 failed, 2 total
Assertions: 1 passed, 1 failed, 2 total
Some tests failed
WARNING
You need to run the tests for this example twice to see them work. The first time you run them, the snapshots will be generated and the second time they will be asserted.