jest-given
Consistently readable test descriptions for Jest
FYI
This little test description helper was inspired by Eric Elliot's RITEway and his article 5 Questions Every Unit Test Must Answer
Installation
$ npm install --save-dev jest-given # yarn add -D jest-given
Usage
const given as = ; const binaryStringToNumber = { if !/^[01]+$/ throw 'Not a binary number.'; return ;}; ;
Results:
PASS binaryStringToNumber ✓ given a valid binary string: returns the correct number given an invalid binary string: ✓ composed of non-numbers: throws CustomError ✓ with extra whitespace: throws CustomError
API
given(what [, nest])
Starts a chain of descriptions that finally result in executing a test.
-
what
Required
:String
describes what is given to the test function
-
nest
Optional
:Function
allows nesting tests under a Jestdescribe()
usingwhat
If nest
IS NOT provided: Returns chained function .it()
(and an alias: .should()
)
-
####
.it(what, fn, timeout)
Has the same signature as Jest's
it()
.-
what
Required
:String
describes the results of the test function
-
-
####
.it.skip(...)
or.xit(...)
or.xshould(...)
When you are maintaining a large codebase, you may sometimes find a test that is temporarily broken for some reason. If you want to skip running this test, but you don't want to just delete this code, you can use
.it.skip
to specify some tests to skip.
If nest
IS provided, using as()
for describing nested tests provides
the same API as given()
however, it's description is simplified for clarity.
- ###
as(what [, nest])
setupTestFrameworkScriptFile
Jest configuration
Optional Setup: using Add jest-given
to your Jest setupTestFrameworkScriptFile
configuration. See for help
"jest":
If you are already using another test framework, like jest-extended, then you should create a test setup file and require
each of the frameworks you are using (including jest-given
😉)
For example:
// ./testSetup.js;;
Then in your Jest config:
"jest":
For linting you'll need to define these globals in your test files:
/* globals given, as */
License
ISC © Buster Collings