#Do I need testing errors?

1 messages · Page 1 of 1 (latest)

valid quest
#
  1. Don't use the "Suggestion to the fw" tag unless you have an actual suggestion
#
  1. Are those errors a part of your logic?
jolly tendon
valid quest
# jolly tendon Yes

And would you agree that the point of unit tests is to test your logic a unit at a time?

jolly tendon
#

Uhm... you mean like first "without error" then with some error and so on?

#
  describe('sign in', () => {
    it('successfully', async () => {
      const user = {
        email: '[email protected]',
        password: 'password$123',
      };

      const result = await service.signIn(user);

      expect(result).toEqual('Hello world');
    });

    it('user not found', async () => {
      const user = {
        email: '[email protected]',
        password: 'password$123',
      };

      const result = await service.signIn(user);

      expect(result).toEqual('User not found');
    });

    it('invalid password', async () => {
      const user = {
        email: '[email protected]',
        password: '00000000',
      };

      const result = await service.signIn(user);

      expect(result).toEqual({
        response: 'Invalid password',
        status: 400,
        message: 'Invalid password',
        name: 'HttpException',
      });
    });
``` Something like this
heady narwhal
#

if you're dealing with promises, I suggest you to avoid the await before expect

#

instead, use:

const whenResult = service.signIn(user);
await expect(whenResult).resolves.toEqual({
  response: 'Invalid password',
  status: 400,
  message: 'Invalid password',
  name: 'HttpException',
});
valid quest
#

Does your method return an exception or throw an exception for the filter to handle?

valid quest
jolly tendon
#

Okokok

valid quest
jolly tendon
#

Instead if this return me something like "Hello wordl" I use .resolves, rigth?

valid quest
#

You could, yeah

jolly tendon
#

Thank you so much 😄

jolly tendon
#

I try to use .rejects.toThrow but dosn't work

#
it('invalid password', async () => {
      jest.spyOn(bcrypt, 'compareSync').mockReturnValue(false);

      const result = await service.signIn({
        email: mockUser.email,
        password: mockUser.password,
      });

      expect(bcrypt.compareSync).toHaveBeenCalledWith(
        mockUser.password,
        mockUser.password
      );

      await expect(Promise.resolve(result)).rejects.toThrow(
        new HttpException('Invalid password', HttpStatus.BAD_REQUEST)
      );
    });```
valid quest
#

await expect(service.signIn(params)).rejects.toThrow(new HttpException('Invalid Password', HttpStatus.BAD_REQUEST))

jolly tendon
#

It's not a promise?

valid quest
#

You shouldn't be assigningthe result in something that is going to throw

valid quest
jolly tendon
#

The same thing

#
it('invalid password', async () => {
      jest.spyOn(bcrypt, 'compareSync').mockReturnValue(false);

      const result = await service.signIn({
        email: mockUser.email,
        password: mockUser.password,
      });

      expect(bcrypt.compareSync).toHaveBeenCalledWith(
        mockUser.password,
        mockUser.password
      );

      await expect(result).rejects.toThrow(
        new HttpException('Invalid Password', HttpStatus.BAD_REQUEST)
      );
    });```
valid quest
#

I'll point this message back out

You shouldn't be assigningthe result in something that is going to throw

#

i.e. don't do const result = await service.signIn(). That's why the error is being thrown, because it's an unhandled rejection

#

Just like regular JS, when an error is thrown, it'll go to the catch.

#

There's no catch, so Jest catches it and reports the error

#

But you don't need to assign it in the first place, just use the await expect() I provided before the expect(bcrypt.compareSync) call

jolly tendon
#

ooooooh

#

I get it

#

Thank you so much again 😄

jolly tendon
#

I dooooo it brooo, look at this ```ts
describe('should sign in', () => {
it('successfully', async () => {
jest.spyOn(bcrypt, 'compareSync').mockReturnValue(true);
jest.spyOn(model, 'findOne').mockResolvedValue(mockUser);

  const result = await service.signIn({
    email: mockUser.email,
    password: mockUser.password,
  });

  await expect(Promise.resolve(model.findOne)).resolves.toHaveBeenCalledWith({
    email: mockUser.email,
  });
  expect(bcrypt.compareSync).toHaveBeenCalledWith(
    mockUser.password,
    mockUser.password
  );
  await expect(Promise.resolve(result)).resolves.toEqual('Hello world');
});

it('user not found', async () => {
  jest.spyOn(model, 'findOne').mockResolvedValue(null);
  jest.spyOn(bcrypt, 'compareSync').mockReturnValue(true);

  await expect(Promise.resolve(model.findOne)).resolves.toHaveBeenCalledWith({
    email: mockUser.email,
  });

  expect(bcrypt.compareSync).toHaveBeenCalledWith(
    mockUser.password,
    mockUser.password
  );
  await expect(
    service.signIn({
      email: mockUser.email,
      password: mockUser.password,
    })
  ).rejects.toThrow(new HttpException('User not found', HttpStatus.NOT_FOUND));
});

it('invalid password', async () => {
  jest.spyOn(model, 'findOne').mockResolvedValue(mockUser);
  jest.spyOn(bcrypt, 'compareSync').mockReturnValue(false);

  await expect(Promise.resolve(model.findOne)).resolves.toHaveBeenCalledWith({
    email: mockUser.email,
  });

  expect(bcrypt.compareSync).toHaveBeenCalledWith(
    mockUser.password,
    mockUser.password
  );

  await expect(
    service.signIn({
      email: mockUser.email,
      password: mockUser.password,
    })
  ).rejects.toThrow(new HttpException('Invalid password', HttpStatus.BAD_REQUEST));
});

});