Professional Documents
Culture Documents
8 Tips For Writing Better Unit Tests - by Fotis Adamakis - Jun, 2023 - Medium
8 Tips For Writing Better Unit Tests - by Fotis Adamakis - Jun, 2023 - Medium
Become a member
Open in app
For example, given the following simple counter component written in Vue
Options API:
// Counter.vue
export default {
data() {
return {
counter: 0
}
}, 4
methods: {
increment() {
this.counter++
}
}
}
// Counter.spec.js
import { shallowMount } from '@vue/test-utils'
import Counter from '@/components/Counter.vue'
describe('Counter.vue', () => {
it('increments the counter when the increment method is called', () => {
// Arrange
const wrapper = shallowMount(Counter)
// Act
wrapper.vm.increment()
// Assert
expect(wrapper.vm.counter).toBe(1)
})
})
2. Descriptive Names and Descriptions
Clear and concise test names and descriptions are essential for effectively
communicating the purpose and scope of the test and making it easier to
understand and maintain them.
To write good test names and descriptions, you should use descript ive and
meaningful names that accurately reflect the purpose of the test. You should
also include relevant details in the description, such as the input data and
expected output.
For example, a lazily written title gives little information about the test case:
describe('Counter.vue', () => {
it('increment', () => {
...
})
})
In contrast, when one of the following fails, the problem will be apparent.
describe('Counter.vue', () => {
it('increments the counter when the increase button is pressed', () => {
...
})
it('prevents the counter value of being negative when the counter is zero and the
...
})
})
3. Fast
Unit tests should be fast to ensure a seamless development workflow. Quick test
execution allows for prompt feedback, making it easier to identify and fix issues.
To create speedy tests, avoid external dependencies and minimize I/O operations.
Utilize mocks and stubs to simulate dependencies and focus on testing small,
isolated pieces of code.
4. Deterministic
Deterministic tests are tests that produce t he same output every t ime t hey are
run. They are important because they help ensure that the tests are reliable and
repeat able. To write deterministic tests, avoid using random data and external
sources that can change. Use repeatable inputs and keep test environments
consistent. Also, avoid using shared global resources that other tests can modify.
5. Comprehensive
A well-rounded unit test assesses both successful execution and potential
failures (happy & unhappy paths). It’s vital to test functions using valid inputs to
confirm expected results and invalid inputs to identify potential issues.
Furthermore, validate error handling by examining proper responses to thrown
exceptions during path execution.
6. Test-driven Development
Test-driven development (TDD) is a technique of writing tests before or during
the development process. It ensures that the code is testable and that the tests
cover all the necessary scenarios.
To implement TDD, start by writing a failing test that covers a specific scenario
or requirement. Then, write the minimum amount of code necessary to pass the
test. Finally, refactor the code to improve its design and maintainability.
7. Using Mocks
Mocks simulate the behaviour of external dependencies or objects. They are
important because they help isolate the code being tested and make it easier to
write deterministic tests.
8. Run on CI/CD
By integrating unit tests into your development pipeline and ensuring their
successful execution, you can prevent potential issues from slipping into
production environments.
Conclusion
Treating unit t est s as first-class cit izens means giving them the same level of
importance as production code. This mindset ensures that tests are consistently
written, well-maintained, and updated as necessary.
Taking the time to invest in proper unit testing techniques will save you time and
effort in the long run and result in more robust and efficient applications.
« Senior Software Engineer @ Glovo · Author · International Speaker · Vue.js Athens Meetup Organizer »
Fotis Adamakis
246 3
Fotis Adamakis
62 2
Fotis Adamakis
Advanced Data Fetching with Vue Query
A Must-Have Library for Every Vue Codebase
74 1
Fotis Adamakis
283 2
684 24
Ignacio de Gregorio
Microsoft Just Showed us the Future of ChatGPT with LongNet
Let’s talk about Billions
2.1K 32
Lists
1.2K 44
595 10
Apoorva in Mac O’Clock
468 7
408 4