【问题标题】:How do I launch server for multiple mocha chai-http test files?如何为多个 mocha chai-http 测试文件启动服务器?
【发布时间】:2015-03-16 19:05:19
【问题描述】:

我在 mocha chai-http 测试的 before 块中启动我的节点服务器。

我让它非常适合单个测试文件。但是,当我尝试在单个命令 NODE_ENV=test mocha test/**/*.js 中运行多个测试时,出现错误。

我尝试让节点服务器在每个测试文件的不同端口上启动。这不起作用,出现节点服务器启动错误。

我现在在想,如果我可以有一个 mocha 文件在我的其他测试文件之前运行来启动服务器,然后在其他测试文件之后运行一个文件来终止/停止服务器,那就太好了。

我会怎么做。

下面是我的一些代码:

这是我的一个测试文件供参考:

var chai = require('chai');
var chaiHttp = require('chai-http');
chai.use(chaiHttp);
var expect = chai.expect;
var Sails = require('sails');

describe('REST User API', function() {

  var app;    // for access to the http app
  var sails;  // for starting and stopping the sails server

  before(function (done) {
    Sails.lift({
      port: 3001,
      log: {
        level: 'error'
      }
    }, function (_err, _sails) {
      if(_err){
        console.log("Error!", _err);
        done();
      }
      else {
        app = _sails.hooks.http.app;
        sails = _sails;
        done();
      }
    });
  });

  describe("user session", function () {
    var res;  // http response
    var authenticatedUser;

    before(function (done) {
      chai.request(app)
        .post('/users/signin')
        .set('Accept', 'application/json')
        .set('Content-Type', 'application/json')
        .send({ email: 'admin@test.com', password: 'secret'})
        .end(function (_res) {
          res = _res; // Record the response for the tests.
          authenticatedUser = JSON.parse(_res.text); // Save the response user for authenticated tests
          done();
        });
    });

    it("should connect with a 200 status", function () {
      expect(res).to.have.status(200);
    });

    it("should have a complete user session", function () {
      var userSession = authenticatedUser;
      expect(userSession).to.have.property('firstName');
      expect(userSession).to.have.property('lastName');
      expect(userSession).to.have.property('gender');
      expect(userSession).to.have.property('locale');
      expect(userSession).to.have.property('timezone');
      expect(userSession).to.have.property('picture');
      expect(userSession).to.have.property('phone');
      expect(userSession).to.have.property('email');
      expect(userSession).to.have.property('username');
      expect(userSession).to.have.property('confirmed');
      expect(userSession).to.have.property('status');
      expect(userSession).to.have.property('authToken');
    });

  });

  after(function (done) {
    sails.lower(function() {
      done()
    });
  });

});

【问题讨论】:

  • 那么你在哪里得到这个错误?
  • 我收到端口冲突错误。奇怪的是,我只有两个测试,它们在不同端口上的 before 方法中启动服务器。 error: Trying to start server on port 3007 but can't...Something else is probably running on that port!Please disable the other server, or choose a different port and try again.

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


【解决方案1】:

从 mocha v8.2.0,您可以使用 GLOBAL FIXTURES 为所有测试套件设置和拆除您的 Web 服务器。全局固定装置保证执行一次且仅执行一次。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 2022-06-20
    • 2019-01-18
    • 2021-12-15
    • 2021-11-22
    相关资源
    最近更新 更多