Search code examples
typescriptjestjsnestjs

TypeScript - "not assignable to type never" error on Entity in Jest's mockResolvedValueOnce method


I've seen this error being solved in primitive types but I am not sure how I would solve it in this example

const newUser: UserEntity = {
      user_id: 'f3bea6de-fb24-4441-b75b-d7642ca573d7',
      name: 'Test User',
    };

jest.spyOn(repo, 'create').mockResolvedValueOnce([newUser]); // error here on [newUser] - 'UserEntity' is not assignable to type 'never'

user.entity.ts

@Entity('users')
export class UserEntity {
  @PrimaryGeneratedColumn('uuid') user_id: string;
  @Column('text') name: string;
}

Solution

  • repo.create is a synchronous function, but mockResolvedValue is for asynchronous functions that return promises. Instead, use mockReturnValueOnce and you'll have no problems.