运行 或使用 typescript、Mocha、Chai 和 SuperTest 对 nodeJs 进行调试集成测试 async/await 节点 api-函数

Run or Debug integration test with nodeJs using typescript, Mocha, Chai and SuperTest for async/await node api-functions

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

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

但我总是收到类似“错误:您无权访问 Google KMS”中的密钥的错误(即在服务中)并加上它说'错误:超时超过 60000 毫秒。对于异步测试和挂钩,确保 "done()" 被调用;如果返回一个 Promise,请确保它 resolves.' ,但如果我单独 运行 service/application 它工作正常。所以这意味着当 运行ning 服务时 API/function 调用的 async/await 代码是相同的。

所以我的意思是,这是否是由于在从 before() 挂钩函数启动服务时 async/await 请求超时而发生的。

下面是 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"
}

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

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

  1. 对于超时问题最重要的是我们必须将超时设置为 0,即 **this.timeout(0)**
  2. 虽然调试应该指向 WebStorm 中 mocha 设置中的 .js 文件,但不要使用 .ts 文件,因为 mocha 挂钩 .js 文件用于 运行ning 以及调试测试,但是我们可以还可以使用 .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 文件。