Assertion library for unit testing JS generators. Works well with redux-saga. Allows snapshot testing.
As in “expect generator…”. An assertion / snapshot library for testing iterators and generators. It was designed for, and works particularly well with, redux-saga but can be used for anything that uses generators.
function* myEffect(fakeAction) {
const fooIds = yield select(fooIds);
yield put(foosLoading());
const results = yield call(apiFetch, options);
}
import expectGen from 'expect-gen';
it('runs effect with fakeFooIds and fakeResults', () => {
expectGen(myEffect, fakeAction)
// asserts step yields first `select(fooIds)`
.yields(
select(fooIds),
// `fakeFooIds` gets pass into the following `next`
fakeFooIds
)
.yields(
put(foosLoading())
)
.yields(
call(apiFetch, options),
fakeResults
)
.finishes()
.run();
});
it('runs effect with fakeFooIds and fakeResults', () => {
const snapshot = expectGen(myEffect, fakeAction)
// Doesn't run assertion for `next`
.next(fakeFooIds)
.next()
.yields(
call(apiFetch, options),
fakeResults
)
.finishes()
.toJSON();
expect(snapshot).toMatchSnapshot();
});
expectGen(generator, [...args])
StepManager
expectGen
returns an instance StepManager
. It gives us a set of chainable methods:
expectGen(generator, [...args])
.yields(expectedValue, [result])
expectedValue
result
into generator’s next
methodStepManager
expectGen(generator, [...args])
.next([result])
result
into generator’s next
methodStepManager
expectGen(generator, [...args])
.throws(error)
error
into generatorStepManager
expectGen(generator, [...args])
.catches(error, [expectedValue])
error
into generatorexpectedValue
StepManager
expectGen(generator, [...args])
.catchesAndFinishes(error, [result])
error
into generatorresult
StepManager
expectGen(generator, [...args])
...
.finishes([result])
result
StepManager
expectGen(generator, [...args])
...
.run()
expectGen(generator, [...args])
...
.toJSON()
run()
into a JSON objectExpect Gen is heading towards v1. The following items still need to go in before then. Please open issues for other v1 requirements you find.
Expect Gen is a second generation version of generator-test-runner. Many of the concepts used here originated there.