【问题标题】:Not able to find Done in mocha test cases using Chai-http使用 Chai-http 在 mocha 测试用例中找不到 Done
【发布时间】:2021-06-14 12:28:03
【问题描述】:

我正在学习使用 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 但仍然是同样的问题

【问题讨论】:

  • 那是因为您没有使用来自it 回调的done 参数...将其更改为类似于it('..', done => { /* your stuff */ done() });

标签: node.js mocha.js chai chai-http


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-11-23
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 2020-10-11
    相关资源
    最近更新 更多