当返回值未定义时,chai期望传递

时间:2017-06-08 11:41:22

标签: javascript node.js chai

在我看来,这个测试不应该通过

chai test

const chai = require('chai')
const expect = chai.expect;
const chaiHttp = require('chai-http');
const request = chaiHttp.expect;
const config = require('../../app/config');
const worker = require('../../app/worker');

chai.use(chaiHttp);

describe('server response', function () {
    .....

    it('should return 200', function () {
        chai.request(`http://localhost:${config.port}`)
        .get('/')
        .end(function (err, res) {
            expect('as').to.have.status(200);
        });
    });
});

终端

> NODE_ENV=test mocha --timeout 20000 --recursive test/ --compilers js:babel-core/register



  server response
    ✓ should return 200


  1 passing (150ms)

1 个答案:

答案 0 :(得分:1)

我认为您错过了done回调。试试这个

it('should return 200', function (done) {
    chai.request(`http://localhost:${config.port}`)
    .get('/')
    .end(function (err, res) {
        expect('as').to.have.status(200);
        done();
    });
});

使用此代码,测试只会在

时通过
expect('as').to.have.status(200);

为true(对于当前示例,它将始终为false)