Jest - TypeError: app.address is not a function for router
Jest - TypeError: app.address is not a function for router
我的app.js
如下...
require('dotenv').config();
const koa = require('koa');
const router = require('./routes/src').default;
const koaBody = require('koa-body');
const app = new koa();
app.use(koaBody({ multipart: true }));
app.use(router());
app.listen(process.env.PORT);
module.exports = app;
我正在合并 routes/scr/index.js
中的路线...
const combineRouters = require('koa-combine-routers');
const Router = require('koa-router');
const service = require('../../services/index').default;
const router = new Router({ prefix: '/api' });
router.post('/retrieve', service);
const routers = combineRouters(
router
);
module.exports.default = routers;
现在我想使用 jest
测试以下 post 请求。我尝试了以下...
const request = require('supertest');
const route = require('../src/index'); // routes/scr/index.js
test('Hello world works', async () => {
const payload = {
test: ''
};
try {
const response = await request(route).post('/api/retrieve').send(payload);
} catch (err) {
console.log(err);
}
});
出于某种原因,我收到以下 supertest
错误...
TypeError: app.address is not a function
我通过这样定义我的服务器来解决这个问题...
const supertest = require('supertest');
const http = require('http');
const app = require('../../app');
const request = supertest(http.createServer(app.callback()));
...
我的app.js
如下...
require('dotenv').config();
const koa = require('koa');
const router = require('./routes/src').default;
const koaBody = require('koa-body');
const app = new koa();
app.use(koaBody({ multipart: true }));
app.use(router());
app.listen(process.env.PORT);
module.exports = app;
我正在合并 routes/scr/index.js
中的路线...
const combineRouters = require('koa-combine-routers');
const Router = require('koa-router');
const service = require('../../services/index').default;
const router = new Router({ prefix: '/api' });
router.post('/retrieve', service);
const routers = combineRouters(
router
);
module.exports.default = routers;
现在我想使用 jest
测试以下 post 请求。我尝试了以下...
const request = require('supertest');
const route = require('../src/index'); // routes/scr/index.js
test('Hello world works', async () => {
const payload = {
test: ''
};
try {
const response = await request(route).post('/api/retrieve').send(payload);
} catch (err) {
console.log(err);
}
});
出于某种原因,我收到以下 supertest
错误...
TypeError: app.address is not a function
我通过这样定义我的服务器来解决这个问题...
const supertest = require('supertest');
const http = require('http');
const app = require('../../app');
const request = supertest(http.createServer(app.callback()));
...