具有TypeError的开玩笑/超级测试错误:app.address不是函数 [英] Jest/Supertest errors with TypeError: app.address is not a function

查看:49
本文介绍了具有TypeError的开玩笑/超级测试错误:app.address不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我尝试了答案此处-无法帮助解决问题,被接受的答案无法解决我的问题.

Firstly, I have tried the answer here - didn't help to resolve, the accepted answer didn't resolve my issue.

我正在尝试使用Jest/Supertest测试我的API端点.从一个简单的/test端点开始.但是,当我运行测试时,我得到: TypeError:app.address不是函数.

I am trying to test my API endpoints with Jest/Supertest. Starting with a simple /test endpoint. However when I run the tests, I get: TypeError: app.address is not a function.

app.js :

...
// Set server (default to 3000)
  app.set('port', process.env.PORT || 3000);
  // Start server
  const server = http.listen(app.get('port'), () => {
    logger.info(`Worker ${process.pid} running on ${app.get('port')} in ${app.get('env')} mode`);
  });

  module.exports = server;
...

app.test.js :

const server = require('./app');
const supertest = require('supertest');
const request = supertest(server);

it('Gets the test endpoint', async (done) => {
  // Sends GET Request to /test endpoint
  const res = await request.get('/test');
  done();
});

测试运行输出:

FAIL  ./app.test.js
  ✕ Gets the test endpoint (24 ms)

  ● Gets the test endpoint

    TypeError: app.address is not a function

      14 | it('Gets the test endpoint', async (done) => {
      15 |   // Sends GET Request to /test endpoint
    > 16 |   const res = await request.get('/test');
         |                             ^
      17 |
      18 |   // ...
      19 |   done();

欢迎任何输入,谢谢.

推荐答案

来自上面 @jonrsharpe 的评论是正确的答案:

Comment from @jonrsharpe above was the correct answer:

Supertest使用应用程序,而不是服务器.您无需监听,它为您设置了端口(这是好处之一-stackoverflow.com/a/62992056/3001761).我建议拆分应用程序和服务器部分,就像我在这里所做的 https://github.com/textbook/starter-kit/tree/1567d269b8afe5d93408202900ac0ac1473fd89e/server

这篇关于具有TypeError的开玩笑/超级测试错误:app.address不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