挂钩之前的Mocha不适用于chai-http

时间:2018-07-15 13:03:03

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

这是我的测试代码。我正在测试一个API。问题是“ after”挂钩正在工作,并且在测试结束后删除数据库。但是“之前”挂钩不起作用。这是什么问题?我尝试过但无法找出问题。我试图仅使用一个虚拟测试来运行before钩子,例如在控制台中记录一些内容。也没用。

const chai = require('chai');
const { assert } = require('chai');
const chaiHttp = require('chai-http');
const mongoose = require('mongoose');

mongoose.Promise = global.Promise;
require('../resolvedir');
const User = require('models/User');
const server = require('bin/www');
const testData = require('./test_data');

chai.use(chaiHttp);

describe('Empty User Collection before test', function () {
  it('should drop User Collection before test starts', function () {
    before(function (done) {
      User.collection.drop();
      done();
    });
  });
});


describe('Testing /registration end point', () => {
  it('should return a valid JWT token', (done) => {
    chai.request(server)
      .post('/register')
      .send({ name: testData.name, email: testData.email, password: testData.password })
      .end((err, res) => {
        assert.equal(res.status, 200, 'Http response code is 200');
        assert.exists(res.body.auth, 'Auth confirmation message exist');
        assert.isTrue(res.body.auth, 'Auth confirmation message is true');
        assert.exists(res.body.token, 'JWT token is neither null or undefined');
        assert.isString(res.body.token, 'JWT token is string');
        done();
      });
  });

  it('should fail registration', (done) => {
    chai.request(server)
      .post('/register')
      .send(testData)
      .end((err, res) => {
        assert.equal(res.status, 409, 'Http response code is 409');
        assert.isString(res.body.message);
        assert.equal(res.body.message, 'User Exist');
        done();
      });
  });
});

describe('Testing /login end point', function () {
  it('should get a valid JWT token on successful login', function (done) {
    chai.request(server)
      .post('/login')
      .send({ email: testData.email, password: testData.password })
      .end((err, res) => {
        assert.isString(res.body.token, 'JWT token is string');
        done();
      });
  });
});

describe('Empty User Collection after test', function () {
  it('should drop User Collection after test ends', function () {
    after(function (done) {
      User.collection.drop();
      done();
    });
  });
});

1 个答案:

答案 0 :(得分:0)

Mongoose和MongoDB支持承诺。 chai-http supports promises

before应该位于describe块内,而不应该位于it内。 Mocha支持异步块的承诺,不需要done。但是测试使用done且不一致。 before是异步的,但是done()被同步调用。 done()仅在测试成功时才被调用,当断言失败时,这会导致测试超时。

应该是:

describe('Empty User Collection before test', function () {
  before(function () {
    return User.collection.drop();
  });

  it('should drop User Collection before test starts', function () {
    ...
  });
});

还有

  it('should get a valid JWT token on successful login', function () {
    return chai.request(server)
      .post('/login')
      .send({ email: testData.email, password: testData.password })
      .then((res) => {
        assert.isString(res.body.token, 'JWT token is string');
      });
  });

等等

如果应在Testing /registration end point测试套件中删除数据库,则也应该before也删除数据库-或所有测试都可以将父describebefore一起使用:

describe('Suite', function () {
  before(function () {
    return User.collection.drop();
  });

  describe('Testing /registration end point', () => {...})

  describe('Testing /login end point', () => {...})
  ...
});