apigee-127 / swagger-tools

A Node.js and browser module that provides tooling around Swagger.
MIT License
701 stars 373 forks source link

Unit tests with mocha - need server.close() #566

Closed dlarr closed 4 years ago

dlarr commented 6 years ago

Hello,

This is not an issue, but more questioning. I am trying to run unit tests in my tests. Somehow in my tests I need to stop HTTP server. So I am doing this :

    var server;
    beforeEach(function () {
        server = require('../app')
    });
    afterEach(function () {
        server.close();
    });

Of course it's not working with the example given to start server with the middleware

// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {

    // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
    app.use(middleware.swaggerMetadata());

    // Validate Swagger requests
    app.use(middleware.swaggerValidator());

    // Route validated requests to appropriate controller
    app.use(middleware.swaggerRouter(options));

    // Serve the Swagger documents and Swagger UI
    app.use(middleware.swaggerUi());

    // Custom error handler that returns JSON
    app.use(logError);
    app.use(errorHandling);

    // Start the server
    http.createServer(app).listen(serverPort, function () {
        console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
        console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort);
    });

});

The Questions are :

  1. Why the listen instruction is done INSIDE the initializeMiddleware fonction ?
  2. Is it mandatory ?
  3. Is there a problem doing this OUTSIDE the initializeMiddleware fonction ? (see JS below)

    
    // Initialize the Swagger middleware
    swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
    
    // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
    app.use(middleware.swaggerMetadata());
    
    // Validate Swagger requests
    app.use(middleware.swaggerValidator());
    
    // Route validated requests to appropriate controller
    app.use(middleware.swaggerRouter(options));
    
    // Serve the Swagger documents and Swagger UI
    app.use(middleware.swaggerUi());
    
    // Custom error handler that returns JSON
    app.use(logError);
    app.use(errorHandling);

});

const server = app.listen(serverPort); console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort); console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort); module.exports = server;



I have tried this and it appears
* The server runs OK, 
* It serves swagger-ui properly
* My tests run and shut down properly

Thanks anyway for reading ;)

Best regards,
Denis.
andrzejzysko commented 6 years ago

@dlarr your approach is fine. The only difference is that your server runs earlier then swagger-tools. It means that you can get errors before swagger-tools registers all resources in middleware. Run your app with debug=swagger* and you will see what is going on after your server start.

dlarr commented 6 years ago

Thanks a lot for input ! :)