Close server after tests
See original GitHub issueI 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:
- Created 6 years ago
- Reactions:24
- Comments:36 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Have you tried
mocha --exit
?I use the following to setup and tear down the server in mocha 4+
I haven’t checked if there’s some way to get a reference to the server(s) superagent sets up.