unexpected-check
property based testing plugin for unexpected
This plugin tries to bring generative testing (aka property testing) to the unexpected assertion library. The idea is that it should be possible to mix property based testing and normal unit-tests using the same tool chain.
This plugin is build to work together with chance-generators which provides a huge range of composable generators that supports shrinking.
If the generators you supply supports shrinking, then unexpected-check will try to shrink the error space as much as possible and therefore provide much more precise error cases.
Usage
Install it with NPM or add it to your package.json
:
Then register the plugin:
Let's imagine we wanted to sort arrays of numbers using this function:
Then we could write a test to ensure the following:
- the resulting array has the same size as the input array.
- the resulting array is sorted.
First we will create an assertion for checking that an array is sorted:
Then we generate the input arrays:
Finally we make the assertion:
But that assumption is actually not true as the build-in sort functions is based on converting items to strings and comparing them. So you will get the following error:
If we wanted to fix the problem, we would need to use a comparison function:
Install
Node
Install it with NPM or add it to your package.json
:
Then:
Browser
Include the unexpected-check.js
found at the lib directory of this
repository.
this will expose the expect function under the following namespace:
RequireJS
Include the library with RequireJS the following way:
Asynchronous testing
Support for asynchronous testing by returning a promise from the subject function:
Options
generators
(default []): an array of generators used to generate the example data.maxIterations
(default 300): the number of iterations that the subject function it ran when no errors occur. You can control the default for this option by setting the environment variableUNEXPECTED_CHECK_MAX_ITERATIONS
or setting the query parametermaxiterations
in the browser.maxErrorIterations
(default 1000): the number of iterations unexpected-check can use to find a better error when an error occurs.maxErrors
(default 201): the number of found errors before stopping the input shrinking process.
As you can see the input shrinking is worse with less iterations, but it will be a bit faster.
Source
The source for this plugin can be found on Github.
MIT License
Copyright (c) 2016 Sune Simonsen sune@we-knowhow.dk
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.