Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do handle enum values in jest test with prisma? Group[] not assignable to Group

My prisma postgresql schema example

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
}

Then I have the following jest test

/* 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);
  }
});

That results in the following 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)

I don't understand Group[] is not assignable to type Group. In userModel I have group: [Group.USER]. A user can be a part of many groups. How do i handle this in typescript test?

like image 284
jtlindsey Avatar asked Oct 18 '25 19:10

jtlindsey


1 Answers

Instead of creating your own enum type, you should use the one provided by Prisma. So directly import the enum like this:

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],
};
like image 96
Ryan Avatar answered Oct 21 '25 08:10

Ryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!