带有 TypeError 的 Jest/Supertest 错误:app.address is not a function

谷石井

首先,我在这里尝试了答案- 没有帮助解决,接受的答案没有解决我的问题。

我正在尝试使用 Jest/Supertest 测试我的 API 端点。从一个简单的 /test 端点开始。但是,当我运行测试时,我得到: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 的评论是正确答案:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeError: app.address is not a function 即使我正在导出我的服务器

如何修复带有 Firebase 的 Vue 应用程序中的错误:“TypeError: firebase.auth.sendPasswordResetEmail is not a function”?

创建带有address_prefixes错误的子网

TypeError:app.address不是函数

带有ChildProcessError的Google Cloud Function错误

带有TypeError的Django错误:必须为unicode而非str

为什么会出现此错误?TypeError:对象不是'instanceof'的有效参数(从'instanceof Address'评估'-REACT-NATIVE

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

TypeScript + Mocha + Express-TypeError:app.address不是函数

带有 HttpClient 参数的服务单元测试引发“TypeError: _this.handler.handle is not a function”

带有列表的python TypeError

错误TypeError:_co.function不是函数

Python套接字错误TypeError:需要类似字节的对象,而不是带有send函数的'str'

setTimeout返回带有Uncaught TypeError的错误:Chrome中AudioContext中的非法调用

意外结果:从错误的函数调用中断言TypeError时,带有match参数的pytest.raises()

带有Jest错误,TypeError的Vue组件测试:无法读取未定义的属性“名称”

Apache2 带有 mod_wsgi python3 'TypeError:' 并返回错误 500

带有/ set对象的html链接TypeError的Django错误是不可逆的

带有TypeError的plt.bar错误:bar()缺少1个必需的位置参数:'height'

Matplotlib:条形图,每个条上带有颜色渐变。错误:TypeError:“ ListedColormap”类型的对象没有len()

带有Cookie的错误

带有“>=”的类型错误

带有Vue 3的Vue路由器引发错误“未捕获的TypeError:Object(...)不是函数”

TypeError:“ FirefoxWebElement”对象在带有Selenium的动态网页上循环浏览页面时不会出现错误

Keras:带有batch_size的TypeError

带有 TypeError 异常的 Dart 结果

带有 __PRETTY_FUNCTION__ 的自定义错误处理程序

带有 std::function 的 RAII

密钥保管箱返回带有访问令牌的401(MSI PowerShell Function App)