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.11k stars 7.56k forks source link

Extra options are set only to default values when using `registerAsync` #13372

Closed snigdha920 closed 6 months ago

snigdha920 commented 6 months ago

Is there an existing issue for this?

Current behavior

For a dynamic module built using the configurable module builder:

Minimum reproduction code

https://github.com/snigdha920/nestjs-extra-options-bug

Steps to reproduce

Steps to reproduce

  1. Install dependencies with pnpm i
  2. Run pnpm start:dev in the root
  3. We see that the AuthorModule is not initialised, even though we pass authorEnabled to true in the BookModule.registerAsync configuration:
image
  1. Comment out the registerAsync configuration, and uncomment the register configuration in app.module.ts, then we see that the AuthorModule is initialised:
image

Expected behavior

The AuthorModule should be initialised when we pass authorEnabled to true in the BookModule.registerAsync configuration

What I'm trying to do is import modules depending on config passed in. So in practice I use a ConfigService to pass the right config to the module:

 JobsModule.registerAsync({
  inject: [ConfigService],
  useFactory: (configService: ConfigService) => {
    const billingEnabled = configService.getOrThrow<boolean>('billingEnabled');
    return {
      billingEnabled,
    };
  },
}),

But the extra options are not being set correctly when using registerAsync, and I can only use registerAsync because I want to use my ConfigService.

Package

Other package

No response

NestJS version

10.3.7

Packages versions

{
  "name": "nestjs-di",
  "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": "10.3.7",
    "@nestjs/core": "10.3.7",
    "@nestjs/platform-express": "10.3.7"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "29.2.4",
    "@types/node": "18.11.18",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "29.3.1",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "29.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.1",
    "typescript": "^4.7.4"
  },
  "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

20.11.1

In which operating systems have you tested?

Other

No response

jmcdo29 commented 6 months ago

authorEnabled is not an option of the BookModule, but rather an extra of it. It lives at the same level as useFactory or useClass when using registerAsync, but looks like it lives at the same level as the options when using regular register. This is not a bug, but the way the system is designed. image

snigdha920 commented 6 months ago

authorEnabled is not an option of the BookModule, but rather an extra of it. It lives at the same level as useFactory or useClass when using registerAsync, but looks like it lives at the same level as the options when using regular register. This is not a bug, but the way the system is designed. image

Clear, thx! I think we should add an example in the documentation, or a note, because from the documentation it looks like it's an option we can pass from registerAsync too. If you agree, I can open a PR.