0

我正在学习使用 mocha 和 chai 为节点应用程序编写测试用例,我已经编写了以下测试用例

let chai = require('chai');
let chaiHttp = require('chai-http');

const should = chai.should;
const expect = chai.expect;
const server = "http:\\localhost:3000"

chai.use(chaiHttp);

describe('Create Login and Register', () => {
    it('should login using credentials', () => {
        chai.request(server)
            .get('/register')
            .send()
            .then((res: any) => {
                res.should.have.status(200);
                done();
            }).catch((err: any) => { done(err) })
    })

})

但它在 done() 下方显示了读取摆动;功能
在此处输入图像描述

我是否需要添加一些类型才能使其正常工作我缺少什么,我尝试再次安装 chai-http 但仍然是同样的问题

4

1 回答 1

0

done作为测试函数的第一个参数传入。

describe('Create Login and Register', () => {
    it('should login using credentials', (done) => { // <-- here
        chai.request(server)
            .get('/register')
            .send()
            .then((res: any) => {
                res.should.have.status(200);
                done();
            }).catch((err: any) => { done(err) })
    })
})

虽然,因为您使用的是Promise链条,所以您应该只返回链条。

describe('Create Login and Register', () => {
    it('should login using credentials', () => {
        return chai.request(server)
            .get('/register')
            .send()
            .then((res: any) => {
                res.should.have.status(200);
            }); // a rejected promise will fail the test automatically
    })
})
于 2021-06-14T12:31:45.220 回答