Search code examples
typescriptjestjstypeormts-jest

Typeorm decorator is not a function


I have the following controller that I want to test :

class Album {
  public static getAlbums(): Promise<AlbumModel[]> {
    return getRepository(AlbumModel).find({ relations: ['pictures'] });
  }
}

Which is linked to a model, i'm using typeorm with decoractors. This is where the problem come from when I use Jest

import { Entity, PrimaryGeneratedColumn, Column, JoinTable, ManyToMany } from 'typeorm';
import { PicturesModel } from '../pictures/pictures.model';

@Entity({
  name: 'T_ALBUM_AL',
  synchronize: true,
})
export class AlbumModel {
  @PrimaryGeneratedColumn({
    name: 'AL_id',
  })
  id: number;

  @Column({
    name: 'AL_name',
  })
  name: string;

  @ManyToMany(() => PicturesModel, (picture: PicturesModel) => picture.id)
  @JoinTable({
    name: 'TJ_PICTURE_ALBUM_PA',
    joinColumn: {
      name: 'AL_id',
      referencedColumnName: 'id',
    },
    inverseJoinColumn: {
      name: 'PI_id',
      referencedColumnName: 'id',
    },
  })
  pictures: PicturesModel[];
}

To test this controller, i'm using ts-jest. This is my unit test :

import Album from './album.controller';

const getRepMock = jest.fn(() => ({
  find: jest.fn().mockImplementation(),
}));

jest.mock('typeorm', () => ({
  getRepository: getRepMock,
}));

describe('Album', () => {
  it('Should work', () => {
    Album.getAlbums();
    expect(getRepMock).toBeCalled();
  });
});

When I run my test, I'm getting the following error :

 FAIL  src/api/v1/album/album.test.ts
  ● Test suite failed to run

    TypeError: typeorm_1.PrimaryGeneratedColumn is not a function

       6 | })
       7 | export class PicturesModel {
    >  8 |   @PrimaryGeneratedColumn({
         |    ^
       9 |     name: 'PI_id',
      10 |   })
      11 |   id: number;

What is wrong with it ?

This is a part of my package.json

  "jest": {
    "preset": "ts-jest",
    "testEnvironment": "node",
    "coveragePathIgnorePatterns": [
      "/node_modules/"
    ]
  },
  "dependencies": {
    "@types/dotenv": "^8.2.0",
    "body-parser": "^1.19.0",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "express-boom": "^3.0.0",
    "glob": "^7.1.6",
    "morgan": "^1.10.0",
    "mysql": "^2.14.1",
    "pg": "^7.18.2",
    "reflect-metadata": "^0.1.10",
    "ts-jest": "^25.3.1",
    "typeorm": "0.2.24"
  },
  "devDependencies": {
    "@types/express": "^4.17.3",
    "@types/express-boom": "^3.0.0",
    "@types/glob": "^7.1.1",
    "@types/jest": "^25.2.1",
    "@types/morgan": "^1.9.0",
    "@types/node": "^8.0.29",
    "@types/supertest": "^2.0.8",
    "@typescript-eslint/eslint-plugin": "^2.24.0",
    "@typescript-eslint/parser": "^2.24.0",
    "eslint": "^6.8.0",
    "eslint-config-airbnb-base": "^14.1.0",
    "eslint-import-resolver-alias": "^1.1.2",
    "eslint-plugin-import": "^2.20.1",
    "eslint-plugin-module-resolver": "^0.16.0",
    "jest": "^25.3.0",
    "nodemon": "^2.0.2",
    "supertest": "^4.0.2",
    "ts-node": "3.3.0",
    "typescript": "3.3.3333",
    "typescript-eslint": "^0.0.1-alpha.0"
  }

Solution

  • You need to mock out typeorm, like this:

    import { Repository, SelectQueryBuilder } from 'typeorm';
    import { mock } from 'jest-mock-extended';
    
    const repositoryMock = mock<Repository<any>>();
    const qbuilderMock = mock<SelectQueryBuilder<any>>();
    
    jest.mock('typeorm', () => {
        qbuilderMock.where.mockReturnThis();
        qbuilderMock.select.mockReturnThis();
        repositoryMock.createQueryBuilder.mockReturnValue(qbuilderMock);
    
        return {
            getRepository: () => repositoryMock,
    
            BaseEntity: class Mock {},
            ObjectType: () => {},
            Entity: () => {},
            InputType: () => {},
            Index: () => {},
            PrimaryGeneratedColumn: () => {},
            Column: () => {},
            CreateDateColumn: () => {},
            UpdateDateColumn: () => {},
            OneToMany: () => {},
            ManyToOne: () => {},
        }
    })