如何使用npm库提供异步集成测试

How can I deliver async integration testing using npm library?

本文关键字:异步 集成测试 何使用 npm      更新时间:2023-09-26

我维护一组中间件,这些中间件是生成有效GeoJSON所必需的,以便为其生成的服务供电。有一个npm模块可以基于String或对象执行linting。由于中间件本身不在节点中,我希望为中间件提供一个测试套件,该套件使用npm作为集成测试套件,确保每个端点都生成有效的GeoJSON。

我正在使用摩卡,但除此之外,我对任何产生结果的框架都持开放态度。(我试过Q、Q-io、异步阵列和其他一些阵列。)

这是一篇同步风格的文章,我的目标是:

describe('testPath()', function() {
    it("should be a function", function () {
      expect(geojsonLint.testPath).to.be.a('function')
    });
    it("should test a path", function () {
      var firstPath = geojsonLint.findEndpoints()[0];
      expect(geojsonLint.testPath(firstPath)).to.be.true
    });
});

然后,在此基础上,测试所有路径的同步版本可能看起来像这样:

describe('testAllPaths()', function() {
    it("should test a path", function () {
      geojsonLint.findEndpoints().map(function(path) {
         expect(geojsonLint.testPath(path)).to.be.true
      }
    });
});

我已经多次显著地更改了testPath的实现,但最能说明问题的尝试如下:

  testPath: function (path, callback) {
    return request('http://localhost:5000/'+path, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        callback(geojsonhint.hint(body), error, response);
      } else {
        callback(body, error, response);
      }
    });
  }

我可以确保中间件在另一个端口上本地运行,如果请求成功,我希望将结果传递给geojsonhint.int。最终,我希望验证该调用的结果是否为空。

到目前为止,我的努力是可行的,但我认为他们很差。

任何可以构建的固定点都是值得赞赏的。

通过现有的testPath实现,@rockbot能够直接使用mocha帮助我进行测试调用:

  it("should test a path", function (done) {
    var firstPath = geojsonLint.findEndpoints()[0];
    geojsonLint.testPath(firstPath, function(r, e, resp) {
      expect(r).to.be.empty;
      done();
    });
  });

更改测试本身的签名并从回调内部调用它成功了!