Description
Please see #1692 (comment) for the accepted proposal and #1692 (comment) for follow-up.
Original issue follows:
Description: add observeTest and commitToResult
Right now we have a function test
of which the essence has this type signature:
const test: (t: TestContext) => PromiseLike<void> | Iterator<any> | Observable | void
It would be helpful for library writers if there was a function:
const observeTest:
(test: (t: TestContext) => PromiseLike<void> | Iterator<any> | Observable | void) =>
{success: boolean, test_result: TestResult}
edit: fixed the type signature
For my purposes TestResult
can be a completely opaque type.
The use for it is to make this a result for the main driver. For the lack of a better name I will call it commitToResult
for now. This could either be a function exported by AVA (or a method on AVA's exported test
), or a method on TestContext
:
const commitToResult: (test_result: TestResult) => void
or
interface TestContext {
...
// stop the execution of the test now reporting this result:
commitToResult: (test_result: TestResult) => void
}
Use case: property based testing
The use case for this is property-based testing, which was first introduced in Haskell with the package QuickCheck.
There are quite a few implementations for JavaScript, for example jsverify, testcheck-js. The idea is to generate random data and test if functions satsify properties run on these data to approximate universal quantification. If the test fails it is common practice in these libraries to try to shrink the generated data. Then the user can be presented with a small (and hopefully minimal) counterexample.
The library writer of a property-based testing would then run tests written using asserts as in AVA-style, but behind the hoods it would be run with observeTest
, and if it fails start a loop trying to shrink the randomly generated input until it cannot be shrunk anymore and then report the smallest possible failing TestResult
using commitToResult
.
There is intergration for AVA in
testcheck-js/AVA-check, but it has to use the AVA internals and is thus a pain to maintain, see for example this commit in a PR by me: leebyron/testcheck-js@65ef263 So there would be clear benefits for library writers if AVA could expose these two functions.