如何使用 prisma 处理笑话测试中的枚举值?组[]不可分配给组

问题描述 投票:0回答:2

我的

prisma postgresql
架构示例

model User {
  id         Int          @id @default(autoincrement())
  uuid       String       @db.Uuid
  createdat DateTime     @default(now()) @db.Timestamp(6)
  updatedat DateTime     @updatedAt
  firstname String       @db.VarChar
  lastname  String       @db.VarChar
  email      String       @unique @db.VarChar
  password   String       @db.VarChar
  group      Group[]
}

enum Group {
  USER
  ADMIN
}

然后我有以下

jest
测试

/* eslint-disable no-unused-vars */
import { create } from '../index';
import { prismaMock } from '../../../../../db/singleton';

enum Group {
  USER,
  ADMIN,
}

// also tried
/*enum Group {
  USER = 'USER',
  ADMIN = 'ADMIN',
}*/


test('should create new user ', async () => {
  try {
    const userModel = {
      id: 1,
      email: '[email protected]',
      uuid: '65sdf5sa4dfs5sdf54ds5f',
      createdat: new Date(),
      updatedat: new Date(),
      firstname: 'jon',
      lastname: 'doe',
      password: '123456',
      group: [Group.USER],
    };

    const demoUser = {
      id: 1,
      email: '[email protected]',
      uuid: '65sdf5sa4dfs5sdf54ds5f',
      firstname: 'jon',
      lastname: 'doe',
      password: '123456',
      group: [Group.USER],
    };

    prismaMock.user.create.mockResolvedValue(userModel);

    await expect(create(demoUser)).resolves.toEqual({
      id: 1,
      email: '[email protected]',
      uuid: '65sdf5sa4dfs5sdf54ds5f',
      createdat: new Date(),
      updatedat: new Date(),
      firstname: 'jon',
      lastname: 'doe',
      password: '123456',
      group: [Group.USER],
    });
  } catch (error) {
    console.log('*****', error);
  }
});

这会导致以下错误:

Argument of type '{ id: number; email: string; uuid: string; createdat: Date; updatedat: Date; firstname: string; lastname: string; nickname: string; password: string; group: Group[]; }' is not assignable to parameter of type 'User | Prisma__UserClient<User>'.
  Type '{ id: number; email: string; uuid: string; createdat: Date; updatedat: Date; firstname: string; lastname: string; nickname: string; password: string; group: Group[]; }' is not assignable to type 'User'.
    Types of property 'group' are incompatible.
      Type 'Group[]' is not assignable to type 'import("/example/example-api/node_modules/.prisma/client/index").Group[]'.
        Type 'Group' is not assignable to type 'import("/example/example-api/node_modules/.prisma/client/index").Group'.ts(2345)

我不明白

Group[]
不能分配给类型
Group
。在
userModel
我有
group: [Group.USER]
。一个用户可以是多个组的一部分。我如何在
typescript
测试中处理这个问题?

typescript enums prisma ts-jest
2个回答
3
投票

您应该使用 Prisma 提供的枚举类型,而不是创建自己的枚举类型。所以直接像这样导入枚举:

import { Group } from '@prisma/client';

const userModel = {
      id: 1,
      email: '[email protected]',
      uuid: '65sdf5sa4dfs5sdf54ds5f',
      createdat: new Date(),
      updatedat: new Date(),
      firstname: 'jon',
      lastname: 'doe',
      password: '123456',
      group: [Group.USER],
};

0
投票

我在 Jest 测试中遇到了一个奇怪的错误:

 TypeError: Cannot read properties of undefined

为了解决这个问题,我更改了代码:

import { Group } from 'prisma/schema'

const valuePayload = {
  resource: Group.USER
}

对此:

import { Group } from 'prisma/schema'

const valuePayload = {
  resource: "USER" as Group
}
© www.soinside.com 2019 - 2024. All rights reserved.