Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to stop server from outside app.js - untestable #168

Open
christian-hawk opened this issue Nov 25, 2020 · 3 comments
Open

Not able to stop server from outside app.js - untestable #168

christian-hawk opened this issue Nov 25, 2020 · 3 comments
Assignees

Comments

@christian-hawk
Copy link
Contributor

Even that we created app-factory.js to create app and also resolved untestable app #141 , this fix works only for decouple created app from server (app.js). So it works for unit test.

But for integration test, not usually.
If we need to test if #163 if rate limit is blocking correctly `/health-check', we gonna need to send X calls till the rate limit is reached.
After this test, we need to restart server, otherwise we will not be able to request this endpoint anymore as it has alreasy reached rate-limit.

@christian-hawk
Copy link
Contributor Author

I did some R&D on that.
Looks like a lot of other users that use the hyped express have this issue. So guess what? There's a lib for that.

supertest may do the trick. According to docs you don't have to start server to run tests. They say:

You may pass an http.Server, or a Function to request() - if the server is not already listening for connections then it is bound to an ephemeral port for you so there is no need to keep track of ports.

We may find some tricky blockers in our way, as we have a very specific startup configuration load flow.

Also refactoring some tests may be required.

@christian-hawk
Copy link
Contributor Author

also a possible workaround:
on app.js also export httpServer right after httpServer = app.listen... line.
and in before hook:

const app = require('../../../server/app')

Beofre({ timeout: 600 * 1000 }, (done) => {
  // mock init external endpoints
  const initMock = new InitMock()
  initMock.passportConfigEndpoint()
  initMock.umaTokenEndpoint()
  initMock.umaConfigurationEndpoint()

  // waits for the server to start (app.listen)
  app.on('appStarted', () => {
    console.log('app started...')

    // import again, after server started = server instance
    let server = require('../../../server/app')
   // restarting...
    server.close()
    server = app.listen(8090, () => {
      console.log('Server listening for tests....')
      app.emit('appStarted') // event emitter for tests
    })
    done()
  })
})

Dirty, i know.

@kdhttps
Copy link
Contributor

kdhttps commented Jul 19, 2021

@christian-hawk i think we fix this problem. can we close this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants