Node.js 使用mocha和chai在单元测试用例中自动控制返回和最后一行不执行

Node.js 使用mocha和chai在单元测试用例中自动控制返回和最后一行不执行,node.js,mocha.js,chai,Node.js,Mocha.js,Chai,我是node.js中使用mocha和chai进行单元测试的新手。 我陷入了控制自动返回而不执行的问题 chai.expect(123).to.be.a(“字符串”) 代码在这里 it.only("should fetch status",()=>{ return chai.request(server) .get("/user/status") .then((result)=>{

我是node.js中使用mocha和chai进行单元测试的新手。 我陷入了控制自动返回而不执行的问题 chai.expect(123).to.be.a(“字符串”)

代码在这里

it.only("should fetch status",()=>{
        return chai.request(server)
        .get("/user/status")
        .then((result)=>{
            let data = result.body;
            console.log("till here execute");

            //this line is not executed and test case is passed even when the below line expect to fail the test
            chai.expect(123).to.be.a("string");
            
        })
        .catch(err=>err);
    });
控制台显示上面的测试用例通过了,我不知道如何通过,为什么通过

chai.expect(123).to.be.a("string");

不执行

这与您的
捕获有关

基本上,当您的
chai.expect失败时,它将抛出一个
AssertionError

在给定的代码中,返回捕获错误,而不是抛出它

根据chai.js官方文件,在中发现,在处理承诺时,在
捕获
内必须
抛出
捕获错误

这样,变革:

.catch(err=>err);
致:

.catch(err => {throw err});