Mocha test stops without further execution of other test cases if an assert,expect or should fails in a test case.

2,726 views
Skip to first unread message

Sandeep Vedavyas

unread,
May 1, 2017, 11:39:37 PM5/1/17
to Mocha
describe('API testing: meeting', function () {
it('Testing meeting update view count call without error', function (done) {
request(app).post('/meeting/updateViewCount').send({ videoId: 17 }).set('Accept', 'application/json').expect(200).end(function (err, res) {
should.not.exist(err);
should.exist(res.body);
expect(res.body.status).to.equal('Success');
done();
});
});-- In above test case am passing invalid videoId as 17 no record exists with this id and it will fail and mocha throws below error and exit.

  throw err;

  ^

AssertionError: expected 'Error' to equal 'Success'

But I want it to continue further executing below test and also other in many files and show the result of passed and failed with proper execution. it('Testing meeting update view count call with error', function (done) {
request(app).post('/meeting/updateViewCount').send({ videoId: 's' }).set('Accept', 'application/json').expect(200).end(function (err, res) {
expect(res.body.status).to.equal('Error');
done();
});
});
});

Kindly give your suggestions, I am new to mocha tests, earlier i worked on java and grails testing frameworks where i never faced any such issues.

Thanks
Sandeep

Vlad GURDIGA

unread,
May 3, 2017, 3:01:22 PM5/3/17
to Mocha
Hey Sandeep! 👋

I’m trying to reproduce this, and I’m trying to clarify a couple of things that I see in the sample code above:
  • what is app
  • what is request
  • what is should
  • what is expect
Having that, I could try to reproduce it and see what I can see. 🤓

Cheers!

Sandeep Vedavyas

unread,
May 3, 2017, 3:12:52 PM5/3/17
to Mocha
Hi Vlad, Thank you so much for looking into the issue. Please find below information about the plugins i am using for my tests.

I have an init file, with below nodejs plugins i am initialising using --require ./test/init.js in mocha.opts which will load below plugins into global object and it will be available for all the test files

'use strict';
require('dotenv').load();
global.chai = require('chai');
global.chai.should();
global.expect = global.chai.expect;
global.assert = global.chai.assert;
global.should = global.chai.should();
global.request = require('supertest');
var _ = require('underscore');
global._ = _

var express = require('express');
var bodyParser = require('body-parser')
var app = express();
var routes = require(process.env.ROOTDIR + 'routes');
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}));
app.use('/',routes)

so app, assert, expect and should are the global variables which i am loading with above lines.

Appreciate your kind support.

Thanks
Sandeep

Vlad GURDIGA

unread,
May 6, 2017, 6:03:26 AM5/6/17
to Mocha
Hey Sandeep! 👋

I don’t have your routes, but I have tried to simulate this as close as possible, so here is your test code, with a minimal Express app that responds with {status: 'Success'} when videoId is 17, and with {status: 'Success'} otherwise.

Here is what I have, the whole picture:

    ~/tmp/sandeep ɀ  tree test/
test/
├── one.js
└── two.js

0 directories, 2 files
    ~/tmp/sandeep ɀ  cat test/one.js 
global.chai = require('chai');
global.chai.should();
global.expect = global.chai.expect;
global.assert = global.chai.assert;
global.should = global.chai.should();
global.request = require('supertest');

var express = require('express');
var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.json());

app.post('/meeting/updateViewCount', function(req, res) {
  if (req.body.videoId === 17) {
    res
      .status(200)
      .json({ status: 'Success' });
  } else {
    res
      .status(200)
      .json({ status: 'Error' });
  }
});

describe('API testing: meeting', function () {
    it('Testing meeting update view count call without error', function (done) {
        request(app)
          .post('/meeting/updateViewCount')
          .send({ videoId: 17 })
          .set('Accept', 'application/json')
          .expect(200)
          .end(function (err, res) {
              expect(err).not.to.exist;
              expect(res.body).to.exist;
              expect(res.body.status).to.equal('Success');
              done();
          });
    });

    it('Testing meeting update view count call with error', function (done) {
        request(app)
          .post('/meeting/updateViewCount')
          .send({ videoId: 's' })
          .set('Accept', 'application/json')
          .expect(200)
          .end(function (err, res) {
              expect(res.body.status).to.equal('Error');
              done();
          });
    });
});
    ~/tmp/sandeep ɀ  cat test/two.js 
