EntityMetadataNotFoundError:未找到express,typeorm,jest的“用户”元数据

8hhllhi2  于 2023-06-20  发布在  Jest
关注(0)|答案(1)|浏览(140)

auth.model.ts

import { AppDataSource } from "../app-data-source";
import { User } from "../entity";

class AuthModel {
  static register = async (userDTO: RegisterUserDTO) => {
    try {
      const userRepo = AppDataSource.getRepository(User);
      const user = userRepo.create(userDTO);
      await userRepo.save(user);
    } catch (err: any) {
      console.error(err);
      throw {
        status: 500,
        message: err.message,
      };
    }
  };
}

export default AuthModel;

app-data-source.ts

import { DataSource } from "typeorm";
import config from "./config";
import { User } from "./entity";

export const AppDataSource = new DataSource({
  type: config.database.type,
  host: config.database.host,
  port: config.database.port,
  username: config.database.username,
  password: config.database.password,
  database: config.database.name,
  entities: [User],
  synchronize: true,
});

user.entity.ts

import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, Generated } from "typeorm";

@Entity({ name: "user" })
class User {
  @PrimaryGeneratedColumn("uuid", { name: "id" })
  userId: string;

  @Column({ type: "varchar", length: 100, nullable: false, unique: true })
  email: string;

  @Column({ type: "varchar", length: 255, nullable: false })
  password: string;

  @Column({ type: "varchar", length: 255, default: "", nullable: true })
  introduce: string;

  @Column({ type: "varchar", length: 255, name: "profile_img", default: "", nullable: true })
  profileImage: string;

  @CreateDateColumn({ type: "datetime", name: "created_at_date", nullable: true })
  createdAt: Date;

  @Column({ type: "varchar", length: 100, nullable: false, unique: true })
  nickname: string;

  @Column({ type: "boolean", name: "is_auth_flag", default: false, nullable: true })
  isAuth: boolean;
}

export default User;

我用Express + TypeORM + Mysql做API服务器
如果我用 Postman 发送邮件,那么成功Postman Success
但我用jest测试,然后抛出EntityMetadataNotFoundError:找不到“用户”的元数据。enter image description here
Postman 是成功的但是用jest测试是抛出错误
请帮帮我

  • 更改app-data-source.ts上的实体属性
slhcrj9b

slhcrj9b1#

您需要设置ConfigurationProvider。你要经过的地方:

private generateConfiguration(environment? : Config) : Config {
    return {
      db : {
        type: 'mysql',
        entities: [entity_path + '/../**/*.entity.ts']
    }
}

相关问题