admin管理员组文章数量:1294326
I am getting the following error when trying to test a basic GET Route in node
TypeError: app.address is not a function
I am retrieving my app code that I want to test but I don't see any reference to "address" error in my code so I don't know what to fix.
Any suggestions from anyone?
Below is my unit test
let chai = require('chai');
let chaiHttp = require('chai-http');
let app = require('../src/app');
let should = chai.should();
chai.use(chaiHttp);
describe('/POST getRating', () => {
it('it should not POST a book without pages field', (done) => {
chai.request(app)
.get('/')
// .send(testData1)
.end((err, res) => {
console.log('ERROR', err);
res.should.have.status(200);
res.body.should.be.a('string');
done();
});
});
});
Below is my app.js code
import express from 'express';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import config from './config';
import http from 'http'
mongoose.Promise = Promise;
import rating from './ponents';
const cors = config.cors
const mongouri = config.mongoURI;
mongoose.connect(mongouri);
const app = express();
app.use(cors.cors(cors.origins));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get("/", (req, res) => res.json({message: "Wele to our Bookstore!"}));
app.use('/api/rating', rating);
const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
const server = http.createServer(app);
server.listen(port);
server.on('listening', onListening);
function normalizePort(val) {
let port = parseInt(val, 10);
if (isNaN(port)) {
return val;
}
if (port >= 0) {
return port;
}
return false;
}
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log('Listening on ' + bind);
}
export default app;
I am getting the following error when trying to test a basic GET Route in node
TypeError: app.address is not a function
I am retrieving my app code that I want to test but I don't see any reference to "address" error in my code so I don't know what to fix.
Any suggestions from anyone?
Below is my unit test
let chai = require('chai');
let chaiHttp = require('chai-http');
let app = require('../src/app');
let should = chai.should();
chai.use(chaiHttp);
describe('/POST getRating', () => {
it('it should not POST a book without pages field', (done) => {
chai.request(app)
.get('/')
// .send(testData1)
.end((err, res) => {
console.log('ERROR', err);
res.should.have.status(200);
res.body.should.be.a('string');
done();
});
});
});
Below is my app.js code
import express from 'express';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import config from './config';
import http from 'http'
mongoose.Promise = Promise;
import rating from './ponents';
const cors = config.cors
const mongouri = config.mongoURI;
mongoose.connect(mongouri);
const app = express();
app.use(cors.cors(cors.origins));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get("/", (req, res) => res.json({message: "Wele to our Bookstore!"}));
app.use('/api/rating', rating);
const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
const server = http.createServer(app);
server.listen(port);
server.on('listening', onListening);
function normalizePort(val) {
let port = parseInt(val, 10);
if (isNaN(port)) {
return val;
}
if (port >= 0) {
return port;
}
return false;
}
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log('Listening on ' + bind);
}
export default app;
Share
asked Feb 28, 2017 at 17:21
andreandre
1,6804 gold badges19 silver badges31 bronze badges
1
- that works! Thanks so much!!! – andre Commented Mar 1, 2017 at 16:33
1 Answer
Reset to default 10It may be a problem because of the transpilation. Try:
let app = require('../src/app').default;
版权声明:本文标题:javascript - Node Testing with Mocha & Chai -> Error.. TypeError: app.address is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741590618a2387100.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论