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"
}
In TypeScript, decorators are functions that can be attached to classes and their members, such as methods and properties. Let’s look at some examples. Don't miss a moment with The Replay, a curated newsletter from LogRocket When you attach a function to a class as a decorator, you’ll receive the class constructor as the first parameter.
Decorators A Decorator is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. Decorators use the form @expression, where expression must evaluate to a function that will be called at runtime with information about the decorated declaration.
The expression for the class decorator will be called as a function at runtime, with the constructor of the decorated class as its only argument. If the class decorator returns a value, it will replace the class declaration with the provided constructor function.
This combination allows developers to use the full JavaScript ecosystem and language features, while also adding optional static type-checking, enums, classes, and interfaces on top of it. One of those extra features is decorator support.
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: () => {},
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With