nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇
https://nestjs.com
MIT License
1.91k stars 206 forks source link

TypeError: Right-hand side of 'instanceof' is not an object when use absolute path in nestjs #321

Closed chungchi300 closed 4 years ago

chungchi300 commented 4 years ago

Bug Report

Current behavior

  ● Test suite failed to run

    TypeError: Right-hand side of 'instanceof' is not an object

       7 | export class PhotoService {
       8 |   constructor(
    >  9 |     @InjectRepository(Photo)
         |      ^
      10 |     private readonly photoRepository: Repository<Photo>,
      11 |   ) {}
      12 | 

      at Object.getRepositoryToken (node_modules/@nestjs/typeorm/dist/common/typeorm.utils.js:16:26)
      at Object.<anonymous>.exports.InjectRepository (node_modules/@nestjs/typeorm/dist/common/typeorm.decorators.js:6:130)
      at Object.<anonymous> (src/typeormAndJest/exp/photo/photo.service.ts:9:6)
      at Object.<anonymous> (src/typeormAndJest/exp/photo/photo.module.ts:2:1)

Input Code

https://github.com/chungchi300/typescript-starter

//at src/typeormAndJest/typeorm.spec.ts
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';

import { AsyncApplicationModule } from 'src/typeormAndJest/exp/app-async.module';
import { author } from 'src/deep/deep2/util';
describe('TypeOrm', () => {
  let server;
  let app: INestApplication;

  beforeEach(async () => {
    const module = await Test.createTestingModule({
      imports: [AsyncApplicationModule],
    }).compile();

    app = module.createNestApplication();
    server = app.getHttpServer();
    await app.init();
  });

  it(`should return created entity`, () => {
    expect(author()).toBe('jeff chung');
    // return request(server)
    //   .post('/photo')
    //   .expect(201, { name: 'Nest', description: 'Is great!', views: 6000 });
  });

  afterEach(async () => {
    // await app.close();
  });
});

Expected behavior

The test should run successfully

Possible Solution

Environment

services:
  db:
    #build: https://github.com/guillaumeprevost/docker-mysql-utf8mb4
    image: mysql:5.7
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --max_allowed_packet=32505856
    environment:
      - MYSQL_DATABASE=nestBoilerplate
      - MYSQL_ROOT_PASSWORD=dockerPassword
    ports:
      - '3306:3306'
v1d3rm3 commented 4 years ago

Not only when the path is absolute. Same error, when relative too.

chungchi300 commented 4 years ago

@victorschinaider is it a problem comes from the version update of nestjs ? Should we run in a lower version of nestjs @kamilmysliwiec ?

kamilmysliwiec commented 4 years ago

This issue is completely unrelated to NestJS.

The TypeError: Right-hand side of 'instanceof' error occurs when you try to call instanceof with the undefined value. This means, in your example (@chungchi300) Photo class is represented as undefined for some reason. Just stop using absolute paths (which are considered as bad practice either way) and avoid using barrel files (index.ts) where your IDE can mess up with the imports order.

chungchi300 commented 4 years ago

I can understand avoid using barrel files, but in the typescript-starter, there is a "baseUrl": "./"in tsconfig.json, does it mean that we can use absolute path?If I use relative path, how can I avoid relative path hell? https://medium.com/beqode/absolute-vs-relative-import-paths-nodejs-1e4efa65a7bb

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  },
  "exclude": ["node_modules", "dist"]
}
kamilmysliwiec commented 4 years ago

@chungchi300 it seems that you just have an invalid jest configuration. By setting this:

"moduleDirectories": [
      "./",
      "node_modules"
],

jest is incorrectly mapping external modules to non-existing directories. Hence, all the functions/types/classes from the typeorm library = undefined. I have removed this (+ enabled allowJs in the tsconfig.json) since you use JS files and it works, got rid of absolute paths, and it works.

This issue is not specifically related to NestJS, but rather to the way how jest resolves modules. You should report future issues like this one in the jest repository.