ladjs / supertest

🕷 Super-agent driven library for testing node.js HTTP servers using a fluent API. Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ECONNREFUSED after swagger setup with YAML file.

GiuseppeMP opened this issue · comments

I'm working on API First mode generating routes from openapi3.x specifications, and using supertests to execute BDD, serving the swagger-ui by using yaml.file, sort of like:

//swagger.decorator.ts
import YAML from 'yamljs'
import swaggerUi from 'swagger-ui-express'
import { ExpressCommonDecorator } from './express-common.decorator'

const swaggerDocument = YAML.load('./api-openapi.yaml')
const swaggerOptions = {
  port:3002,
  explorer : true
}

export default class SwaggerExpressDecorator extends ExpressCommonDecorator{

  configureAddon(): void {
    this.app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swaggerDocument, swaggerOptions))
  }

}

For some reason, this line: YAML.load('./api-openapi.yaml'), make all supertests get broken, with the following error:

Error: connect ECONNREFUSED 127.0.0.1:80
        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)

Funky behavior = Removing the YAML.load('./api-openapi.yaml'), everything backs to normal.

I'm suspecting the I/O to read the yaml is hanging the app configuration and for some reason, supertest.default(app) not waiting this promise.

Additional infos:
The decorator ExpressCommonDecorator don't do nothing, its just a abstract class used to change/remove/add all decorators on going, without changing any code from server.ts with minimal impact.

//express-common.decorator.ts
import express from 'express'

export abstract class ExpressCommonDecorator {
  app: express.Application;

  constructor(app: express.Application) {
    this.app = app
    this.configureAddon()
  }
  
  abstract configureAddon(): void;
}

and server:

const app = express(),
  extended = true

// Decorate app with express ui/docs
new SwaggerExpressDecorator(app)

I forget the workaround in use:

if(process.env.NODE_ENV !== 'test') {
      const swaggerDocument = YAML.load('./api-openapi.yaml')
      const swaggerOptions = {
        port:3002,
        explorer : true
      }
      this.app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swaggerDocument, swaggerOptions))
}