I want to write unit testing using Jest for my Node.js functions and need to test if response object has some specific keys or not. Something like this:
expect(await mokChannelsService.getChannel(inputDto)).toEqualKeys(outputDto);
// Or just a normal function
const result = await mokChannelsService.getChannel(inputDto);
const equal = isEqualKeys(result, outputDto);
This toEqualKeys
function should check equality just for keys not values. And these objects are not instances of a class. for example these two objects should be equal:
const object1 = {
names: {
firstName: '',
lastName: '',
},
phone: 1,
};
const object2 = {
names: {
firstName: 'John',
lastName: 'Due',
},
phone: 1234567890,
};
const object3 = {
names: {
firstName: 'John',
lastName: 12,
},
mobile: '12345',
};
const result1 = toEqualKeys(object1, object2); // true
const result1 = toEqualKeys(object1, object3); // false
Is there any NPM packages available for this?