如何在 prisma 的玩笑测试中处理枚举值? Group[] 不可分配给 Group
How do handle enum values in jest test with prisma? Group[] not assignable to Group
我的 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: 'hello@prisma.io',
uuid: '65sdf5sa4dfs5sdf54ds5f',
createdat: new Date(),
updatedat: new Date(),
firstname: 'jon',
lastname: 'doe',
password: '123456',
group: [Group.USER],
};
const demoUser = {
id: 1,
email: 'hello@prisma.io',
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: 'hello@prisma.io',
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
测试中处理这个问题?
您不应创建自己的枚举类型,而应使用 Prisma 提供的枚举类型。所以直接像这样导入枚举:
import { Group } from '@prisma/client';
const userModel = {
id: 1,
email: 'hello@prisma.io',
uuid: '65sdf5sa4dfs5sdf54ds5f',
createdat: new Date(),
updatedat: new Date(),
firstname: 'jon',
lastname: 'doe',
password: '123456',
group: [Group.USER],
};
我的 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: 'hello@prisma.io',
uuid: '65sdf5sa4dfs5sdf54ds5f',
createdat: new Date(),
updatedat: new Date(),
firstname: 'jon',
lastname: 'doe',
password: '123456',
group: [Group.USER],
};
const demoUser = {
id: 1,
email: 'hello@prisma.io',
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: 'hello@prisma.io',
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
测试中处理这个问题?
您不应创建自己的枚举类型,而应使用 Prisma 提供的枚举类型。所以直接像这样导入枚举:
import { Group } from '@prisma/client';
const userModel = {
id: 1,
email: 'hello@prisma.io',
uuid: '65sdf5sa4dfs5sdf54ds5f',
createdat: new Date(),
updatedat: new Date(),
firstname: 'jon',
lastname: 'doe',
password: '123456',
group: [Group.USER],
};