首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Typeorm装饰器不是函数

Typeorm装饰器不是函数
EN

Stack Overflow用户
提问于 2020-04-16 22:52:23
回答 2查看 2.4K关注 0票数 9

我有以下要测试的控制器:

代码语言:javascript
运行
复制
class Album {
  public static getAlbums(): Promise<AlbumModel[]> {
    return getRepository(AlbumModel).find({ relations: ['pictures'] });
  }
}

它链接到一个模型,我使用了带有装饰器的typeorm。这就是我使用Jest时的问题所在

代码语言:javascript
运行
复制
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[];
}

为了测试这个控制器,我使用ts-jest。这是我的单元测试:

代码语言:javascript
运行
复制
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();
  });
});

当我运行我的测试时,我得到以下错误:

代码语言:javascript
运行
复制
 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;

它有什么问题?

这是我的package.json的一部分

代码语言:javascript
运行
复制
  "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"
  }
EN

Stack Overflow用户

发布于 2021-10-10 17:38:41

我喜欢让尽可能多的typeorm自动锁定,所以我选择了一种不同的方法。我模拟所有的typeorm,然后只覆盖我正在使用的特定装饰器:

代码语言:javascript
运行
复制
jest.mock('typeorm')
mocked(EntityRepository).mockImplementation(() => jest.fn())
mocked(Entity).mockImplementation(() => jest.fn())
mocked(Column).mockImplementation(() => jest.fn())
mocked(PrimaryGeneratedColumn).mockImplementation(() => jest.fn())
mocked(CreateDateColumn).mockImplementation(() => jest.fn())
mocked(UpdateDateColumn).mockImplementation(() => jest.fn())
mocked(DeleteDateColumn).mockImplementation(() => jest.fn())
mocked(OneToMany).mockImplementation(() => jest.fn())
mocked(ManyToOne).mockImplementation(() => jest.fn())
mocked(TableInheritance).mockImplementation(() => jest.fn())
mocked(ChildEntity).mockImplementation(() => jest.fn())

然后,当我想模拟像.find()这样的特定方法时,我会模拟EntityManager的特定方法

代码语言:javascript
运行
复制
mocked(EntityManager.prototype.getCustomRepository).mockReturnValue(repository);
mocked(EntityManager.prototype.transaction as transactionOp).mockImplementation(async (runInTransaction) => runInTransaction(entityManager));
mocked(EntityManager.prototype.findOne).mockResolvedValue({
  id: 1,
  name: 'Cool Album',
});
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61253282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档