avajs / ava

Node.js test runner that lets you develop with confidence 🚀
MIT License
20.74k stars 1.41k forks source link

Skipping snapshots with ids can cause other snapshots to fail or write spurious snapshots #2662

Closed ninevra closed 3 years ago

ninevra commented 3 years ago

What you're trying to do

Skip a snapshot assertion that has an id.

(I'm still just reading/working on the code and testing edge cases. This one could affect someone in actual usage, though.)

What happened

Normally, snapshots only increment nextSnapshotIndex if they don't have an id. Skipped snapshots, however, always increment nextSnapshotIndex. If a snapshot with an id is skipped, it effectively pushes down any subsequent snapshot assertions, causing them to compare against the expected value of the next snapshot in order. If there was a next snapshot, it will probably fail; if not, it will record a new snapshot that doesn't actually exist.

What you expected to happen

Skipping an assertion should have no effect on the behavior of other assertions.

Sample code

const test = require('ava');

test('some snapshots', t => {
  t.snapshot(null, {id: 'an id'}); // <--
  t.snapshot('one');
  t.snapshot('two');
});

Run once, then change the first line of the test to t.snapshot.skip(null, {id: 'an id'}); and run again. The second assertion will fail and the third will record a "Snapshot 3".

MRE

Configuration

novemberborn commented 3 years ago

https://github.com/avajs/ava/issues/2669