Mocha API 测试:收到“TypeError:app.address 不是函数”

问题描述 投票:0回答:11

我的问题

我编写了一个非常简单的 CRUD API,最近我也开始使用

chai
chai-http
编写一些测试,但在使用
$ mocha
运行测试时遇到问题。

当我运行测试时,我在 shell 上收到以下错误:

类型错误:app.address 不是函数

我的代码

这是我的一个测试的示例(/tests/server-test.js):

var chai = require('chai');
var mongoose = require('mongoose');
var chaiHttp = require('chai-http');
var server = require('../server/app'); // my express app
var should = chai.should();
var testUtils = require('./test-utils');

chai.use(chaiHttp);

describe('API Tests', function() {
  before(function() {
    mongoose.createConnection('mongodb://localhost/bot-test', myOptionsObj);
  });

  beforeEach(function(done) {
    // I do stuff like populating db
  });

  afterEach(function(done) {
    // I do stuff like deleting populated db
  });

  after(function() {
    mongoose.connection.close();
  });

  describe('Boxes', function() {

    it.only('should list ALL boxes on /boxes GET', function(done) {
      chai.request(server)
        .get('/api/boxes')
        .end(function(err, res){
          res.should.have.status(200);
          done();
        });
    });

    // the rest of the tests would continue here...

  });

});

还有我的

express
应用程序文件(/server/app.js):

var mongoose = require('mongoose');
var express = require('express');
var api = require('./routes/api.js');
var app = express();

mongoose.connect('mongodb://localhost/db-dev', myOptionsObj);

// application configuration
require('./config/express')(app);

// routing set up
app.use('/api', api);

var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('App listening at http://%s:%s', host, port);
});

和(/server/routes/api.js):

var express = require('express');
var boxController = require('../modules/box/controller');
var thingController = require('../modules/thing/controller');
var router = express.Router();

// API routing
router.get('/boxes', boxController.getAll);
// etc.

module.exports = router;

补充说明

在运行测试之前,我尝试注销

/tests/server-test.js
文件中的 server 变量:

...
var server = require('../server/app'); // my express app
...

console.log('server: ', server);
...

我的结果是一个空对象:

server: {}

javascript node.js express mocha.js chai
11个回答
325
投票

您不会导出应用程序模块中的任何内容。尝试将其添加到您的 app.js 文件中:

module.exports = server

64
投票

重要的是导出

http.Server
返回的
app.listen(3000)
对象,而不仅仅是函数
app
,否则你会得到
TypeError: app.address is not a function

示例:

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 ...', () => {

34
投票

这也可能有帮助,并满足 @dman 更改应用程序代码以适应测试的观点。

根据需要向本地主机和端口发出请求


chai.request('http://localhost:5000')

而不是


chai.request(server)

这修复了我使用 Koa JS (v2) 和 ava js 时遇到的相同错误消息。


14
投票

上面的答案正确地解决了这个问题:

supertest
想要一个
http.Server
来工作。然而,调用
app.listen()
来获取服务器也会启动监听服务器,这是不好的做法,也是不必要的。

您可以使用

http.createServer()
:

来解决这个问题
import * as http from 'http';
import * as supertest from 'supertest';
import * as test from 'tape';
import * as Koa from 'koa';

const app = new Koa();

# add some routes here

const apptest = supertest(http.createServer(app.callback()));

test('GET /healthcheck', (t) => {
    apptest.get('/healthcheck')
    .expect(200)
    .expect(res => {
      t.equal(res.text, 'Ok');
    })
    .end(t.end.bind(t));
});


5
投票

以防万一,如果有人使用Hapijs,问题仍然会发生,因为它不使用Express.js,因此address()函数不存在。

TypeError: app.address is not a function
      at serverAddress (node_modules/chai-http/lib/request.js:282:18)

使其发挥作用的解决方法

// this makes the server to start up
let server = require('../../server')

// pass this instead of server to avoid error
const API = 'http://localhost:3000'

describe('/GET token ', () => {
    it('JWT token', (done) => {
       chai.request(API)
         .get('/api/token?....')
         .end((err, res) => {
          res.should.have.status(200)
          res.body.should.be.a('object')
          res.body.should.have.property('token')
          done()
      })
    })
  })

5
投票

在主 API 文件末尾导出

app
,如
index.js

module.exports = app;

2
投票

当我们在 Node + TypeScript 无服务器项目中使用 ts-node 运行 mocha 时,我们遇到了同样的问题。

我们的 tsconfig.json 有 "sourceMap": true 。这样生成的 .js 和 .js.map 文件会导致一些有趣的转译问题(与此类似)。当我们使用 ts-node 运行 mocha runner 时。因此,我将 sourceMap 标志设置为 false 并删除 src 目录中的所有 .js 和 .js.map 文件。然后问题就消失了。

如果您已经在 src 文件夹中生成了文件,下面的命令将非常有用。

find src -name ".js.map" -exec rm {} \; 找到 src -name ".js" -exec rm {} \;


2
投票

我正在使用 Jest 和 Supertest,但收到了同样的错误。这是因为我的服务器需要时间来设置(它与设置数据库、读取配置等异步)。我需要使用 Jest 的

beforeAll
帮助程序来允许异步设置运行。我还需要重构我的服务器以单独监听,并使用 @Whyhankee 的建议来创建测试的服务器。

index.js

export async function createServer() {
  //setup db, server,config, middleware
  return express();
}

async function startServer(){
  let app = await createServer();
  await app.listen({ port: 4000 });
  console.log("Server has started!");
}

if(process.env.NODE_ENV ==="dev") startServer();

测试.ts

import {createServer as createMyAppServer} from '@index';
import { test, expect, beforeAll } from '@jest/globals'
const supertest = require("supertest");
import * as http from 'http';
let request :any;

beforeAll(async ()=>{
  request = supertest(http.createServer(await createMyAppServer()));
})

test("fetch users", async (done: any) => {
  request
    .post("/graphql")
    .send({
      query: "{ getQueryFromGqlServer (id:1) { id} }",
    })
    .set("Accept", "application/json")
    .expect("Content-Type", /json/)
    .expect(200)
    .end(function (err: any, res: any) {
      if (err) return done(err);
      expect(res.body).toBeInstanceOf(Object);
      let serverErrors = JSON.parse(res.text)['errors'];
      expect(serverErrors.length).toEqual(0);
      expect(res.body.data.id).toEqual(1);
      done();
    });
});

编辑:

我在使用

data.foreach(async()=>...
时也遇到了错误,应该在我的测试中使用
for(let x of...


0
投票

您必须导出应用程序而不是服务器。 Supertest 采用应用程序,而不是服务器。

module.exports = app;

0
投票

对于那些出于某种原因来这里寻求 Fastify 解决方案的人来说,这是

import { app } from "../index.js"; // <-- the app, not object returned from .listen()

beforeAll(async () => {
    // wait until the server actually starts, otherwise eat weird errors
    await app.ready();
});

test('your great test', async () => {
    const agent = supertest.agent(app.server); // <-- pass the server, not the app
    // .....your tests here
});

0
投票

对于即使在分离应用程序和服务器并使用打字稿后也遇到此问题的人。尝试使用

module.exports = app 
而不是
export default app
导出。不知道为什么我会遇到这个问题,但更改它有效。

© www.soinside.com 2019 - 2024. All rights reserved.