首页 > 解决方案 > Sails.js 测试中的超时问题

问题描述

这是我的生命周期.test.js 文件:

var sails = require('sails');

// Before running any tests...
before(function(done) {

  // Increase the Mocha timeout so that Sails has enough time to lift, even if you have a bunch of assets.
  this.timeout(5000);

  sails.lift({
    // Your Sails app's configuration files will be loaded automatically,
    // but you can also specify any other special overrides here for testing purposes.

    // For example, we might want to skip the Grunt hook,
    // and disable all logs except errors and warnings:
    hooks: { grunt: false },
    log: { level: 'warn' },

  }, function(err) {
    if (err) { return done(err); }

    // here you can load fixtures, etc.
    // (for example, you might want to create some records in the database)

    return done();
  });
});

// After all tests have finished...
after(function(done) {

  // here you can clear fixtures, etc.
  // (e.g. you might want to destroy the records you created above)

  sails.lower(done);

});

这是 FooController.test.js 文件:

var supertest = require('supertest');
var sails = require('sails');

describe('FooController.foo', function() {

  describe('#foo()', function() {
    it('should respond 200', function (done) {
      supertest(sails.hooks.http.app)
      .get('/foo')
      .expect(200)
    });
  });

});

这是运行 npm run test 时运行的命令:

"test": "node ./node_modules/mocha/bin/mocha --timeout=10000 test/lifecycle.test.js test/integration/**/*.test.js",

但我收到此错误:错误:超过 10000 毫秒的超时。对于异步测试和钩子,确保“done()”...

标签: javascriptmocha.jssails.js

解决方案


done() 很重要

  describe('#foo()', function() {
    it('should respond 200', function (done) {
      supertest(sails.hooks.http.app)
      .get('/foo')
      .expect(200, done)
    });
  });

推荐阅读