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.69k stars 7.63k forks source link

seeing 'Cannot resolve dependency' issue only when use jest.mock('uuid', ... #8986

Closed oojikoo closed 2 years ago

oojikoo commented 2 years ago

Is there an existing issue for this?

Current behavior

using nestjs, nx, ogma when run nx test , I'm seeing this CleanShot 2022-01-21 at 08 38 19

withthis line commented, all tests pass...

apps/payment-svc/test/payment.spec.ts

jest.mock('uuid', () => ({ v4: () => '00000000-0000-0000-0000-000000000000' }));

Minimum reproduction code

https://github.com/oojikoo/ogma-jest

Steps to reproduce

  1. setup postgresql for testing

  2. update all db credential and info for testing

    MikroOrmModule.forRoot({
    ...  <- update these values in the project
    })
  3. yarn

  4. nx test payment-svc --verbose --detectOpenHandles

Expected behavior

even with uuid mocking, tests should be passed

Package

Other package

@ogma/nestjs-module,

NestJS version

7.1.0

Packages versions

{
  "name": "nest-ogma",
  "version": "1.0.0-alpha.1",
  "author": "Ryan <oojikoo@gmail.com> (https://github.com/oojikoo)",
  "license": "UNLICENSE",
  "private": true,
  "scripts": {
    "serve": "nx serve",
    "build": "nx build",
    "build:data-svc": "nx build data-svc --prod",
    "test": "nx test",
    "lint": "nx workspace-lint && nx lint",
    "affected:apps": "nx affected:apps",
    "affected:libs": "nx affected:libs",
    "affected:build": "nx affected:build",
    "affected:e2e": "nx affected:e2e",
    "affected:test": "nx affected:test",
    "affected:lint": "nx affected:lint",
    "affected:dep-graph": "nx affected:dep-graph",
    "affected": "nx affected",
    "format:write": "nx format:write",
    "format:lint": "nx affected:lint --fix",
    "format:check": "nx format:check",
    "data-svc:mikro-orm:gen-entity": "nx mikro-orm data-svc --args='generate-entities --save --path=./src/entities'",
    "data-svc:mikro-orm:migration:diff": "nx mikro-orm data-svc --args='migration:create'",
    "data-svc:mikro-orm:migration:empty": "nx mikro-orm data-svc --args='migration:create -b'",
    "data-svc:mikro-orm:migration:up": "nx mikro-orm data-svc --args='migration:up'",
    "data-svc:mikro-orm:migration:down": "nx mikro-orm data-svc --args='migration:down'"
  },
  "dependencies": {
    "@automapper/classes": "^7.1.0",
    "@automapper/core": "^7.1.0",
    "@automapper/nestjs": "^7.1.0",
    "@mikro-orm/cli": "^4.5.9",
    "@mikro-orm/core": "^4.5.9",
    "@mikro-orm/knex": "^4.5.9",
    "@mikro-orm/migrations": "^4.5.9",
    "@mikro-orm/nestjs": "^4.3.1",
    "@mikro-orm/postgresql": "^4.5.9",
    "@mikro-orm/reflection": "^4.5.9",
    "@mikro-orm/sql-highlighter": "^1.0.1",
    "@nestjs/common": "^8.2.4",
    "@nestjs/config": "^1.1.6",
    "@nestjs/core": "^8.2.4",
    "@nestjs/mapped-types": "^1.0.1",
    "@nestjs/microservices": "^8.2.4",
    "@nestjs/platform-express": "^8.2.4",
    "@nestjs/schedule": "^1.0.2",
    "@ogma/nestjs-module": "^3.1.0",
    "@ogma/platform-nats": "^3.0.1",
    "@sinonjs/fake-timers": "^8.1.0",
    "@types/cron": "^1.7.3",
    "@types/faker": "^5.5.9",
    "@types/sinonjs__fake-timers": "^8.1.1",
    "@types/supertest": "^2.0.11",
    "@types/uuid": "^8.3.3",
    "axios": "^0.24.0",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "dayjs": "^1.10.7",
    "faker": "^5.5.3",
    "ioredis": "^4.28.0",
    "lodash": "^4.17.21",
    "mockdate": "^3.0.5",
    "moleculer": "0.14.7",
    "mysql2": "^2.3.3",
    "nanoid": "^3.1.30",
    "nats": "^2.3.0",
    "nestjs-moleculer": "^1.0.5",
    "pg": "^8.7.1",
    "redis": "^4.0.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "~7.4.0",
    "supertest": "^6.2.1",
    "tslib": "^2.0.0",
    "uuid": "^8.3.2"
  },
  "devDependencies": {
    "@alexy4744/nx-mikro-orm-cli": "1.0.4",
    "@automapper/types": "6.3.1",
    "@nestjs/schematics": "8.0.5",
    "@nestjs/testing": "8.2.4",
    "@nrwl/cli": "13.1.3",
    "@nrwl/eslint-plugin-nx": "13.3.11",
    "@nrwl/jest": "13.3.11",
    "@nrwl/linter": "13.3.11",
    "@nrwl/nest": "13.3.11",
    "@nrwl/node": "13.3.11",
    "@nrwl/tao": "13.3.11",
    "@nrwl/workspace": "13.3.11",
    "@types/jest": "27.0.3",
    "@types/node": "17.0.2",
    "@typescript-eslint/eslint-plugin": "5.8.0",
    "@typescript-eslint/parser": "5.8.0",
    "eslint": "8.5.0",
    "eslint-config-prettier": "8.3.0",
    "eslint-plugin-import": "^2.25.3",
    "eslint-plugin-prettier": "^4.0.0",
    "eslint-plugin-simple-import-sort": "^7.0.0",
    "husky": "^7.0.4",
    "jest": "27.4.5",
    "lint-staged": "^12.1.3",
    "prettier": "^2.5.1",
    "ts-jest": "27.1.2",
    "ts-morph": "^13.0.2",
    "typescript": "4.5.4"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.{js,json,css,scss,md,ts,html,graphql}": [
      "yarn format --uncommitted"
    ]
  },
  "mikro-orm": {
    "useTsNode": true,
    "configPaths": [
      "./mikro-orm.config.ts",
      "./dist/mikro-orm.config.js"
    ]
  }
}

Node.js version

v16.13.1

In which operating systems have you tested?

Other

No response

jmcdo29 commented 2 years ago

Okay, I was able to recreate this in a truly minimum fashion, no deps other than Nest. You can find the repo here and run pnpm i && pnpm test:e2e (yarn and npm of course work too).

I believe the issue comes from the ModulesContainer class where this.applicationId = v4(). If every module container gets the same application id, (due to mocking the return value of v4()) then it could be problematic. I don't know if there's anything Nest could do about this other than give a warning in the docs about not making a blanket mock for uuid.v4() or possibly trying to keep a track of all application ids and throwing an error if a duplicate comes up

oojikoo commented 2 years ago

thanks! I should probably set default uuid value for entity id from database layer, not from application layer.

micalevisk commented 2 years ago

also, using 3rd-party libs like this

image

make things a bit harder to test :/

kamilmysliwiec commented 2 years ago

Nest internally relies on the uuid package so if you mock it to return a constant value it will surely have a negative impact on the framework's behavior. If you feel like we should add a hint about that in the docs, please, create a PR to the docs! Contributions are more than welcome