Skip to content

Conversation

@angeldevildev
Copy link

I added some tests with Jest to some features.

@angeldevildev angeldevildev mentioned this pull request Aug 10, 2024
Copy link
Owner

@yujhenchen yujhenchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some comments—feel free to take a look. I'll continue reviewing the other files in the coming weeks. Thanks for your contributions and patience!

});

test('should reject an email without required fields', () => {
const invalidEmail: any = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to specify the any type for invalidEmail because TypeScript will infer its type based on the assigned value. This ensures that passing invalidEmail to isValidEmail will trigger a compiler error if the email object is invalid. As the image below
image

});

test('should reject an email with invalid types', () => {
const invalidEmail: any = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

expect(res.body.data).toBe('Operation successful');
});

it('should allow APIRequest and APIResponse without generics', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain a bit more about this test case. Why it's necessary to test if APIRequest and APIResponse can be cast as non-generic?

const OtpModel: IOtpModel = {
create: jest.fn(),
findOne: jest.fn(),
} as any;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: IOtpModel infer the type of OtpModel, not need to tell the TypeScript compiler to treat it as any type. Besides, avoid using any

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the idea of mocking and testing these two functions—it's really helpful and great!

createdAt: new Date(),
};

(OtpModel.create as jest.Mock).mockResolvedValueOnce(otpData);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the function create is defined correctly at the previous declaration step, not need to convert the data type into jest.Mock again here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants