AnonymousGeese
AnonymousGeese

Reputation: 43

Jest Test failing due to promise rejected

I am trying to run some unit tests in my NestAPI. My service.ts has this:

  findAll(): Promise<Users[]> {
    return prisma.users.findMany();
  }

In my service.spec.ts:

const usersMany = [
  {
    username: 'user1',
    name: 'User One',
    password: '123',
    email: '[email protected]',
    tasks: [],
  },
  {
    username: 'user2',
    name: 'User Two',
    password: '456',
    email: '[email protected]',
    tasks: [],
  }
}

And after that, I have this test case for my FindAll()

  describe('findAll', () => {
    it('should return all users', async () => {
      const usersAll = await service.findAll();
      expect(usersAll).toEqual(usersMany);
    });
  });

However, when I run this test, I get the following error:

    - Expected  - 23
    + Received  +  1

    - Array [
    -   Object {
    -     "email": "[email protected]",
    -     "name": "User One",
    -     "password": "123",
    -     "tasks": Array [],
    -     "username": "user1",
    -   },
    -   Object {
    -     "email": "[email protected]",
    -     "name": "User Two",
    -     "password": "456",
    -     "tasks": Array [],
    -     "username": "user2",
    -   },
    - ]
    + Array []

I'm new to writing unit tests, so would really appreciate some help here. Why is my test failing here?

Upvotes: 0

Views: 134

Answers (1)

SwaD
SwaD

Reputation: 641

The test fails for the reason that the service method.find All() returns an empty array, and checks against the usersMany array. Here, either dip the function and then the test is essentially about nothing, or check with an empty array.

The most correct option is to check this method together with other methods of adding data and returning them.

Below is the code of the synthetic test, but it's a bit silly.

import { findAll } from './func.js'
jest.mock('./func.js');

describe('findAll', () => {
  it('should return all users', async () => {
    const usersMany = [
      {
        username: 'user1',
        name: 'User One',
        password: '123',
        email: '[email protected]',
        tasks: [],
      },
      {
        username: 'user2',
        name: 'User Two',
        password: '456',
        email: '[email protected]',
        tasks: [],
      }];
    findAll.mockReturnValue(usersMany)
    const usersAll = await findAll();
    expect(usersAll).toEqual(usersMany);
  });
});

If you fantasize about the methods of prisma.users, then you can imagine it like this:

class Users {
  constructor() {
    this.users = [];
  }
  findMany() {
    return this.users;
  }
  addUser(user) {
    this.users.push(user);
  }
  delUser(user) {
    this.users = this.users.filter((item) => item.username !== user.username);
  }
}
export const prisma = {
  users: new Users()
}

export function findAll() {
  return prisma.users.findMany();
}

The tests will be as follows:

describe('prisma.users', () => {
  let result = [];
  const user = {
    username: 'user1',
    name: 'User One',
    password: '123',
    email: '[email protected]',
    tasks: [],
  }
  const user2 = {
    username: 'user2',
    name: 'User One',
    password: '123',
    email: '[email protected]',
    tasks: [],
  }
  const user3 = {
    username: 'user3',
    name: 'User One',
    password: '123',
    email: '[email protected]',
    tasks: [],
  }

  test('null', () => {
    expect(findAll()).toEqual(result);
  })

  test('add 1', () => {
    // I expect to get
    result.push(user);
    prisma.users.addUser(user);
    expect(findAll()).toEqual(result);
  });

  test('add 2', () => {
    result.push(user2);
    prisma.users.addUser(user2);
    expect(findAll()).toEqual(result);
  })
  test('add 3', () => {
    result.push(user3);
    prisma.users.addUser(user3);
    expect(findAll()).toEqual(result);
  })
  test('del 2', () => {
    result = [];
    result.push(user);
    result.push(user3);
    prisma.users.delUser(user2);
    expect(findAll()).toEqual(result);
  })

})

Upvotes: 1

Related Questions