How can I check that two objects have the same set of property names?

You can serialize simple data to check for equality: data1 = {firstName: ‘John’, lastName: ‘Smith’}; data2 = {firstName: ‘Jane’, lastName: ‘Smith’}; JSON.stringify(data1) === JSON.stringify(data2) This will give you something like ‘{firstName:”John”,lastName:”Smith”}’ === ‘{firstName:”Jane”,lastName:”Smith”}’ As a function… function compare(a, b) { return JSON.stringify(a) === JSON.stringify(b); } compare(data1, data2); EDIT If you’re using chai like you say, … Read more

Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai

The issue is caused by this: .catch((error) => { assert.isNotOk(error,’Promise error’); done(); }); If the assertion fails, it will throw an error. This error will cause done() never to get called, because the code errored out before it. That’s what causes the timeout. The “Unhandled promise rejection” is also caused by the failed assertion, because … Read more

How do I properly test promises with mocha and chai?

The easiest thing to do would be to use the built in promises support Mocha has in recent versions: it(‘Should return the exchange rates for btc_ltc’, function() { // no done var pair=”btc_ltc”; // note the return return shapeshift.getRate(pair).then(function(data){ expect(data.pair).to.equal(pair); expect(data.rate).to.have.length(400); });// no catch, it’ll figure it out since the promise is rejected }); Or … Read more

Mocha / Chai expect.to.throw not catching thrown errors

You have to pass a function to expect. Like this: expect(model.get.bind(model, ‘z’)).to.throw(‘Property does not exist in model schema.’); expect(model.get.bind(model, ‘z’)).to.throw(new Error(‘Property does not exist in model schema.’)); The way you are doing it, you are passing to expect the result of calling model.get(‘z’). But to test whether something is thrown, you have to pass a … Read more