question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

“Right-hand side of 'instanceof' is not an object” when testing NestJS + TypeORM using Jest

See original GitHub issue

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:

  • in the NestJS server: QueryBuilder require("./InsertQueryBuilder") returns an ES module with a InsertQueryBuilder in it. When setting a breakpoint here, strangely the debugged file appears located at src/query-builder/QueryBuilder.ts (which is non-existent, and should rather be node_modules/typeorm/query-builder/QueryBuilder.js), but this succeeds.
  • in the Jest test: require("./InsertQueryBuilder") return an empty object, and so InsertQueryBuilder is undefined which not an object indeed. The debugged file is as expected node_modules/typeorm/query-builder/QueryBuilder.js, but this fails.

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:

  • src
  • node_modules
  • jest.config.js
  • tsconfig.json

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don’t know how to start. I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:18
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

17reactions
hkimanicommented, Aug 6, 2021

For those facing this issue on Mongo DB. @davidzwa and @spmsupun are right, the new version introduces breaking changes.

The following worked for me (Downgraded my mongodb driver): npm install mongodb@3.5.7

12reactions
spmsupuncommented, Jul 27, 2021

If anyone having this with mongo check your mongodb package version, type orm still uses 3.6 and in mongodb change id name ObjectID to ObjectId. so the issue happens here:

https://github.com/typeorm/typeorm/blob/master/src/entity-manager/MongoEntityManager.ts#L175

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found