2017-11-12 1 views
0

자, 모카 버전 4 이후로 테스트가 실행 된 후에 노드를 중지하려면 mocha 다음에 플래그 --exit을 넣어야합니다. 그래서 지금은 내 통합 테스트를 구현하는 방식에 따라, 나는 두 가지 행동을 가지고 둘 다 내가 무엇을 찾고 있지 않습니다.노드와 약속이 거부 된 mocha 4 종료 플래그

첫 번째 방법

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); 
    }); 
}) 

테스트 실행 및 노드가 중지됩니다. 하지만 하나의 "예상"오류가 발생하면 내 테스트가 intellij에서 녹색으로 표시됩니다. (다음 블록과 캐치 내부) (완료의 노트 존재) 콘솔

오류 :

(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. 

두 번째 방법

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); 
    }); 
}) 

테스트 실행 및 노드가되지 STOP 것이다. "예상"오류가 하나있는 경우 내 테스트는 intellij (예상되는 동작)에 빨간색으로 표시됩니다.

여기에 테스트를 실행 인 IntelliJ에서 내 줄 명령입니다 :

/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 " 

내가 테스트 실행이 후 노드가 정상적으로 중지되고 싶은 IntelliJ를 날 빨간색 보여 어떤 때 오류 및 녹색 때 오류없이.

이 기능 logMessage

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

참고 : 나는 "반환"을 사용하는 경우 함수에서 제거 할이

답변

0

솔루션은

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) 
}) 

설명 내가 무슨 짓을했는지 here

:

  1. 제거는
  2. 제거 캐치
  3. 추가 한 후 블록 내부에서 수행 (수행 완료)
관련 문제