oaijs / koa-oai-router

Koa Router, based on OpenAPI, Swagger and Json Schema.
105 stars 15 forks source link

Unterminated Promise #45

Closed dancju closed 6 years ago

dancju commented 6 years ago

An error occurred while I was testing a REST server with jest and supertest. It seems a stuck Promise in koa-oai-router was detected.

Jest has detected the following 3 open handles potentially keeping Jest from exiting:

  ●  DNSCHANNEL

      at dns.js:338:23

  ●  PROMISE

          at Promise.then (<anonymous>)
      at OAIRouter.routes (node_modules/koa-oai-router/lib/router.js:100:40)
      at Object.<anonymous> (src/index.ts:141:16)
      at Object.<anonymous> (test/app.test.ts:3:1)

  ●  PROMISE

          at Promise.then (<anonymous>)
          at Promise.catch (<anonymous>)
      at OAIRouter.routes (node_modules/koa-oai-router/lib/router.js:114:17)
      at Object.<anonymous> (src/index.ts:141:16)
      at Object.<anonymous> (test/app.test.ts:3:1)
amazing-gao commented 6 years ago

Sorry for the incomplete docs. The api parser of router is asynchronous. Router has two events ready, error.

Before router ready, you should't use it. You can refer to my test code. https://github.com/BiteBit/koa-oai-router/blob/6f11e25288ec3e69f279fcdb7abce57876bb8b6e/test/helpers/index.js#L12-L38

dancju commented 6 years ago

Thank you for replying.

I have tried the method you gave (with a little bit difference). And it did not work for me.

// app.ts
// ...
const router = new Router({
  apiDoc: path.join(__dirname, "api"),
  apiExplorerVisible: true
});
router.mount(middleware, path.join(__dirname, "controllers"));
app.use(router.routes());

validate(app);

export { app, router };
// app.spec.ts
import request from "supertest";

import { app, router } from "../src";

const status = new Promise((resolve, reject) => {
  router.on("ready", resolve);
  router.on("error", error => {
    reject(error);
  });
});

describe("routes: misc", async () => {
  await status;
// ...

Now I get this.

Jest has detected the following 4 open handles potentially keeping Jest from exiting:

  ●  DNSCHANNEL

      at dns.js:338:23

  ●  PROMISE

          at Promise.then (<anonymous>)
          at Promise.catch (<anonymous>)
      at OAIRouter.routes (node_modules/koa-oai-router/lib/router.js:114:17)
      at Object.<anonymous> (src/index.ts:143:16)
      at Object.<anonymous> (test/app.spec.ts:3:1)

  ●  PROMISE

      11 | });
      12 | 
    > 13 | const status = new Promise((resolve, reject) => {
         |                ^
      14 |   router.on("ready", resolve);
      15 |   router.on("error", error => {
      16 |     reject(error);

      at Object.<anonymous> (test/app.spec.ts:13:16)

Note that Promise const status = new Promise((resolve, reject) => { never resolves. I guess it means router.on("ready", resolve); was never emitted.

amazing-gao commented 6 years ago

Have you solved this problem? I think it may be that you have some errors before starting the app.

dancju commented 6 years ago

The website runs properly without errors. I am just ignoring this error.