#Do I need testing errors?
1 messages · Page 1 of 1 (latest)
Yes
Sorry
And would you agree that the point of unit tests is to test your logic a unit at a time?
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
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',
});
Does your method return an exception or throw an exception for the filter to handle?
a throw exception
Ok, thanks
Then it won't .resolves, it'll .rejects.toThrow()
Okokok
Instead if this return me something like "Hello wordl" I use .resolves, rigth?
You could, yeah
I give you a star hahaha
Thank you so much 😄
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)
);
});```
await expect(service.signIn(params)).rejects.toThrow(new HttpException('Invalid Password', HttpStatus.BAD_REQUEST))
It's not a promise?
You shouldn't be assigningthe result in something that is going to throw
Added the await to the beginning of the expect
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)
);
});```
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
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));
});
});