2017-09-04 104 views
0

我试图运行(使用命令npm run test并调试我已使用IDE Webstorm)使用节点开发的集成测试.js用打字稿,摩卡,chai和supertest编写,用于使用打字机开发的节点应用程序。使用typecript,Mocha,Chai和SuperTest进行异步/等待节点api-functions的运行或调试集成测试nodeJs

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

但始终我得到这样的错误“错误:您无权访问密钥在谷歌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" 
} 

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

回答

0

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

  1. 超时问题,最重要的是,我们必须设置超时时间为0,即**this.timeout(0)**
  2. 在调试应指向.js文件在WebStorm摩卡安装文件,不使用的.ts文件,如摩卡挂钩起来.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文件。
相关问题