Hi folks,
Let’s do some hands-on practice about making our applications more reliable, with fewer bugs and happier life.
We can achieve that by adding unit tests. Let’s learn how to do that together.
It’s time for “How to write unit tests for dummies” 🥰. DummyTechDev will cover this in easy terms for you 💪
We’re using Jest x TypeScript for the tutorial. Don’t worry, every programming language will have the same approach when it comes to writing unit tests.
The Why – How to write unit test for dummies
Before learning anything, always a good idea to learn the “whys”.
Unit Testing, well, basically we’ll add the test to verify our functions if they run correctly or not. We’ll prepare our own test cases.
The PROs:
- Helps us to find early bugs/flaws in the development.
- Give us more confidence when shipping changes to the Staging / Production.
- Maintain a healthy codebase/project.
- (Later) if we refactor any part of the code, tests will ensure that we don’t break the business logic, data calculation, etc.
Pro tip: it’s 100% fine to write the function first before test cases. Following TDD will cost more time and headaches.
The Basic – How to write unit tests for dummies
I have a basic function:
// user.ts
type User = {
id: string;
firstName: string;
lastName: string;
}
export const getUserFullName = (user?: User | null) => {
if (!user) {
return '';
}
return `${user.firstName} ${user.lastName}`;
};
With the simple function above, there are 2 test cases we need to write:
- Test the function with the correct data
- Test the function with the invalid data (
undefined
user,null
user)
We should describe our tests in short but meaningful terms.
// user.test.ts
describe('getUserFullName', () => {
it('returns full name when using valid user', () => {
const fullName = getUserFullName({
id: 1,
firstName: 'Dummy',
lastName: 'TechDev',
});
expect(fullName).toEqual('Dummy TechDev');
});
it('returns empty string when using undefined', () => {
const fullName = getUserFullName(undefined);
expect(fullName).toEqual('');
});
it('returns empty string when using null', () => {
const fullName = getUserFullName(null);
expect(fullName).toEqual('');
});
});
With the above test cases, our getUserFullName
method is 100% covered, we don’t have to worry about it going wrong in the Staging or Production environment.
The Tips for How to write unit tests for dummies
Create simple & small functions
Don’t create a huge function (function with many lines of code), let’s always try to create more and more smaller functions.
Big functions will end up having multiple conditions, and dependencies which makes it hard to write test cases.
Always declare the output/return type
Avoid using void
as the return type for your functions, we can write a test but we don’t really know what would be the output/outcome after we triggered the functions.
It’s fine to have some untestable code
Yes it is, don’t stress too much on this. If isn’t testable automatically, ensure that you have tested it carefully, covered it with love, etc.
Conclusion
Unit tests are really cool to help us:
- Gain more confidence in our changes
- Spot early errors/bugs when developing
- Spot regression
- Refactor with ease in mind.
DummyTechDev hopes that you will adapt this and increase your productivity & processes to a better horizon.
Thanks for reading!