【问题标题】:Run or Debug integration test with nodeJs using typescript, Mocha, Chai and SuperTest for async/await node api-functions使用 typescript、Mocha、Chai 和 SuperTest 为 async/await 节点 api 函数运行或调试 nodeJs 集成测试
【发布时间】:2018-02-12 14:50:29
【问题描述】:

我正在尝试运行(使用命令npm run test 并调试我已使用IDE Webstorm)使用用 typescript、mocha、chai 和 supertest 编写的 node.js 开发的集成测试,用于使用 typescript 开发的节点应用程序。

在 before() 钩子函数中,我们正在调用实际启动服务的应用程序,此调用用于异步(使用 async-await)函数(来自节点应用程序的 app.ts/app.js 文件) .

但我总是收到类似“错误:您无权访问 Google KMS 中的密钥”(即服务中)之类的错误,并且显示“错误” : 超过 60000 毫秒的超时。对于异步测试和钩子,确保调用了“done()”;如果返回 Promise,请确保它解决。',但如果我单独运行服务/应用程序,它工作正常。这意味着在运行服务时,API/函数调用的异步/等待代码是相同的。

所以我的意思是,这是由于在从 before() 钩子函数启动服务时异步/等待请求超时而发生的。

下面是 test.int-test.ts 文件中的代码示例,

    import {expect} from "chai";
    const app = require('../app');
    const supertest = require('supertest');

    describe("Test model", function () {

    this.timeout(60000);

    before(async () => {
      api = supertest(await app); 
      // app is here a entry point for my service/application which runs actually service/applicaiton. 
      // This file has async-await function which makes call to third party application
      console.log('inside before: ', JSON.stringify(api));
    });

    describe('get', function () {
      it('should respond with 200 Success', async () => {
        // call to async function
       });
    });
});

在 package.json 中的部分脚本下

    "scripts": {
"test": "nyc --reporter=html --reporter=text --reporter=cobertura node_modules/mocha/bin/_mocha --reporter mocha-multi-reporters --reporter-options configFile=mocha-multi-reporters.config build/test/test.int-test.js"
}

任何人都可以面对这样的情况吗?那如何从集成测试文件启动异步/等待服务。

【问题讨论】:

    标签: node.js typescript mocha.js google-cloud-platform chai


    【解决方案1】:

    我终于找到了运行和调试集成测试的解决方案,我们需要在这里做一些更改。

    1. 对于超时问题最重要的是,我们必须将超时设置为 0,即**this.timeout(0)**
    2. 虽然在 WebStorm 的 mocha 设置中调试应该指向 .js 文件,但不要使用 .ts 文件,因为 mocha 连接 .js 文件来运行以及调试测试,但是我们也可以使用 .ts 文件来运行测试。 (https://journal.artfuldev.com/write-tests-for-typescript-projects-with-mocha-and-chai-in-typescript-86e053bdb2b6)。
    3. 要运行,请使用命令“npm run name-of-test-script”,mocha 将只挂接 .js 文件。

    【讨论】:

      猜你喜欢
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      相关资源
      最近更新 更多