0
votes

Well, since the version 4 of mocha, you have to put the flag --exit after mocha, if you want your node to stop after your tests runs. So now depending on the way i implement my integration test, i have two behavior and both are not what i m looking for.

First way

it('User exists?', function(done){
  console.log(index++ +' - Verify if user already exists');

  chai.request(app)
      .get('/api/users/exists?value='+createdUser.userName)
      .set('Cookie', createdCookie)
      .then(function(res){
        if(trace)console.log(res.body);
          expect(res).to.be.status(200);
          expect(res.body.isValid).to.be.true;
          done();
      }).catch(function(error){
          logMessage(error,done);
  });
})

Test will run and node will stop. But if i have one "expect" error, my test will be green in intellij. (note presence of done(), inside block then and catch)

errors in console :

(node:30824) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError: expected false to be true
(node:30824) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Second way

it('User exists?', function(){
  console.log(index++ +' - Verify if user already exists');

  return chai.request(app)
      .get('/api/users/exists?value='+createdUser.userName)
      .set('Cookie', createdCookie)
      .then(function(res){
        if(trace)console.log(res.body);
          expect(res).to.be.status(200);
          expect(res.body.isValid).to.be.true;
      }).catch(function(error){
          logMessage(error);
  });
})

Test will run and node will not STOP. If i have one "expect" error, my test will be red in intellij (expected behavior).

Here is my line command from intellij to run test :

/opt/node-v8.7.0-linux-x64/bin/node /home/bryan/stack/stack_server/node_modules/mocha/bin/_mocha --exit --ui bdd --reporter /home/bryan/.IntelliJIdea2017.2/config/plugins/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js /home/bryan/stack/stack_server/test/api/user/user.controller.js --grep "User controller tests "

What i want is node normally stop after tests are runs and intellij show me red when error and green when no error.

Function logMessage

function logMessage(error,done){
    console.log('Promise rejected');
    console.log(error.message);
    if(trace) console.log(error);
    done();
    throw error;
}

note : done is removed from function if i use "return"

1

1 Answers

0
votes

Solution is

it('User exists?', function(done){
  console.log(index++ +' - Verify if user already exists');

  chai.request(app)
      .get('/api/users/exists?value='+createdUser.userName)
      .set('Cookie', createdCookie)
      .then(function(result){
        if(trace)console.log(result.body);
          expect(result).to.be.status(200);
          expect(result.body.isValid).to.be.true;
      }).then(done,done)
})

Explanations here

what i did :

  1. remove done inside block
  2. remove catch
  3. add then(done,done)