chai

How to test promises with Mocha

北城余情 提交于 2019-11-26 20:40:38
问题 I'm using Mocha to test an asynchronous function that returns a promise. What's the best way to test that the promise resolves to the correct value? 回答1: Mocha has built-in Promise support as of version 1.18.0 (March 2014). You can return a promise from a test case, and Mocha will wait for it: it('does something asynchronous', function() { // note: no `done` argument return getSomePromise().then(function(value) { expect(value).to.equal('foo'); }); }); Don't forget the return keyword on the

How to test react-router with enzyme

前提是你 提交于 2019-11-26 20:34:46
问题 I am using enzyme+mocha+chai to test my react-redux project. Enzyme provides shallow to test component behavior. But I didn't find a way to test the router. I am using react-router as below: <Router history={browserHistory}> ... <Route path="nurse/authorization" component{NurseAuthorization}/> ... </Route> I want to test this route nurse/authorization refer to NurseAuthorization component. How to test it in reactjs project? EDIT1 I am using react-router as the router framework. 回答1: You can

chai test array equality doesn't work as expected

旧巷老猫 提交于 2019-11-26 19:54:25
Why does the following fail? expect([0,0]).to.equal([0,0]); and what is the right way to test that? For expect , .equal will compare objects rather than their data, and in your case it is two different arrays. Use .eql in order to deeply compare values. Check out this link . Or you could use .deep.equal in order to simulate same as .eql . Or in your case you might want to check .members . For asserts you can use .deepEqual , link . Try to use deep Equal. It will compare nested arrays as well as nested Json. expect({ foo: 'bar' }).to.deep.equal({ foo: 'bar' }); Please refer to main

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

杀马特。学长 韩版系。学妹 提交于 2019-11-26 19:24:12
In my node application I'm using mocha to test my code. While calling many asynchronous functions using mocha, I'm getting timeout error ( Error: timeout of 2000ms exceeded. ). How can I resolve this? var module = require('../lib/myModule'); var should = require('chai').should(); describe('Testing Module', function() { it('Save Data', function(done) { this.timeout(15000); var data = { a: 'aa', b: 'bb' }; module.save(data, function(err, res) { should.not.exist(err); done(); }); }); it('Get Data By Id', function(done) { var id = "28ca9"; module.get(id, function(err, res) { console.log(res);

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

泪湿孤枕 提交于 2019-11-26 11:52:30
问题 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

How do I properly test promises with mocha and chai?

只愿长相守 提交于 2019-11-26 10:17:46
The following test is behaving oddly: it('Should return the exchange rates for btc_ltc', function(done) { var pair = 'btc_ltc'; shapeshift.getRate(pair) .then(function(data){ expect(data.pair).to.equal(pair); expect(data.rate).to.have.length(400); done(); }) .catch(function(err){ //this should really be `.catch` for a failed request, but //instead it looks like chai is picking this up when a test fails done(err); }) }); How should I properly handle a rejected promise (and test it)? How should I properly handle a failed test (ie: expect(data.rate).to.have.length(400); ? Here is the

NodeJS UnhandledPromiseRejectionWarning

只愿长相守 提交于 2019-11-26 08:01:45
问题 So, I\'m testing a component that relies on an event-emitter. To do so I came up with a solution using Promises with Mocha+Chai: it(\'should transition with the correct event\', (done) => { const cFSM = new CharacterFSM({}, emitter, transitions); let timeout = null; let resolved = false; new Promise((resolve, reject) => { emitter.once(\'action\', resolve); emitter.emit(\'done\', {}); timeout = setTimeout(() => { if (!resolved) { reject(\'Timedout!\'); } clearTimeout(timeout); }, 100); }).then

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

喜欢而已 提交于 2019-11-26 06:56:22
问题 In my node application I\'m using mocha to test my code. While calling many asynchronous functions using mocha, I\'m getting timeout error ( Error: timeout of 2000ms exceeded. ). How can I resolve this? var module = require(\'../lib/myModule\'); var should = require(\'chai\').should(); describe(\'Testing Module\', function() { it(\'Save Data\', function(done) { this.timeout(15000); var data = { a: \'aa\', b: \'bb\' }; module.save(data, function(err, res) { should.not.exist(err); done(); }); }

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

梦想与她 提交于 2019-11-25 23:21:26
问题 I\'m having issues getting Chai\'s expect.to.throw to work in a test for my node.js app. The test keeps failing on the thrown error, but If I wrap the test case in try and catch and assert on the caught error, it works. Does expect.to.throw not work like I think it should or something? it(\'should throw an error if you try to get an undefined property\', function (done) { var params = { a: \'test\', b: \'test\', c: \'test\' }; var model = new TestModel(MOCK_REQUEST, params); // neither of