Mocha API Testing: getting 'TypeError: app.address is not a function' Mocha API Testing: getting 'TypeError: app.address is not a function' javascript javascript

Mocha API Testing: getting 'TypeError: app.address is not a function'


You don't export anything in your app module. Try adding this to your app.js file:

module.exports = server


It's important to export the http.Server object returned by app.listen(3000) instead of just the function app, otherwise you will get TypeError: app.address is not a function.

Example:

index.js

const koa = require('koa');const app = new koa();module.exports = app.listen(3000);

index.spec.js

const request = require('supertest');const app = require('./index.js');describe('User Registration', () => {  const agent = request.agent(app);  it('should ...', () => {


This may also help, and satisfies @dman point of changing application code to fit a test.

make your request to the localhost and port as neededchai.request('http://localhost:5000')

instead of

chai.request(server)

this fixed the same error message I had using Koa JS (v2) and ava js.