Vinod Vinod - 1 month ago 25
TypeScript Question

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.

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).

But always I am getting an error like 'Error: You are not authorized to access the key in Google KMS' (i.e. in service) and plus it said 'Error: Timeout of 60000ms exceeded. For async tests and hooks, ensure "done()" is called; 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.

So my point is here does this happen due to timeout the async/await request while initiating service from before() hook function.

Below are code sample in test.int-test.ts file,

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

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

Answer Source

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 i.e. **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. (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.