chai

What is the difference between assert, expect and should in Chai?

此生再无相见时 提交于 2019-11-28 15:26:25
What is the difference between assert , expect and should , and when to use what? assert.equal(3, '3', '== coerces values to strings'); var foo = 'bar'; expect(foo).to.equal('bar'); foo.should.equal('bar'); Louis The differences are documented there . The three interfaces present different styles of performing assertions. Ultimately, they perform the same task. Some users prefer one style over the other. This being said, there are also a couple technical considerations worth highlighting: The assert and expect interfaces do not modify Object.prototype , whereas should does. So they are a

Make mocha tests show the actual error

这一生的挚爱 提交于 2019-11-28 14:29:56
One of the things that I find frustrating about Mocha is that when tests fail, they don't give the actual error message of the failing line, Instead, they just end with Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test. Take this test for example: describe("myTest", function() { it("should return valid JSON.", function(done) { api.myCall("valid value").then(function(result) { console.log(result); var resultObj = JSON.parse(result); assert.isFalse(resultObj.hasOwnProperty("error"), "result has an error"); done(); }); }); }); The output is: myTest {"error

How to require same file in Mocha test

蹲街弑〆低调 提交于 2019-11-28 11:07:32
问题 I have config/index.js which returns a different config file based on the NODE_ENV environment variable that is set. I'm trying to write a simple test to ensure that the right config is returned for each environment, but I'm running into an issue where only the first require is actually be called and subsequent requires of the same file are using the value from the first require. How should I change my test to resolve this issue? describe('config', function () { it('should return dev config',

Nice way to get rid of no-unused-expressions linter error with chai

隐身守侯 提交于 2019-11-28 10:39:47
In my Chai tests I often find myself wanting to use their assertions that are something like .to.be.empty , .to.be.true e.t.c., because I find them to be cleaner to read than .to.be.length(1) or .to.be.equal(true) . However, this breaks my linter (I'm using default Airbnb linting). I could use the // disable-eslint-line syntax, but then I'd have to add it to every single line that reads like that and that seems tedious. I've also read about the DirtyChai library, but that would require me to go back through my entire testing library adding brackets to them all which seems like something I

Testing JS exceptions with Mocha/Chai [duplicate]

风流意气都作罢 提交于 2019-11-27 17:14:20
问题 This question already has answers here : Mocha / Chai expect.to.throw not catching thrown errors (6 answers) Closed 2 years ago . Trying to test some code that throws an exception with Mocha/Chai, but having no luck, here's the simple code I'm trying to test: class window.VisualizationsManager test: -> throw(new Error 'Oh no') Here is my test: describe 'VisualizationsManager', -> it 'does not permit the construction of new instances', -> manager = new window.VisualizationsManager chai.expect

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

梦想的初衷 提交于 2019-11-27 11:26:05
I am using node, mocha, and chai for my application. I want to test that my returned results data property is the same "type of object" as one of my model objects. (Very similiar to chai's instanceof). I just want to confirm that the two objects have the same sets of property names. I am specifically not interested in the actual values of the properties. Lets say I have the model Person like below. I want to check that my results.data has all the same properties as the expected model does. So in this case, Person which has a firstName and lastName. So if results.data.lastName and results.data

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

时光总嘲笑我的痴心妄想 提交于 2019-11-27 10:27:02
prior to posting this question, I tried to search in sqa stackexchange but I found no post about shallow and render there, so I hope someone can help me out here. When should I use shallow and render in testing react components? Based on the airbnb docs, I've made some opinions on the difference of the two: Since shallow is testing components as a unit , so it should be used for 'parent' components. (ex. Tables, Wrappers, etc.) Render is for child components. The reason I asked this question, is that I'm having a hard time to figure out which one I should use (though the docs say that they're

Make mocha tests show the actual error

穿精又带淫゛_ 提交于 2019-11-27 08:24:29
问题 One of the things that I find frustrating about Mocha is that when tests fail, they don't give the actual error message of the failing line, Instead, they just end with Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test. Take this test for example: describe("myTest", function() { it("should return valid JSON.", function(done) { api.myCall("valid value").then(function(result) { console.log(result); var resultObj = JSON.parse(result); assert.isFalse

Nice way to get rid of no-unused-expressions linter error with chai

这一生的挚爱 提交于 2019-11-27 03:02:22
问题 In my Chai tests I often find myself wanting to use their assertions that are something like .to.be.empty , .to.be.true e.t.c., because I find them to be cleaner to read than .to.be.length(1) or .to.be.equal(true) . However, this breaks my linter (I'm using default Airbnb linting). I could use the // disable-eslint-line syntax, but then I'd have to add it to every single line that reads like that and that seems tedious. I've also read about the DirtyChai library, but that would require me to

Sinon stub being skipped as node express middleware

五迷三道 提交于 2019-11-26 21:51:42
问题 I'm trying to test the behavior of a particular route. It continues to run the middleware even when I create a stub. I want the event authentication to simply pass for now. I understand that it's not truly a "unit" test at this point. I'm getting there. I've also simplified the code a little. Here is the code to test: const { rejectUnauthenticated } = require('../modules/event-authentication.middleware'); router.get('/event', rejectUnauthenticated, (req, res) => { res.sendStatus(200); });