当单元测试代码时,
2通过(14ms)1失败
1)测试套件外部未捕获的错误:未捕获的错误:listen EaddRinuse:已在使用的地址:::3000在服务器上。SetupListenHandle[AS_Listen2](net.js:1255:14)在listenInCluster(net.js:1303:12)在服务器上。listen(net.js:1391:7)在Function.listen(net.js:1391:7)在Object.listen(node_modules/express/lib/application.js:618:24)在Module._compile(internal/modules/cjs/loader.js:721:30)/pirates/lib/index.js:99:24)在module._extensions.。js(内部/modules/cjs/loader.js:732:10)在object.newloader[as.js](Node_modules/pirates/lib/index.js:104:7)在module.load(内部/modules/cjs/loader.js:620:32)在tryModuleLoad(内部/modules/cjs/loader.js:560:12)在function.module._load(内部
我使用的是mocha,我的package.json上有--watch
。我用es6的方法来表达。
package.json
{
"name": "elies6express",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/mocha --watch --require @babel/register",
"start": "nodemon --exec babel-node main.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"bookshelf": "^0.14.2",
"chai-http": "^4.3.0",
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"dotenv": "^8.0.0",
"express": "^4.17.0",
"knex": "^0.16.5",
"morgan": "^1.9.1",
"path": "^0.12.7",
"pg": "^7.11.0"
},
"devDependencies": {
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.5",
"@babel/node": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"@babel/register": "^7.4.4",
"chai": "^4.2.0",
"mocha": "^6.1.4",
"nodemon": "^1.19.0",
"reify": "^0.19.1",
"request": "^2.88.0"
}
}
main.js
import 'dotenv/config';
import cors from 'cors';
import express from 'express';
import logger from 'morgan';
import path from 'path';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import userRoute from './routes/users';
const app = express();
app.use(cors());
app.use(logger('dev'));
// For React Stuff if need be
// app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'build')));
app.use(cookieParser());
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.use('/users', userRoute);
app.use(() => (req, res, next) =>{
res.locals.user = req.user; // This is the important line
// req.session.user = user
console.log(res.locals.user);
next();
});
app.use(bodyParser.urlencoded({ extended:false}));
//build mode
// app.get('*', (req, res) => {
// res.sendFile(path.join(__dirname+'/client/public/index.html'));
// })
app.listen(process.env.PORT, () =>
console.log(`Example app listening on port ${process.env.PORT}!`),
);
export default app;
main.test.js
import chai from "chai"
import chaiHttp from 'chai-http';
import request from 'request';
import server from '../main';
const expect = chai.expect;
const should = chai.should();
chai.use(chaiHttp);
// should get /
describe('should GET /', () => {
it('should get 200 status', (done) =>{
chai.request(server)
.get('/')
.end( (err, res) => {
res.should.have.status(200);
done();
});
});
})
// should check for Hello World!
describe('Should check for Hello World! text', () => {
it('should check for hello world text', (done) =>{
chai.request(server)
.get('/')
.end( (err, res) => {
expect(res.body).to.be.an('object') // works
expect(res.text).to.equal('Hello World!') // use res.text to check for res.send() text
done();
})
})
})
在main.js
中,将您的app.listen
调用放在!module.parent
的测试中,如下所示:
if(!module.parent){
app.listen(process.env.PORT, () =>
console.log(`Example app listening on port ${process.env.PORT}!`),
);
}
来源:http://www.marcusoft.net/2015/10/eaddrinuse-when-watch-tests-with-mocha-and-supertest.html
“testwatch”:“nodemon--exec”Mocha--recursive“”
100%
将服务于您的目的,直到测试库发布更相关的内容
这意味着某些东西已经在端口3000上运行。您需要停止此操作,然后启动您的应用程序。或者只是将应用程序的端口更改为其他端口。
要释放端口3000,请使用:
FUSER-K 3000/TCP
或
kill $(sudo lsof -t -i:3000)
要更改应用程序的端口,请执行以下操作:
var port = 8000;
app.listen(port, () =>
console.log(`Example app listening on port ${port}!`),
);