“Right-hand side of 'instanceof' is not an object” when testing NestJS + TypeORM using Jest
See original GitHub issueIssue 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 aInsertQueryBuilder
in it. When setting a breakpoint here, strangely the debugged file appears located atsrc/query-builder/QueryBuilder.ts
(which is non-existent, and should rather benode_modules/typeorm/query-builder/QueryBuilder.js
), but this succeeds. - in the Jest test:
require("./InsertQueryBuilder")
return an empty object, and soInsertQueryBuilder
isundefined
which not an object indeed. The debugged file is as expectednode_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
- Define a NestJS test using a testing module with
TypeOrmModule.forRoot(options)
andTypeOrmModule.forFeature([MyEntity])
- call your service that has
@InjectRepository(StoredPrediction) repository
constructor parameter, then attempts torepository.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:
- Created 3 years ago
- Reactions:18
- Comments:10 (1 by maintainers)
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
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