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
Add a ment  | 

1 Answer 1

Reset to default 10

It may be a problem because of the transpilation. Try:

let app = require('../src/app').default;

本文标签: javascriptNode Testing with Mocha amp Chai gt Error TypeError appaddress is not a functionStack Overflow