This repository is used for demonstrating the topic Simplify Angular unit test which is presented at Ho Chi Minh - Angular meetup Nov 04, 2023,. The article for this repo can be read here: Unit test - Make it simple https://docs.google.com/presentation/d/1Q8pra0ZEfKckuMWMmhxUgfwTnnnP1P1JC-fk9x-4JyA/edit?usp=sharing
This project was generated with Angular CLI version 16.2.6.
And there are some changes to the default Angular project created by Angular CLI:
- Using
jestframework for writing tests - Using
yarnfor the package installation - The
yarn run testwill triggerjest --verboseto provide the list of test cases
This is the default branch, and contains only basic configurations to prepare for the samples
The usage branch: 01-sample-one:
src/app/components/sample-one.component.before.spec.ts: The original creation of the testing module, where we need to import all child components with pipes (or directives)src/app/components/sample-one.component.after.spec.ts: The simplified version, where we use theNO_ERROR_SCHEMAwith mock pipes to test the component itself.
Other test file
src/app/components/block.component.spec.ts
The usage branches 02-sample-two:
src/app/components/sample-two.component.before.spec.ts: The original creation of the testing module, where we need to simulate deep-relation dependency injections for checking the output resultsrc/app/components/sample-two.component.after.spec.ts: The simplified version, where we just mock the usage dependency injection for the test case input.
The usage branch 03-sample-three:
src/app/components/counter.component.before.spec.ts: The original creation of the testing module, where we need to declare thestoreand simulate the state to get the changes. This approach having some unsure matter where a state changed may lead to side effects.src/app/components/counter.component.after.spec.ts: The simplified implementation, which uses mock service and the helper tool to cast the observable input value for testing output result.
(Feel free to add more use cases)
Thank you for the support from @trungk for arranging the meetup to share this topic. Also I send thanks to all people joining and sharing this document.