[英]Node Testing with Mocha & Chai -> Error.. TypeError: app.address is not a function
[英]Mocha API Testing: getting 'TypeError: app.address is not a function'
我編寫了一個非常簡單的 CRUD API 並且我最近開始使用chai
和chai-http
編寫一些測試,但是在使用$ mocha
運行我的測試時遇到了問題。
當我運行測試時,我在 shell 上收到以下錯誤:
類型錯誤:app.address 不是 function
這是我的一個測試示例( /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);
...
我的結果是一個空的 object: server: {}
。
您不會在應用程序模塊中導出任何內容。 嘗試將其添加到您的 app.js 文件中:
module.exports = server
導出app.listen(3000)
返回的http.Server
對象而不僅僅是函數app
,否則你會得到TypeError: app.address is not a function
。
const koa = require('koa');
const app = new koa();
module.exports = app.listen(3000);
const request = require('supertest');
const app = require('./index.js');
describe('User Registration', () => {
const agent = request.agent(app);
it('should ...', () => {
這也可能有所幫助,並滿足@dman 更改應用程序代碼以適合測試的點。
根據需要向本地主機和端口發出請求chai.request('http://localhost:5000')
代替
chai.request(server)
這修復了我在使用 Koa JS (v2) 和 ava js 時遇到的相同錯誤消息。
上面的答案正確地解決了這個問題: 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));
});
當我們在 node + typescript serverless 項目中使用 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 {} \\; find src -name " .js" -exec rm {} \\;
以防萬一,如果有人使用 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()
})
})
})
我正在使用 Jest 和 Supertest,但收到了同樣的錯誤。 這是因為我的服務器需要時間來設置(它與設置數據庫、讀取配置等異步)。 我需要使用 Jest 的beforeAll
助手來允許異步設置運行。 我還需要重構我的服務器以分離監聽,而是使用@Whyhankee 的建議來創建測試服務器。
索引.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();
測試文件
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...
在主 API 文件(如index.js
)的末尾導出app
。
module.exports = app;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.