How can I test floating-point equality using chai?

I was looking for this too, and apart from this question, I also found this discussion regarding a feature request that led to the addition of closeTo. With that you can specify a value and a +/- delta, so basically it lets you specify the precision with which to check the result. percentage.should.be.closeTo(6.666, 0.001); or … Read more

Trying ES6 style import gives ‘Cannot use import statement outside a module’

The easiest way to run Mocha tests written in ES6 is compiling them on-the-fly using Mocha –require @babel/register option (see https://github.com/mochajs/mocha/wiki/compilers-deprecation#what-should-i-use-instead-then). Of course, you need to make sure to install the corresponding modules and set up the .babelrc accordingly package.json: “dependencies”: { “@babel/cli”: “^7.7.4”, “@babel/core”: “^7.7.4”, “@babel/preset-env”: “^7.7.4”, “@babel/register”: “^7.7.4”, … } .babelrc: { “presets”: … Read more

What is the difference between equal and eql in Chai Library

Strictly equal (or ===) means that your are comparing exactly the same object to itself: var myObj = { testProperty: ‘testValue’ }; var anotherReference = myObj; expect(myObj).to.equal(anotherReference); // The same object, only referenced by another variable expect(myObj).to.not.equal({testProperty: ‘testValue’}); // Even though it has the same property and value, it is not exactly the same object … Read more

How to do an “or” in chai should

Asserts that the target is a member of the given array list. However, it’s often best to assert that the target is equal to its expected value. expect(1).to.be.oneOf([1, 2, 3]); expect(1).to.not.be.oneOf([2, 3, 4]); https://www.chaijs.com/api/bdd/#method_oneof

Reset “called” Count on Sinon Spy

This question was asked a while back but may still be interesting, especially for people who are new to sinon. this.spied.reset() is not needed as Obj.prototype.spiedMethod.restore(); removes the spy. Update 2018-03-22: As pointed out in some of the comments below my answer, stub.reset will do two things: Remove the stub behaviour Remove the stub history … Read more

Test a rejection with Chai as promised

If you’re using Chai-As-Promised (as you say you are), then it allows for chaining off of rejectedWith – and it sets the chain assertion object to be the error object – meaning anything after rejectedWith() is now going to assert on the Error. This lets you do cool things like: return expect(foo()).to.eventually .be.rejectedWith(“my error message”) … Read more

Verify that an exception is thrown using Mocha / Chai and async/await

The problem with this approach is that (await fails()).should.throw(Error) doesn’t make sense. await resolves a Promise. If the Promise rejects, it throws the rejected value. So (await fails()).should.throw(Error) can never work: if fails() rejects, an error is thrown, and .should.throw(Error) is never executed. The most idiomatic option you have is to use Chai’s rejectedWith property, … 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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)