Skip to content

Nondeterministic behavior when running e2e tests on freshly generated nest project #15239

@josephjunker

Description

@josephjunker

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When repeatedly sending requests with supertest the behavior of a freshly-scaffolded nest server becomes nondeterministic.

I've created a new Nest repo using the CLI, and replaced the single test inside test/app.e2e-spec.ts with the following contents:

  it('/ (GET)', async () => {
    for (let i = 0; i < 5000; i++) {
      await request(app.getHttpServer())
        .get('/')
        .expect(200)
        .expect('Hello World!');
    }
  }, 1000000);

(This is just the same test that's generated by default, only run in a loop 5000 times with a higher Jest timeout set)

When I run these tests, around 1/2 - 1/10th of the time the tests will fail. So far I have seen the following failures:

  ● AppController (e2e) › / (GET)

    expected 200 "OK", got 404 "Not Found"

      21 |       await request(app.getHttpServer())
      22 |         .get('/')
    > 23 |         .expect(200)
         |          ^
      24 |         .expect('Hello World!');
      25 |     }
      26 |   }, 1000000);

      at Object.<anonymous> (app.e2e-spec.ts:23:10)
      ----
      at Test._assertStatus (../node_modules/supertest/lib/test.js:267:14)
      at ../node_modules/supertest/lib/test.js:323:13
      at Test._assertFunction (../node_modules/supertest/lib/test.js:300:13)
      at Test.assert (../node_modules/supertest/lib/test.js:179:23)
      at Server.localAssert (../node_modules/supertest/lib/test.js:135:14)
  ● AppController (e2e) › / (GET)

    expected 200 "OK", got 405 "Method Not Allowed"

      21 |       await request(app.getHttpServer())
      22 |         .get('/')
    > 23 |         .expect(200)
         |          ^
      24 |         .expect('Hello World!');
      25 |     }
      26 |   }, 1000000);

      at Object.<anonymous> (app.e2e-spec.ts:23:10)
      ----
      at Test._assertStatus (../node_modules/supertest/lib/test.js:267:14)
      at ../node_modules/supertest/lib/test.js:323:13
      at Test._assertFunction (../node_modules/supertest/lib/test.js:300:13)
      at Test.assert (../node_modules/supertest/lib/test.js:179:23)
      at Server.localAssert (../node_modules/supertest/lib/test.js:135:14)
 FAIL  test/app.e2e-spec.ts
  AppController (e2e)
    ✕ / (GET) (3106 ms)

  ● AppController (e2e) › / (GET)

    Parse Error: Expected HTTP/

The last one is the weirdest-- when I've gone in with a debugger I've seen this full error:

    Error: Parse Error: Expected HTTP/
        at Socket.socketOnData (node:_http_client:558:22)
        at Socket.emit (node:events:518:28)
        at addChunk (node:internal/streams/readable:561:12)
        at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
        at Socket.Readable.push (node:internal/streams/readable:392:5)
        at TCP.onStreamRead (node:internal/stream_base_commons:189:23) {
      bytesParsed: 0,
      code: 'HPE_INVALID_CONSTANT',
      reason: 'Expected HTTP/',
      rawPacket: <Buffer 00 00 18 04 00 00 00 00 00 00 04 00 40 00 00 00 05 00 40 00 00 00 06 00 00 40 00 fe 03 00 00 00 01 00 00 04 08 00 00 00 00 00 00 3f 00 01>,
      response: undefined
    }

When I captured the error in a debugger that rawPacket field contained the value SSH-2.0-OpenSSH_9.9.

Minimum reproduction code

https://github.com/josephjunker/nest-scaffold-bug-repro

Steps to reproduce

  1. nest new minimal-repro
  2. cd minimal-repro
  3. Replace contents of test/app.e2e-spec.ts with code given above
  4. run npm run test:e2e a few times. (it may be helpful to do while npm run test:e2e; do; done and let it run for a while)

Expected behavior

Tests should always pass.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

11.1.2

Packages versions

[System Information]
OS Version     : macOS24.5.0
NodeJS Version : v22.15.1
NPM Version    : 10.9.2 

[Nest CLI]
Nest CLI Version : 11.0.7 

[Nest Platform Information
platform-express version : 11.1.2
schematics version       : 11.0.5
testing version          : 11.1.2
common version           : 11.1.2
core version             : 11.1.2
cli version              : 11.0.7

package.json:

{
  "name": "minimal-repro",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^11.0.1",
    "@nestjs/core": "^11.0.1",
    "@nestjs/platform-express": "^11.0.1",
    "reflect-metadata": "^0.2.2",
    "rxjs": "^7.8.1"
  },
  "devDependencies": {
    "@eslint/eslintrc": "^3.2.0",
    "@eslint/js": "^9.18.0",
    "@nestjs/cli": "^11.0.0",
    "@nestjs/schematics": "^11.0.0",
    "@nestjs/testing": "^11.0.1",
    "@swc/cli": "^0.6.0",
    "@swc/core": "^1.10.7",
    "@types/express": "^5.0.0",
    "@types/jest": "^29.5.14",
    "@types/node": "^22.10.7",
    "@types/supertest": "^6.0.2",
    "eslint": "^9.18.0",
    "eslint-config-prettier": "^10.0.1",
    "eslint-plugin-prettier": "^5.2.2",
    "globals": "^16.0.0",
    "jest": "^29.7.0",
    "prettier": "^3.4.2",
    "source-map-support": "^0.5.21",
    "supertest": "^7.0.0",
    "ts-jest": "^29.2.5",
    "ts-loader": "^9.5.2",
    "ts-node": "^10.9.2",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.7.3",
    "typescript-eslint": "^8.20.0"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

v22.15.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue has not been looked into

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions