Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testrunner freezes / stops when setting up Express server #44189

Closed
matthiashermsen opened this issue Aug 9, 2022 · 2 comments
Closed

Testrunner freezes / stops when setting up Express server #44189

matthiashermsen opened this issue Aug 9, 2022 · 2 comments
Assignees
Labels
test_runner Issues and PRs related to the test runner subsystem.

Comments

@matthiashermsen
Copy link

Version

18.7.0

Platform

Linux pop-os 5.18.10-76051810-generic #202207071639165910843122.04~c9172fb SMP PREEMPT_DYNAMIC Fri J x86_64 x86_64 x86_64 GNU/Linux

Subsystem

node:test

What steps will reproduce the bug?

  • Create a new project via mkdir reproduction && cd reproduction && npm init -y && npm install express
  • Add ./test/HttpTests.js containing
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);
});
  • Replace the content of the package.json with
{
  "name": "reproduction",
  "type": "module",
  "scripts": {
    "test": "node --test $(find . -name '*Tests.js')"
  }
}
  • Run the tests via npm run test

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

The testrunner should finish and exit with 1 since the assertion failed.

What do you see instead?

The testrunner freezes

Additional information

The test script is not using the default execution model because my project is using TypeScript and based on #43675 I had to find a way to use ts-node while using the builtin testrunner.

I created two reproduction repositories

@MoLow MoLow added the test_runner Issues and PRs related to the test runner subsystem. label Aug 9, 2022
@MoLow MoLow self-assigned this Aug 9, 2022
@MoLow
Copy link
Member

MoLow commented Aug 9, 2022

@matthiashermsen the test will never end since the server keeps the event loop waiting for requests.
you can run node test/HttpTests.js and see there is an output, but the process won't close:

TAP version 13
# Subtest: Http
not ok 1 - Http
  ---
  duration_ms: 0.012851733
  failureType: 'testCodeFailure'
  error: 'false == true'
  code: 'ERR_ASSERTION'
  stack: |-
    TestContext.<anonymous> (file:///Users/mosheatlow/tmp/express.test.mjs:10:10)
    Test.runInAsyncScope (node:async_hooks:203:9)
    Test.run (node:internal/test_runner/test:417:25)
    Test.start (node:internal/test_runner/test:367:17)
    Test.test (node:internal/test_runner/harness:149:18)
    file:///Users/mosheatlow/tmp/express.test.mjs:5:1
    ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    async Promise.all (index 0)
    async ESMLoader.import (node:internal/modules/esm/loader:541:24)
    async loadESM (node:internal/process/esm_loader:91:5)
  ...
^C1..1
# tests 1
# pass 0
# fail 1
# cancelled 0
# skipped 0
# todo 0
# duration_ms 5.064054492

there is an effort to add better output that will stream the results as they pass (instead of waiting for the entire file to run), see #43344

anyhow, adding server.close() will solve the issue

@matthiashermsen
Copy link
Author

@MoLow thanks a lot for your reply. I tried that but that didn't help ( I used this code https://github.com/matthiashermsen/reproduce-broken-test-js/blob/main/test/HttpTests.js#L9 )

❯ node test/HttpTests.js
(node:24632) ExperimentalWarning: The test runner is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
TAP version 13
not ok 1 - Http
  ---
  duration_ms: 0.00220323
  failureType: 'testCodeFailure'
  error: 'server.close is not a function'
  code: 'ERR_TEST_FAILURE'
  stack: |-
    TestContext.<anonymous> (file:///home/me/reproduce-broken-test-js/test/HttpTests.js:9:10)
    Test.runInAsyncScope (node:async_hooks:202:9)
    Test.run (node:internal/test_runner/test:340:20)
    Test.start (node:internal/test_runner/test:292:17)
    Test.test (node:internal/test_runner/harness:126:18)
    file:///home/me/reproduce-broken-test-js/test/HttpTests.js:5:1
    ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    async Promise.all (index 0)
    async ESMLoader.import (node:internal/modules/esm/loader:409:24)
    async loadESM (node:internal/process/esm_loader:85:5)
  ...
^C
❯ 

But I was wrong, the correct code should be

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

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

Now the runner doesn't freeze anymore :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test_runner Issues and PRs related to the test runner subsystem.
Projects
None yet
Development

No branches or pull requests

2 participants