2017-10-20 7 views
1

약속 테스트를 실행하려고했지만 테스트에서 제한 시간을 초과하여 테스트가 실패하고 작업 항목이 있는지 확인하도록 제안합니다.모카, nodejs 약속 테스트가 완료되지 않았기 때문에 완료 할 수 없습니다.

이 내 테스트 코드의 일부입니다

$configurations 
    .updateConfiguration(configurations_driver.NOT_VALID_MODEL) //invalid model 
    .then(function() { 
     done(new Error("Expected INVALID_MODEL error but got OK")); 
    }, function (error) { 
     chai.assert.isNotNull(error); 
     chai.expect(error.message).to.be.eq("INVALID_MODEL_ERROR"); 
     chai.expect(error.kind).to.be.eq("ERROR_KIND"); 
     chai.expect(error.path).to.be.eq("ERROR_PATH"); 
     done(); 
    }) 
    .catch(done); 
}); 

난 당신이 볼 수있는이 모든 일 조항, 그래서 시험 또는 구조에 뭔가를 누락 나도 몰라 틀렸어.

답변

4

모카는 약속이 return 인 한 done을 사용하지 않고 테스트 약속을 지원합니다.

const expect = chai.expect 

it('should error', function(){ 
    return $configurations 
    .updateConfiguration(configurations_driver.NOT_VALID_MODEL) //invalid model 
    .then(()=> { throw new Error("Expected INVALID_MODEL error but got OK")}) 
    .catch(error => { 
     expect(error).to.not.be.null; 
     expect(error.message).to.equal("INVALID_MODEL_ERROR"); 
     expect(error.kind).to.equal("ERROR_KIND"); 
     expect(error.path).to.equal("ERROR_PATH"); 
    }) 
}) 

또한 더 표준 차이 주장/기대 같은 약속 테스트를 만들기 위해 chai-as-promised 봐.

chai.should() 
chai.use(require('chai-as-promised')) 

it('should error', function(){ 
    return $configurations 
    .updateConfiguration(configurations_driver.NOT_VALID_MODEL) 
    .should.be.rejectedWith(/INVALID_MODEL_ERROR/) 
}) 
노드 7.6+ 환경에

또는 당신은 또한 async/await 약속 처리기의 사용을 babel/babel-register 할 수있는 곳

it('should error', async function(){ 
    try { 
    await $configurations.updateConfiguration(configurations_driver.NOT_VALID_MODEL) 
    throw new Error("Expected INVALID_MODEL error but got OK")}) 
    } catch (error) { 
    expect(error).to.not.be.null; 
    expect(error.message).to.equal("INVALID_MODEL_ERROR"); 
    expect(error.kind).to.equal("ERROR_KIND"); 
    expect(error.path).to.equal("ERROR_PATH"); 
    } 
})