gen-tester
Test generators with ease
Why?
Testing generators is kind of a pain to do manually. Because of the way generators
work, the order in which mock values are injected into a generator relates
to the previous yield
.
How?
const call = ; // side-effects as data libraryconst test = ;const genTester yields = ; { const resp = ; const data = ; return ...data extra: 'stuff' ;} ;
This is what the test would look like using a manual approach
;
API
genTester
genTester
accepts a generator function and arguments to pass to generator and
returns a function that accepts an array of yields, described below:
generator
(generator function), the generator function to testargs
(array, default: []), a list of arguments being called withgenerator
const tester = ;
tester
which is the return value of genTester
accepts an array of yields
and returns a list of results from the generator at each step
yields
(array, default: []), a list ofyield
s that the generator will call with the value that will be the result of the yield as well as what was expected of that yield.
const genTester yields = ; const tester = ;const results = ;console;/*{ actual: ['each', 'yield', 'and return'], expected: ['each', 'yield', 'and return'],}*/
yields
yields
is a helper function that will allow the user to send the expected results
of a yield as well as the return value of that yield. This is primarily used
to inject values into yields for mocking purposes.
expected
(any), what we expect the yield to yieldreturns
(any), what we want the yield to yield for mocking
skip
skip
is a helper function that will allow the user to skip a yield. The generator
will progress to the next steps as normal, but we will not keep track of the results
or expectations of that yield.
returns
(any), what we want the yield to yield for mocking
const skip = ; { 1; const resp = ; if respstatus !== 200 return; const val = ; return val;} const results = ;
throws
throws
allows the developer to throw an exception inside a generator.
returns
(any)
const assert = ;const genTester throws skip = ; { let value = 1; try 1; catch err value = 2; err + ' handled'; return value;} const tester = ;const actual expected = ;console; assert;
throws
can also be used when something throws an exception between yields.
When asserting that an exception is raised, you must pass it a function which
will receive the error as an argument.
const assert = ;const genTester yields throws = ; { 1; throw 'Something happened';} const tester = ;const actual expected = ;console; assert;
finishes
finishes
ensures that the last step is marked as done
by the generator.
const genTester finishes = ; ;
evaluateSteps
evaluateSteps
is a helper that takes the results of genTester
and determines
what steps are no equal and displays more useful information.
const genTester = ;const deepEqual = ; { 1; 2; return 3;} const tester = ;// message gets called when a step is not equalconst message = { return `error on step : actual: expected: `;};const actual expected = ;const results = ;console;/*{ message: [Function: message], pass: false, actual: 2, expected: 4,}*/
stepsToBeEqual
stepsToBeEqual
is a jest matcher that uses evaluateSteps
const genTester stepsToBeEqual = ; expect; { 1; 2; return 3;} const tester = ;const results = ;;