Reputation: 161
We have a function like this:
export function* postPermitApplicationRequest() {
try {
const uris = yield select(state => getUris(state));
.... (more yields hereafter)
We test this function with Jest and Chai as follows:
...
const action = { type: Action.POST_PERMIT_APPLICATION };
const generator = postPermitApplicationRequest(action);
it('must select uris from state', () => {
const nextCall = generator.next().value;
const uris = select(state => getUris(state));
expect(nextCall).to.deep.equal(uris);
});
However the expect fails:
AssertionError: expected { Object (@@redux-saga/IO, SELECT) } to deeply equal { Object (@@redux-saga/IO, SELECT) }
at Assertion.assertEqual (node_modules/chai/lib/chai/core/assertions.js:485:19)
at Assertion.ctx.(anonymous function) [as equal] (node_modules/chai/lib/chai/utils/addMethod.js:41:25)
at Object.<anonymous> (src/app/pa/PermitApplicationServiceSagas.test.js:20:43)
at process._tickCallback (internal/process/next_tick.js:109:7)
The two objects both look like:
{ '@@redux-saga/IO': true,
SELECT: { selector: [Function], args: [] } }
However the selector functions are different. The one that is the outcome of generator.next() contains code coverage skip hints:
function (state) {/* istanbul ignore next */cov_zrpq42gyn.f[12]++;cov_zrpq42gyn.s[19]++;return (/* istanbul ignore next */(0, _Selectors.getUris)(state));}
while the original function doesn't:
function (state) {return (0, _Selectors.getUris)(state);}
It looks like generator.next() adds these hints and the assertion fails
What do we wrong here?
We use redux-saga 0.14.8
Upvotes: 4
Views: 1016
Reputation: 1123
The tests fail because in your saga and your test you create a new functions every time you execute the code. These both functions will be compared but are not the same instance.
You can simply use select(getUris)
in your saga and your test because both will reference to the same function.
Your saga:
export function* postPermitApplicationRequest() {
try {
const uris = yield select(getUris);
.... (more yields hereafter)
Your test:
...
const action = { type: Action.POST_PERMIT_APPLICATION };
const generator = postPermitApplicationRequest(action);
it('must select uris from state', () => {
const nextCall = generator.next().value;
const uris = select(getUris);
expect(nextCall).to.deep.equal(uris);
});
Upvotes: 3