describe('two', function() {
  it('runs', function() {
  });
});
    ~/tmp/sandeep ɀ  mocha


  API testing: meeting
    ✓ Testing meeting update view count call without error (85ms)
    ✓ Testing meeting update view count call with error

  two
    ✓ runs


  3 passing (133ms)

    ~/tmp/sandeep ɀ  

Now both tests pass. 👍

If I modify the assertion in the first test like this:

    ~/tmp/sandeep ɀ  git diff
diff --git a/test/one.js b/test/one.js
index 7c097fe..0676927 100644
--- a/test/one.js
+++ b/test/one.js
@@ -33,7 +33,7 @@ describe('API testing: meeting', function () {
           .end(function (err, res) {
               expect(err).not.to.exist;
               expect(res.body).to.exist;
-              expect(res.body.status).to.equal('Success');
+              expect(res.body.status).to.equal('THE TEST WILL FAIL');
               done();
           });
     });
    ~/tmp/sandeep ɀ  

…it fails the test as expected, but does not exit Mocha: it continues to run the other tests¹. 👷

    ~/tmp/sandeep ɀ  mocha


  API testing: meeting
    1) Testing meeting update view count call without error
    ✓ Testing meeting update view count call with error

  two
    ✓ runs


  2 passing (126ms)
  1 failing

  1) API testing: meeting Testing meeting update view count call without error:

      Uncaught AssertionError: expected 'Success' to equal 'THE TEST WILL FAIL'
      + expected - actual

      -Success
      +THE TEST WILL FAIL
      
      at Test.<anonymous> (test/one.js:36:42)
      at Test.assert (node_modules/supertest/lib/test.js:179:6)
      at Server.assert (node_modules/supertest/lib/test.js:131:12)
      at emitCloseNT (net.js:1575:8)
      at _combinedTickCallback (internal/process/next_tick.js:77:11)
      at process._tickCallback (internal/process/next_tick.js:104:9)



    ~/tmp/sandeep ɀ  

NOTE: ¹One thing worth mentioning is that as soon as one assertion fails the rest of the assertions in the same test are skipped, since a test failure is just a JS runtime error caught my Mocha. By assersion here I mean things like this:

              expect(err).not.to.exist;
              expect(res.body).to.exist;

So in this example, if the first expect throws an assertion error, the second one will not be run. 👀

Unless you have the app in a public repo where I can try your exact code, I don’t know where to go from here, but if you see any relevant differences between my example and your code, let’s discuss! 🤓

Sandeep Vedavyas

unread,
May 8, 2017, 6:13:41 AM5/8/17
to Mocha
Hello Vlad, 

Sorry i could not respond back earlier. Thank you so much for all the hard work in debugging the issue. I have found the issue in my code today,  it was the supertest plugin which was exiting the thread instead propagating the asserts fails to further levels. I have found some error log like below when i ran my code.

at Test.assert (/Users/svedavya/Documents/cisco_codebase/back2customer/package/repo/node_modules/supertest/lib/test.js:179:6)

    at Server.assert (/Users/svedavya/Documents/cisco_codebase/back2customer/package/repo/node_modules/supertest/lib/test.js:131:12)

    at Object.onceWrapper (events.js:293:19)

Then I investigated supertest at those specific lines and found its failing to propagate the errors. I have replaced supertest with chai-http and tested then everything worked fine. This was not mocha issue at all it was very subtle at supertest level itself. Finally issue is resolved. 

Appreciate your help. Thank you so much for all the support. 

Sandeep Vedavyas

Vlad GURDIGA

unread,
May 8, 2017, 11:48:54 AM5/8/17
to Mocha
Hey Sandeep! 👋

I’m very glad you’ve figured it out! 🤓

Good luck!
Reply all
Reply to author
Forward
0 new messages