question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Close server after tests

See original GitHub issue

I have koa server with mongo connection, jest as test framework.

const app = new Koa()
...
export default app.listen(PORT, (err) => {
  if (err) console.log(err)

  if (!IS_TEST) {
    console.log(`Server running on port: ${PORT}`)
  }
})

After success finish test or fail server connection steel works, how close koa server connection after test ?

Test example:

import supertest from 'supertest'
import mongoose from 'mongoose'
import server from '../../../app/server'
import User from '../../../app/models/user'

const r = supertest.agent(server.listen())

afterEach(async (done) => {
  await mongoose.connection.db.dropDatabase()
  done()
})

describe('Authorization', () => {
  describe('POST /signup', () => {
    const userData = {
      email: 'test@test.com',
      password: 111111,
    }

    test('success create user', (done) => {
      r
        .post(`/api/auth/signup`)
        .send(userData)
        .expect(200)
        .expect({
          data: {
            email: userData.email,
          },
        })
        .end(done)
    })

    test('fail of user create, password required', (done) => {
      const userData = {
        email: 'test@test.com',
      }

      r
        .post(`/api/auth/signup`)
        .send(userData)
        .expect(400)
        .expect({
          errors: {
            password: 'Password required',
          },
        })
        .end(done)
    })

    test('fail of user create, user already exist', async (done) => {
      const userData = {
        email: 'test@test.com',
        password: 111111,
      }

      await User.create(userData)

      r
        .post(`/api/auth/signup`)
        .send(userData)
        .expect(400)
        .expect({
          errors: {
            email: `User already exist`,
          },
        })
        .end(done)
    })
  })
})

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:24
  • Comments:36 (1 by maintainers)

github_iconTop GitHub Comments

83reactions
demisxcommented, Nov 27, 2017

Have you tried mocha --exit?

31reactions
deployablecommented, Dec 1, 2017

I use the following to setup and tear down the server in mocha 4+

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

describe('int::app', function(){

  let request = null
  let server = null

  before(function(done){
    server = app.listen(done)
    request = supertest.agent(server)
  })

  after(function(done){
    server.close(done)
  })

  it('should get /api/v1/laps/85299', function(){
    return request.get('/api/v1/laps/85299')
      .expect(200, { data: {} })
  })

})

I haven’t checked if there’s some way to get a reference to the server(s) superagent sets up.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to correctly close express server between tests using ...
You can check that it's closing by adding this line server.on( 'close', () => console.log('Closing') ) just bellow server.listen in server.js ...
Read more >
Close server after tests · Issue #437 · ladjs/supertest - GitHub
I have koa server with mongo connection, jest as test framework. const app = new Koa() ... export default app.listen(PORT, ...
Read more >
How to correctly unit test Express server - Gleb Bahmutov
close () call. Only then the server is going to be closed and the tests continue correctly. properly closing the server after each...
Read more >
close() - Api - Mock Service Worker Docs
It's recommended to call .close() after all test suites to clean up and prevent the current interception layer from affecting irrelevant ...
Read more >
start-server-and-test - npm
Starts server, waits for URL, then runs test command; when the tests ... and then run multiple test commands (and stop the server...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found