Testing asynchronous function with mocha

You have to specify the callback done as the argument to the function which is provided to mocha – in this case the it() function. Like so: describe(‘api’, function() { it(‘should load a user’, function(done) { // added “done” as parameter assert.doesNotThrow(function() { doRequest(options, function(res) { assert.equal(res, ‘{Object … }’); // will not fail assert.doesNotThrow … Read more

Enzyme simulate an onChange event

You can simply spy to the method directly via the prototype. it(“responds to name change”, done => { const handleChangeSpy = sinon.spy(New.prototype, “handleChange”); const event = {target: {name: “pollName”, value: “spam”}}; const wrap = mount( <New /> ); wrap.ref(‘pollName’).simulate(‘change’, event); expect(handleChangeSpy.calledOnce).to.equal(true); }) Alternatively, you can use spy on the instance’s method, but you have to … Read more

Running Mocha 6 ES6 tests with Babel 7, how to set up?

Testing in ES6 with Mocha and Babel 7. Look here: https://dev.to/bnorbertjs/my-nodejs-setup-mocha–chai-babel7-es6-43ei or http://jamesknelson.com/testing-in-es6-with-mocha-and-babel-6/ npm install –save @babel/runtime npm install –save-dev @babel/plugin-transform-runtime And, in .babelrc, add: { “presets”: [“@babel/preset-env”], “plugins”: [ [“@babel/transform-runtime”] ] }

How to authenticate Supertest requests with Passport?

As zeMirco points out, the underlying superagent module supports sessions, automatically maintaining cookies for you. However, it is possible to use the superagent.agent() functionality from supertest, through an undocumented feature. Simply use require(‘supertest’).agent(‘url’) instead of require(‘supertest’)(‘url’): var request = require(‘supertest’); var server = request.agent(‘http://localhost:3000’); describe(‘GET /api/getDir’, function(){ it(‘login’, loginUser()); it(‘uri that requires user to be … Read more