Is there a way with Sinon to have a negative match? Specifically that an object does not have a given property?
Thanks!
Is there a way with Sinon to have a negative match? Specifically that an object does not have a given property?
Thanks!
There isn't currently a built-in matcher for that.
Sinon allows you to create custom matchers so you could create your own, here is one for doesNotHave based on the built-in has matcher:
import * as sinon from 'sinon';
const doesNotHave = (prop) => sinon.match(function (actual) {
if(typeof value === "object") {
return !(prop in actual);
}
return actual[prop] === undefined;
}, "doesNotHave");
test('properties', () => {
const obj = { foo: 'bar' };
sinon.assert.match(obj, sinon.match.has('foo')); // SUCCESS
sinon.assert.match(obj, doesNotHave('baz')); // SUCCESS
})
I've just realized that it's possible to specify undefined in the object's shape to make the check:
sinon.assert.match(actual, {
shouldNotExists: undefined
});
Not completely sure if it's 100% valid, but seems to do the job.
You cannot use sinon for this, you have to use something like chai.
You would do:
cont { expect } = require("chai");
expect({ foo: true }).to.not.have.keys(['bar']);