小编TLd*_*TLd的帖子

Typeorm 装饰器不是函数

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

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

这与模型相关联,我正在使用带有装饰器的 typeorm。这就是我使用 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: …
Run Code Online (Sandbox Code Playgroud)

typescript jestjs typeorm ts-jest

7
推荐指数
1
解决办法
1862
查看次数

Typescript Jest 模拟:xx.default 不是构造函数:无法实例化模拟

我在尝试模拟类和构造函数时遇到问题。

我有一个要测试的 App.ts 类:

class App {
  public server: Express;

  constructor() {
    this.server = new Express();
    this.server.init();
  }
}

export default App;
Run Code Online (Sandbox Code Playgroud)

对于测试场景 -> 一旦我实例化一个 App 类,它应该: - 确保创建了类 Express 的新实例 - 确保调用了 init 函数

所以我有我的 App.test 文件:

import App from '../App';

let mockedExp: jest.Mock;
jest.mock('../Express', () => {
  return {
    default: jest.fn().mockImplementation(() => {
      return {
        init: mockedExp,
      };
    }),
  };
});

describe('App', () => {
  beforeEach(() => {
    mockedExp = jest.fn().mockImplementation();
    mockedExp.mockClear();
  });

  it('Should call init from express …
Run Code Online (Sandbox Code Playgroud)

typescript jestjs ts-jest

1
推荐指数
1
解决办法
1803
查看次数

标签 统计

jestjs ×2

ts-jest ×2

typescript ×2

typeorm ×1