jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.21k stars 6.46k forks source link

Dynamic loading issue (same code works without Jest) #10848

Closed Javarome closed 1 year ago

Javarome commented 3 years ago

πŸ› Bug Report

Issue Description

I have a working NestJS server that performs some TypeORM repository.insert() command. However when running the same operation from a Jest test (using @nestjs/testing's Test.createTestingModule(...), the infamous Right-hand side of 'instanceof' is not an object appears.

Looking in more details, it appears that this is due to some dynamic loading occurring in TypeORM's QueryBuilder:

// loading it dynamically because of circular issue
const InsertQueryBuilderCls = require("./InsertQueryBuilder").InsertQueryBuilder;

That line succeeds when running the NestJS server but fails when running the Jest test. More specifically:

Expected Behavior

Same behavior when launching the server and the test.

Actual Behavior

Exception is raised:

TypeError: Right-hand side of 'instanceof' is not an object
    at SelectQueryBuilder.Object.<anonymous>.QueryBuilder.insert (/xxx/src/query-builder/QueryBuilder.ts:173:17)
    at EntityManager.<anonymous> (/xxx/src/entity-manager/EntityManager.ts:466:14)
    at step (/xxx/node_modules/typeorm/node_modules/tslib/tslib.js:141:27)
    at Object.next (/xxx/node_modules/typeorm/node_modules/tslib/tslib.js:122:57)
    at /xxx/node_modules/typeorm/node_modules/tslib/tslib.js:115:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/xxx/node_modules/typeorm/node_modules/tslib/tslib.js:111:16)
    at EntityManager.Object.<anonymous>.EntityManager.insert (/xxx/node_modules/typeorm/entity-manager/EntityManager.js:286:24)
    at Repository.Object.<anonymous>.Repository.insert (/xxx/src/repository/Repository.ts:231:29)
    at MyCode.myMethod (/xxx/src/MyClass.ts)

Steps to Reproduce

  1. Define a NestJS test using a testing module with TypeOrmModule.forRoot(options) and TypeOrmModule.forFeature([MyEntity])
  2. call your service that has @InjectRepository(StoredPrediction) repository constructor parameter, then attempts to repository.insert(myEntity)

My Environment

My Jest configuration is as follows:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  moduleDirectories: ['node_modules', 'src']
}

as my Typescript sources are under a src directory under the project root. Those path issues are also be related to my tsconfig.json, which contains:

{
  "compilerOptions": {
  "module": "commonjs",
  "allowSyntheticDefaultImports": true,
  "target": "es2019",
  "baseUrl": "./src",
  "outDir": "./dist",
  "incremental": true,
  "lib": [
    "es2019"
  ]
}
}
Dependency Version
Operating System MacOS Big Sur
Node.js version v13.14.0
Typescript version v3.9.7
TypeORM version v0.2.29
TS-Jest v26.4.4
Jest v26.6.3

Additional Context

My directory structure is:

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] commented 1 year ago

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.