1
我有koa服務器連接mongo並使用supertest模擬服務器併發送請求,將其作爲測試框架。測試後關閉服務器和db連接
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}`)
}
})
成功完成測試或失敗後,服務器連接鋼鐵廠的作品,測試後koa服務器連接有多接近?
測試例如:
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: '[email protected]',
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: '[email protected]',
}
r
.post(`/api/auth/signup`)
.send(userData)
.expect(400)
.expect({
errors: {
password: 'Password required',
},
})
.end(done)
})
})
})