简体   繁体   English

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

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

I am trying to run(using command npm run test and to debug i have been used IDE Webstorm ) the integration test developed using node.js written in typescript, mocha, chai and supertest for node application developed using typescript. 我正在尝试运行(使用命令npm run test进行调试,并调试我已经使用过IDE Webstorm )使用以打字稿编写的node.js,mocha,chai和supertest开发的集成测试,以使用打字稿开发的节点应用程序。

In before() hook function, we are making a call to the application which actually initiating service and this call are for asynchronous(used async-await) functions(from the app.ts/app.js file of node application). 在before()钩子函数中,我们正在对实际上启动服务的应用程序进行调用,并且此调用是针对异步(使用的async-await)函数的(来自节点应用程序的app.ts / app.js文件)。

But always I am getting an error like ' Error: You are not authorized to access the key in Google KMS ' (ie in service) and plus it said ' Error: Timeout of 60000ms exceeded. 但是,我总是收到类似“ 错误:您无权访问Google KMS中的密钥 ”(即在使用中)的错误 ,并显示“ 错误:超时超过60000ms。 For async tests and hooks, ensure "done()" is called; 对于异步测试和挂钩,请确保调用了“ done()”; if returning a Promise, ensure it resolves. 如果返回承诺,请确保其解决。 ' , but if I ran service/application individually it is working fine. ',但是如果我单独运行服务/应用程序,则可以正常运行。 so that means while running service the async/await code for API/function call is same. 因此,这意味着在运行服务时,用于API /函数调用的异步/等待代码是相同的。

So my point is here does this happen due to timeout the async/await request while initiating service from before() hook function. 所以我的意思是,这是由于从before()挂钩函数启动服务时异步/等待请求超时引起的。

Below are code sample in test.int-test.ts file, 以下是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
       });
    });
});

and under the section script in package.json 并在package.json中的section脚本下

    "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"
}

Can anybody face such a situation? 有人可以面对这样的情况吗? that how to initiate async/await service from integration test file. 如何从集成测试文件启动异步/等待服务。

Finally I have found the solution to run and debug integration test, we need to do few changes here. 最后,我找到了运行和调试集成测试的解决方案,我们在这里需要做一些更改。

  1. Most important to timeout issue we must set timeout as 0 ie **this.timeout(0)** 对于超时问题最重要的是,我们必须将超时设置为0,即**this.timeout(0)**
  2. While debugging should point to .js file in mocha setup in WebStorm, do not use .ts file, as mocha hooks up .js files for running as well as debugging a test, however we can also use .ts file to run test. 虽然调试应该指向WebStorm的Mocha设置中的.js文件,但是不要使用.ts文件,因为mocha会连接.js文件以运行以及调试测试,但是我们也可以使用.ts文件来运行测试。 ( https://journal.artfuldev.com/write-tests-for-typescript-projects-with-mocha-and-chai-in-typescript-86e053bdb2b6 ). https://journal.artfuldev.com/write-tests-for-typescript-projects-with-mocha-and-chai-in-typescript-86e053bdb2b6 )。
  3. To run, use command 'npm run name-of-test-script', mocha will hooks up .js file only. 要运行,请使用命令“ npm run name-of-test-script”,mocha将仅连接.js文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node / Mocha / Chai / Sinon-异步等待单元测试错误 - Node / Mocha / Chai / Sinon - Async await unit test error 在 mocha、chai 中使用 await/async - Using await / async with mocha, chai 在带有 supertest 的 mocha 断言中使用异步等待函数 - using async await function in mocha assertion with supertest 使用express,node.js构建的restful api的单元测试记录(使用mocha,supertest和chai) - Unit test logging (use mocha, supertest, and chai) of restful api built with express, node.js 在Mocha中使用Supertest来测试Node.js Express API和MongoDB - Using Supertest in Mocha to test Node.js Express API and MongoDB 如何在异步等待中强制通过测试用例 Node.js 使用 Chai 和 mocha 的单元测试代码 - How to pass test-case forcibly in Async await Node.js Unit Testing code using Chai and mocha 无法使用 mocha、chai 和 supertest 运行单元测试用例,得到:- 错误:ECONNREFUSED:连接被拒绝 - Unable to run unit test case using mocha, chai and supertest, getting:- Error: ECONNREFUSED: Connection refused 如何使用Mocha和Chai测试Login API NodeJs - How to test Login API NodeJs using Mocha and Chai 将Mocha Chai与Node.js错误结合使用:未指定测试 - Using mocha chai with Nodejs error: no test specified 异步等待 Node.js 使用 Chai 和 mocha 进行单元测试代码 - Async await Node.js Unit Testing code using Chai and mocha
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM