I want to get a better understanding of how to test these kinds of functions in Typescript using mocha. Namely, I have the following small file:
import { Request, Response } from 'express';
export const ping = (req: Request, res: Response) => {
res.send('pong');
};
export const health = (req: Request, res: Response) => {
res.send('OK');
};
This exercise may be trivial since there's not much to test here but below I have some basic mocha tests:
describe('Health Tests', () => {
it('Check ping', () => {
expect(ping).to.not.be.null;
})
it('Check health', () => {
expect(health).to.not.be.null;
})
})
When I run code coverage on this test I get: 50% Stmts | 100% Branch | 0% Funcs | 50% Lines. Especially because this is such a small bit of code, I want to get 100% coverage on all of the categories if possible. Would anyone have any advice on how to accomplish this? Also, could someone please explain why I have 0% coverage for functions? By checking ping or health, am I not also then calling a function and hence, testing that as well.
Any advice would be much appreciated!
undefined). It is always easier to test a function that returns a value based on its parameters so write as many functions in this manner as possible for maximal testability. You can't always do that, but in this case, you still have to call them, and your assertions will need to be performed on their side effects, which is to say on how they impactres.