When should you use render and shallow in Enzyme / React tests?

As per the Enzyme docs: mount(<Component />) for Full DOM rendering is ideal for use cases where you have components that may interact with DOM apis, or may require the full lifecycle in order to fully test the component (ie, componentDidMount etc.) vs. shallow(<Component />) for Shallow rendering is useful to constrain yourself to testing … 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 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

In mocha testing while calling asynchronous function how to avoid the timeout Error: timeout of 2000ms exceeded

You can either set the timeout when running your test: mocha –timeout 15000 Or you can set the timeout for each suite or each test programmatically: describe(‘…’, function(){ this.timeout(15000); it(‘…’, function(done){ this.timeout(15000); setTimeout(done, 15000); }); }); For more info see the docs.

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

tech