Reputation: 697
With Chai expect, I am trying to compare API response with expected response. The expected response of API changes with time and can be one of two possible static JSONs.
Is there any way in Chai, to expect one of these two JSONs?
I know that it works perfectly fine with single value: expect(actualResponse).to.deep.equal(expectedJson);
But I want something like: expect(actualResponse).to.deep.equal(expectedJson1, expectedJson2);
Upvotes: 0
Views: 975
Reputation: 102247
You could use .satisfy(matcher[, msg]) method of chai
.
E.g.
import { expect } from 'chai';
import _ from 'lodash';
describe('63811326', () => {
it('should pass', () => {
function genResponse() {
const successResponse = {
data: {},
success: true,
};
const failResponse = {
success: false,
errorMessage: 'API error',
};
return Math.random() > 0.5 ? failResponse : successResponse;
}
const expectedJson1 = { data: {}, success: true };
const expectedJson2 = { success: false, errorMessage: 'API error' };
expect(genResponse()).to.satisfy((actualResponse) => {
console.log(actualResponse);
return _.isEqual(actualResponse, expectedJson1) || _.isEqual(actualResponse, expectedJson2);
});
});
});
unit test result for the first execution:
63811326
{ success: false, errorMessage: 'API error' }
✓ should pass
1 passing (26ms)
unit test result for the second execution:
63811326
{ data: {}, success: true }
✓ should pass
1 passing (31ms)
Upvotes: 3