-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect more forms of snapshot file corruption
- Loading branch information
Showing
7 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import {promises as fs} from 'fs'; | ||
import path from 'path'; | ||
|
||
import test from '@ava/test'; | ||
|
||
import {cwd, fixture} from '../helpers/exec.js'; | ||
import {withTemporaryFixture} from '../helpers/with-temporary-fixture.js'; | ||
|
||
function countMatches(string, regex) { | ||
return [...string.matchAll(regex)].length; | ||
} | ||
|
||
function countStringMatches(string, patternString) { | ||
if (patternString.length === 0) { | ||
throw new RangeError('Pattern must be non-empty'); | ||
} | ||
|
||
let index = string.indexOf(patternString); | ||
let matches = 0; | ||
while (index !== -1 && index < string.length) { | ||
matches++; | ||
index = string.indexOf(patternString, index + patternString.length); | ||
} | ||
|
||
return matches; | ||
} | ||
|
||
test('snapshot corruption is reported to the console', async t => { | ||
await withTemporaryFixture(cwd('corrupt'), async cwd => { | ||
const snapPath = path.join(cwd, 'test.js.snap'); | ||
await fs.writeFile(snapPath, Uint8Array.of(0x00)); | ||
const result = await t.throwsAsync(fixture([], {cwd})); | ||
t.snapshot(result.stats.failed, 'failed tests'); | ||
t.is(countMatches(result.stdout, /The snapshot file is corrupted./g), 2); | ||
t.is(countMatches(result.stdout, /File path:/g), 2); | ||
t.is( | ||
countMatches( | ||
result.stdout, | ||
/Please run AVA again with the .*--update-snapshots.* flag to recreate it\./g | ||
), | ||
2 | ||
); | ||
t.is(countStringMatches(result.stdout, snapPath), 2); | ||
}); | ||
}); | ||
|
||
test('with --update-snapshots, corrupt snapshot files are overwritten', async t => { | ||
await withTemporaryFixture(cwd('corrupt'), async cwd => { | ||
const snapPath = path.join(cwd, 'test.js.snap'); | ||
const env = { | ||
AVA_FORCE_CI: 'not-ci' | ||
}; | ||
await fs.writeFile(snapPath, Uint8Array.of(0x00)); | ||
await fixture(['--update-snapshots'], {cwd, env}); | ||
|
||
const snapContents = await fs.readFile(snapPath); | ||
t.not(snapContents.length, 1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const test = require(process.env.TEST_AVA_IMPORT_FROM); | ||
|
||
test('a snapshot', t => { | ||
t.snapshot('foo'); | ||
}); | ||
|
||
test('a snapshot with a message', t => { | ||
t.snapshot('foo', 'a message'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Snapshot report for `test/snapshot-tests/corrupt.js` | ||
|
||
The actual snapshot is saved in `corrupt.js.snap`. | ||
|
||
Generated by [AVA](https://avajs.dev). | ||
|
||
## snapshot corruption is reported to the console | ||
|
||
> failed tests | ||
[ | ||
{ | ||
file: 'test.js', | ||
title: 'a snapshot', | ||
}, | ||
{ | ||
file: 'test.js', | ||
title: 'a snapshot with a message', | ||
}, | ||
] |
Binary file not shown.