我正在尝试使用玩笑进行集成测试(restApi测试)。我正在本地连接数据库。当我运行测试时,它显示出这样的错误,例如“监听EADDRINUSE:地址已在使用::: 5000。
product.test.js
const request=require('supertest')
let server;
describe('all data',()=>{
beforeEach(()=>{server=require('../../index');})
afterEach(()=>{server.close();})
it('should return all the data', async()=>{
const result=await request(server).get('/product');
expect(result.status).toBe(200)
})
})
index.js
var express = require('express')
var MongoClient = require('mongodb').MongoClient;
var body_parser = require('body-parser')
const product = require('./routes/product.js')
const register = require('./routes/registeration.js')
const login = require('./routes/login.js')
var app = express()
app.use(body_parser.json())
app.use('/product', product);
app.use('/register',register)
app.use('/login',login)
const port = process.env.PORT || 5000;
const server = app.listen(port, () => console.log(`Listening on port ${port}...`));
module.exports=server;
product.js
var express = require('express')
var validate = require('../models/product.js')
const router = express.Router();
const MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID
var validate_token = require('../jwt/jwttoken.js')
var file = require('../log_file/logfile.js')
var config=require('config')
const client = new MongoClient(config.get('db'), { useNewUrlParser: true })
router.get('/', async (req, res, next) => {
try {
await client.connect();
const db = client.db('olc_prod_db');
const r = await db.collection('Ecommerce').find().toArray();
res.send(r)
} catch (err) {
console.log(err.stack);
}
});