2017-09-04 63 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文件。
相關問題