nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.77k stars 7.64k forks source link

createTestingModule does not work as expected when running tests via node:test #14130

Closed a-c-m closed 2 days ago

a-c-m commented 3 days ago

Is there an existing issue for this?

Current behavior

When running a simple test that uses createTestingModule, the dependencies are not injected, if the test is run using node:test, instead they are left undefined - so the test fails.

Similar code using jest works fine.

I've provided both for comparison.

Minimum reproduction code

https://codesandbox.io/p/sandbox/nestjs-createtestingmodule-nodetest-psylr8

Steps to reproduce

  1. npm install
  2. npm run test-jest
  3. npm run test-node

Expected behavior

The DI system should still work with node:test - or we need to warn people.

Package

Other package

No response

NestJS version

No response

Packages versions

{
  "dependencies": {
    "@nestjs/common": "^10.3.2",
    "@nestjs/config": "^3.3.0",
    "@nestjs/core": "^10.4.4",
    "@nestjs/platform-express": "^10.4.4",
    "@types/sinon": "^17.0.3",
    "reflect-metadata": "^0.2.1",
    "rxjs": "^7.8.1",
    "sinon": "^19.0.2",
    "tsx": "^4.19.2"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.4.5",
    "@nestjs/schematics": "^10.1.0",
    "@nestjs/testing": "^10.3.2",
    "@swc/cli": "^0.3.9",
    "@swc/core": "^1.4.0",
    "@types/express": "^4.17.21",
    "@types/jest": "^29.5.12",
    "@types/node": "^20.11.16",
    "@types/supertest": "^6.0.2",
    "@typescript-eslint/eslint-plugin": "^6.21.0",
    "@typescript-eslint/parser": "^6.21.0",
    "eslint": "^8.56.0",
    "eslint-config-prettier": "^9.1.0",
    "eslint-plugin-prettier": "^5.1.3",
    "jest": "^29.7.0",
    "prettier": "^3.2.5",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.4",
    "ts-jest": "^29.1.2",
    "ts-loader": "^9.5.1",
    "ts-node": "^10.9.2",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.3.3"
  }

Node.js version

No response

In which operating systems have you tested?

Other

Originally raised on discord - https://discord.com/channels/520622812742811698/1305581785991614625/1305581785991614625

micalevisk commented 3 days ago

image

Can't you try running tsc over those spec files and then just node --test? if this works, then it's not related with nestjs at all

a-c-m commented 2 days ago

Sorry I don't follow - jest works fine, it's node test which fails and I'm running tsc before the node test example?

kamilmysliwiec commented 2 days ago

Node's built-in TS transpilation doesn't support emitting metadata, hence, DI and anything metadata-related won't work as expected.

Thank you for taking the time to submit your report! From the looks of it, this could be better discussed on our Discord. If you haven't already, please join here and send a new post in the #⁠ 🐈 nestjs-help forum. Make sure to include a link to this issue, so you don't need to write it all again. We have a large community of helpful members, who will assist you in getting this to work.