-
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 t.try() usage in hooks; experimentally disable t.snapshot()
`t.try()` has never worked in hooks. Fail the hook properly instead of letting it crash. `t.snapshot()` sort of works, but not really. Add an experiment so support can be disabled. This will become the default behavior in the next major AVA release. Fixes #2523. Co-authored-by: Mark Wubben <mark@novemberborn.net>
- Loading branch information
1 parent
eee2f7b
commit d01db61
Showing
10 changed files
with
89 additions
and
3 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
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
9 changes: 9 additions & 0 deletions
9
test/hook-restrictions/fixtures/invalid-snapshots-in-hooks.js
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('ava'); | ||
|
||
test.before(t => { | ||
t.snapshot({}); | ||
}); | ||
|
||
test('cannot use snapshot in hook', t => { | ||
t.pass(); | ||
}); |
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('ava'); | ||
|
||
test.before(async t => { | ||
await t.try(tt => tt.pass()); | ||
}); | ||
|
||
test('cannot use `t.try()` in hook', t => { | ||
t.pass(); | ||
}); |
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,10 @@ | ||
{ | ||
"ava": { | ||
"files": [ | ||
"*.js" | ||
], | ||
"nonSemVerExperiments": { | ||
"disableSnapshotsInHooks": true | ||
} | ||
} | ||
} |
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,17 @@ | ||
# Snapshot report for `test/hook-restrictions/test.js` | ||
|
||
The actual snapshot is saved in `test.js.snap`. | ||
|
||
Generated by [AVA](https://avajs.dev). | ||
|
||
## `t.try()` cannot be used in hooks | ||
|
||
> error message | ||
'`t.try()` can only be used in tests' | ||
|
||
## snapshots cannot be used in hooks | ||
|
||
> error message | ||
'`t.snapshot()` can only be used in tests' |
Binary file not shown.
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,14 @@ | ||
const test = require('@ava/test'); | ||
const exec = require('../helpers/exec'); | ||
|
||
test('snapshots cannot be used in hooks', async t => { | ||
const result = await t.throwsAsync(exec.fixture('invalid-snapshots-in-hooks.js')); | ||
const error = result.stats.getError(result.stats.failedHooks[0]); | ||
t.snapshot(error.message, 'error message'); | ||
}); | ||
|
||
test('`t.try()` cannot be used in hooks', async t => { | ||
const result = await t.throwsAsync(exec.fixture('invalid-t-try-in-hooks.js')); | ||
const error = result.stats.getError(result.stats.failedHooks[0]); | ||
t.snapshot(error.message, 'error message'); | ||
}); |