In this Action Item, you will:
- βοΈ write effective
tests
following theAAA
pattern - π΅οΈ learn how to use
code coverage
to measure your testing progress - π¦ get introduced to
Test-Driven Development
- πͺ[COMPETENT] Write a simple
unit
test using theAAA
pattern - ππ½ββοΈ[PROFICIENT] Add the missing function, in a
TDD
style - π₯[PROFICIENT] Write an
integration test
- π§ββοΈ[EXPERT][BONUSπ] Get the code coverage to
100%
usingautomocking
HINT: Use the AAA pattern to structure your tests
- Test the public Interface/APIs of the system rather than the internal functions.
- Test state, not interactions. Rather than testing how the system got to a certain result, test the result.
- Test Behaviors, not Functions. Do not try to match the structure of the test to the one of the code.
- It is ok to repeat yourself(forget DRY) and be DAMP(Descriptive and Meaningfully Phrases)
- Write Clear Error Messages: Given -> When -> Then
WALK-THROUGH: 1.[COMPETENT] Write a simple unit test
Test a single function in isolation using a unit test
.
npm install
npm test
Check the terminal and inspect the results.
The tests will now run when you change your code. You should also see the current Code Coverage:
Check the coverage folder and open the coverage/lcov-report/index.html file in your browser. You should see something like this:
Code Coverage for a specific function:
- π Add tests for calculatePriceAfterDiscount.ts and get the coverage to 100% for this function
Follow the examples in the tests files:
Run the tests in watch
mode:
npm run test:dev
You should see something like this:
Press a to run all the tests. Keep them running to get real-time feedback on your progress.
Make sure you cover the function at 100% in tests, see below:
- π CODE SOLUTION - Adding a unit test:
git checkout solution_one
- π₯οΈ VIDEO SOLUTION - Adding a unit test
WALK-THROUGH: 2.[PROFICIENT] Add the missing function, in a TDD style
Use Test-Driven Development
to implement a method. We have already written test cases for the function, you have to write the code.
- π Complete the function in filterAndSortByDiscount.ts so the test in filterAndSortByDiscount.spec.ts will pass.
Check the failed test message and implement a function to make the tests pass:
- π CODE SOLUTION - Add the function in TDD style:
git checkout solution_two
- π₯οΈ VIDEO SOLUTION - Add the function in TDD style
WALK-THROUGH: 3.[PROFICIENT] Write an integration test
- π Add tests for calculateCartItemPrice.ts and get the coverage to 100% for this function
π‘ HINT: Use jest
to mock dependencies.
Note: We have added some mock
objects to make it easier for you in mocksExample.ts.
- π CODE SOLUTION - Add integration tests:
git checkout solution_three
- π₯οΈ VIDEO SOLUTION - Add integration tests
π‘ HINT: Use ts-auto-mock
to easily create mock objects.
const discountedProduct = createMock<Product>({
discount: {
isEnabled: true,
percentage: 50,
},
});
- π CODE SOLUTION - Set up auto-mocks:
git checkout solution_three_automocks
- π₯οΈ VIDEO SOLUTION - Set up auto-mocks
WALK-THROUGH: 4.[EXPERT][BONUSπ] Get the code coverage to 100% by addding integration tests
- π Get the code coverage to
100%
for the module by addingintegration
tests in priceCalculator.spec.ts
π‘ HINT: Use jest
to mock dependencies.
π‘ HINT: Use ts-auto-mock
to easily create mock objects.
If you have issues with the Action Item, you can get help and feedback in the Community or, in the Weekly Calls.