【问题标题】:jest timeout on express server using supertest使用 supertest 在快速服务器上开玩笑超时
【发布时间】:2021-11-13 04:10:47
【问题描述】:

我正在尝试使用jestsupertest 测试快速应用的端点。这是我的文件:

// backend/tests/integration.test.ts
const server = require('../server');
const supertest = require('supertest');
const appTest = supertest(server);

describe('Integration tests', () => {
  it('GET /api/ping', async () => {
    const expected = { status: 200, body: { success: true } }
    const res = await appTest.get('/api/ping');
    expect(res.status).toEqual(expected.status);
    expect(res.body).toEqual(expected.body);
  });
});
// backend/server.ts
import app from './src/app';

const PORT = 3000;

const server = app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

export default server;
// backend/src/app.ts
const express = require('express');
const app = express();

app.get('/api/ping', (req, res) => {
  res.status(200).json({
    'success': true
  });
});

export default app;

我不断收到错误:

TypeError: app.address is not a function

并且堆栈跟踪说错误就行了:

       8 |   it('GET /api/ping', async () => {
       9 |     const expected = { status: 200, body: { success: true } }
    > 10 |     const res = await appTest.get('/api/ping');
         |                               ^
      11 |     expect(res.status).toEqual(expected.status);
      12 |     expect(res.body).toEqual(expected.body);
      13 |   });

我的配置如下:

// package.json
...
"scripts": {
  "test": "jest"
}
// jest.config.js
module.exports = {
  preset: 'ts-jest',
  modulePathIgnorePatterns: ["<rootDir>/dist/"],
  transform: {
    '^.+\/*.(ts|tsx)?$': 'ts-jest'
  }
};
// babel.config.js
module.exports = {
  presets: ['@babel/preset-env']
};
// tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist"
  },
  "lib": ["es2015"],
  "exclude": [
    "../node_modules"
  ],
  "include": [
    "./**/*.ts"
  ]
}

我不知道为什么会这样。已尝试谷歌搜索和阅读有关堆栈溢出超时错误的信息,但无济于事。

【问题讨论】:

    标签: node.js typescript express jestjs ts-jest


    【解决方案1】:

    backend/tests/integration.test.ts只使用没有http.createServer的服务器

    应该做出改变:

    1. 改为const appTest = supertest(http.createServer(server)); 使用const appTest = supertest(server);
    2. 如果你使用 async-await 从你不需要的签名中删除 done ,将测试签名更改为it('GET /api/ping', async () =&gt; {...}
    3. 删除规范末尾的 done 调用
    4. 添加到 jest.config allowJs: true,
    5. 比较依赖的版本

    package.json

    {
      "name": "node",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "jest"
      },
      "author": "",
      "license": "ISC",
      "dependencies": {
        "babel": "^6.23.0",
        "express": "^4.17.1",
        "jest": "^27.2.0",
        "supertest": "^6.1.6",
        "ts-jest": "^27.0.5"
      },
      "devDependencies": {
        "@types/jest": "^27.0.1"
      }
    }
    

    【讨论】:

    • 这使得测试运行,但我在做await appTest.get('/api/ping')时得到app.address is not a function
    • 请更新代码和输出
    • 更新了代码和输出
    • 更新我的答案,看起来像打字稿翻译问题尝试更改为 js 只是为了健全导入和 module.exports = server
    • 解决了!泰!!! :)
    猜你喜欢
    • 1970-01-01
    • 2021-06-04
    • 2020-08-21
    • 1970-01-01
    • 2022-08-07
    • 2022-09-27
    • 2019-01-08
    • 2019-11-06
    • 1970-01-01
    相关资源
    最近更新 更多