繁体   English   中英

设置 Express 服务器时 NodeJs Testrunner 冻结/停止

[英]NodeJs Testrunner freezes / stops when setting up Express server

我将 Node v18 与实验性测试运行程序一起使用。 我使用 express 作为 http 集成测试的开发依赖项,它工作正常,但有一个测试冻结或停止测试运行程序(它不会继续)

我用的是 TS 但也可以用 JS 重现,测试文件HttpTests.js包含

import assert from 'assert/strict';
import express from 'express';
import test from 'node:test';

test('Http', async () => {
  const server = express();
    
  server.listen(3000);
  assert.ok(false);
});

使用 npm 脚本"test": "node --test $(find. -name '*Tests.js')"会破坏测试运行器。

任何想法有什么问题或缺失?


为什么我不使用默认执行 model

由于我使用的是 TS,因此我必须找到一种将 ts-node 与 testrunner 一起使用的方法。 您可以在这里找到更多信息

https://github.com/nodejs/node/issues/43675

所以目前我的 TS 项目正在使用这个npm 脚本,它工作正常


再生产

我创建了一个最小的复制存储库,有和没有 TypeScript

出于复制目的,运行mkdir reproduction && cd reproduction && npm init -y && npm install express 之后,使用包含如上所示内容的文件HttpTests.js创建一个测试目录。 package.json更改为

{
  "name": "reproduction",
  "type": "module",
  "scripts": {
    "test": "node --test $(find . -name '*Tests.js')"
  }
}

并运行脚本,testrunner 不应该完成。


测试运行器仍处于试验阶段

是的,我知道。 但是该项目中有许多测试工作得非常好。 一些示例代码

await t.test('subtest - saves data.', async () => {
    const expectedResult = {};
  
    const api = express();
    const port = await getRandomPort();
    const server = api
        .use(express.json())
        .post('/save', (request, response) => {
            response.json(expectedResult);
        })
        .listen(port);

    const httpDataProvider = new HttpDataProvider({ url: `http://localhost:${port}` });
    const actualResult = await httpDataProvider.saveSomething();

    assert.deepEqual(actualResult, expectedResult);

    server.close();
});

问题是您启动的异步活动( server.listen() )但在测试错误出现之前不停止(由assert.ok(false)引发的异常)。

如果由于相同的问题(不会调用actualResult server.close()实际结果不等于expectedResult结果,您的第二个测试用例也可能会停止。

一种解决方法是始终确保服务器最终关闭:

test('Http', async () => {
  const app    = express();
  const server = app.listen(3000);
  try {
    assert.ok(false);
  } finally {
    server.close();
  }
});

大多数测试框架提供“之前/之后”功能,可用于在测试之前和之后设置或拆除辅助对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM