Here is an example of my prisma postgresql
schema:
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
}
Now, let's take a look at the jest
test I wrote:
/* eslint-disable no-unused-vars */
import { create } from '../index';
import { prismaMock } from '../../../../../db/singleton';
enum Group {
USER,
ADMIN,
}
// code snippet
/*enum Group {
USER = 'USER',
ADMIN = 'ADMIN',
}*/
test('should create new user ', async () => {
try {
const userModel = {
id: 1,
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b232e2727240b3b392238262a">[email protected]</a>',
uuid: '65sdf5sa4dfs5sdf54ds5f',
createdat: new Date(),
updatedat: new Date(),
firstname: 'jon',
lastname: 'doe',
password: '123456',
group: [Group.USER],
};
const demoUser = {
id: 1,
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f272a2323200f3f3d263c222e612620">[email protected]</a>',
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: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="543c3138383b1424263d2739357a3d3b">[email protected]</a>',
uuid: '65sdf5sa4dfs5sdf54ds5f',
createdat: new Date(),
updatedat: new Date(),
firstname: 'jon',
lastname: 'doe',
password: '123456',
group: [Group.USER],
});
} catch (error) {
console.log('*****', error);
}
});
An error occurred when running the test:
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)
I am confused why Group[]
cannot be assigned to type Group
. In the userModel
, I specified group: [Group.USER]
. Since a user can belong to multiple groups, how should I handle this scenario in my typescript
test?