Javascript Mocha API 测试:获取“TypeError: app.address is not a function”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33986863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mocha API Testing: getting 'TypeError: app.address is not a function'
提问by charliebrownie
My Issue
我的问题
I've coded a very simple CRUD API and I've started recently coding also some tests using chai
and chai-http
but I'm having an issue when running my tests with $ mocha
.
我编写了一个非常简单的 CRUD API,最近我也开始编写一些使用chai
和 的测试,chai-http
但是在使用$ mocha
.
When I run the tests I get the following error on the shell:
当我运行测试时,我在 shell 上收到以下错误:
TypeError: app.address is not a function
TypeError: app.address is not a function
My Code
我的代码
Here is a sample of one of my tests (/tests/server-test.js):
这是我的一个测试示例(/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...
});
});
And my express
app files (/server/app.js):
我的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);
});
and (/server/routes/api.js):
和(/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;
Extra notes
附加说明
I've tried logging out the server
variable in the /tests/server-test.jsfile before running the tests:
在运行测试之前,我尝试注销/tests/server-test.js文件中的server
变量:
...
var server = require('../server/app'); // my express app
...
console.log('server: ', server);
...
and I the result of that is an empty object: server: {}
.
我的结果是一个空对象:server: {}
。
回答by xersiee
You don't export anything in your app module. Try adding this to your app.js file:
您不会在应用程序模块中导出任何内容。尝试将其添加到您的 app.js 文件中:
module.exports = server
回答by Kim Kern
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
.
导出由http.Server
返回的对象app.listen(3000)
而不仅仅是函数很重要app
,否则您将获得TypeError: app.address is not a function
.
Example:
例子:
index.js
索引.js
const koa = require('koa');
const app = new koa();
module.exports = app.listen(3000);
index.spec.js
index.spec.js
const request = require('supertest');
const app = require('./index.js');
describe('User Registration', () => {
const agent = request.agent(app);
it('should ...', () => {
回答by Skyguard
This may also help, and satisfies @dman point of changing application code to fit a test.
这也可能有所帮助,并满足@dman 更改应用程序代码以适合测试的点。
make your request to the localhost and port as needed
chai.request('http://localhost:5000')
根据需要向本地主机和端口发出请求
chai.request('http://localhost:5000')
instead of
代替
chai.request(server)
chai.request(server)
this fixed the same error message I had using Koa JS (v2) and ava js.
这修复了我在使用 Koa JS (v2) 和 ava js 时遇到的相同错误消息。
回答by Whyhankee
The answers above correctly address the issue: supertest
wants an http.Server
to work on. However, calling app.listen()
to get a server will also start a listening server, this is bad practice and unnecessary.
上面的答案正确地解决了这个问题:supertest
想要http.Server
继续工作。但是,调用app.listen()
获取服务器也会启动侦听服务器,这是不好的做法且没有必要。
You can get around by this by using http.createServer()
:
您可以使用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));
});
回答by Dilunika
We had the same issue when we run mocha using ts-node in our node + typescript serverless project.
当我们在 node + typescript serverless 项目中使用 ts-node 运行 mocha 时,我们遇到了同样的问题。
Our tsconfig.json had "sourceMap": true . So generated, .js and .js.map files cause some funny transpiling issues (similar to this). When we run mocha runner using ts-node. So, I will set to sourceMap flag to false and deleted all .js and .js.map file in our src directory. Then the issue is gone.
我们的 tsconfig.json 有 "sourceMap": true 。如此生成的 .js 和 .js.map 文件会导致一些有趣的转译问题(与此类似)。当我们使用 ts-node 运行 mocha runner 时。因此,我将 sourceMap 标志设置为 false 并删除 src 目录中的所有 .js 和 .js.map 文件。那么问题就没有了。
If you have already generated files in your src folder, commands below would be really helpful.
如果您已经在 src 文件夹中生成了文件,下面的命令将非常有用。
find src -name ".js.map" -exec rm {} \; find src -name ".js" -exec rm {} \;
find src -name " .js.map" -exec rm {} \; find src -name ".js" -exec rm {} \;