Easy test setup without side effects.
- testing different methods on the same object in multiple tests can result in cross-contamination.
- tests can depend on order; and break when reordered.
- Running only some tests in a single file (like via jest’s
.only) can make tests behave unpredictably.
Givens, when used correctly solves all of these, dries up your tests, and might even make them more readable, while still letting you use
What does it do?
when you call
getGiven(), givens calls
afterEach with a callback that clears the cache. Because this
afterEach hook is declared first, it will run after every other
afterEach in every scope.
when you call
given(key, callback), givens calls the following lifecycle functions:
beforeAll, with a hook which pushes
callbackonto a stack of functions for
keyand makes sure there is an accessor present for key, which simply calls the topmost function on the stack and returns the result.
afterAll, with a hook which pops
callbackback off the stack of functions for
key, and deletes the accessor if the stack is now empty.
first, install from npm:
npm install --save-dev givens
yarn add --dev givens
or add to testing framework config, for example in jest:
setupFilesAfterEnv: // this CANNOT be setupFiles'givens/setup.js'
then you can use the
given keyword without importing.
in the test file, use the following import:
;const given = ;
or in typescript
When you call
given(myKey, callback), givens stores the callback function you give it. When you go to retrieve the key (via
given.myKey) givens will execute the most recent callback you have given for the key, and cache the value. Additionally, if you call
given() inside a describe, the callback will revert to the previous one given for
myKey after executing all tests in the describe, allowing you to override a given value for a set of tests. The cache is cleared after every test.
For more examples of usage for your particular setup, read through the integration tests. They are examples of how to use the library as intended (and in some cases how it's not supposed to be used).