This training was presented on November 2021.
To run this project, make sure that you met the following requirements:
- NodeJS v14
- NPM
To install the project dependencies run the following command at the project root folder:
npm install
- Mocha: Test runner
- Chai: Assertion library
- Sinon: Mock library
- Rewire: Library to test private functions
- NYC: Code coverage
The project have two folders:
- /src - Where the "real" code lives, it's just fake implementations to simulate a real project.
- /tests - It's where the unit tests are, and we will focus on this training, this folder have three folders inside
basic
,intermediate
, andadvanced
to divide the tests into topics,NOTE: This structure is just for educational purposes
.
The test files have comments to explain the test behaviour and what we are expecting.
To run all the tests, and code coverage run on the terminal:
npm test
Or, to run only basic
, intermediate
, or advanced
tests:
npm run test:basic
npm run test:intermediate
npm run test:advanced
The basic tests covers the foundations of the Mocha and Chai libraries.
File | Description |
---|---|
mochaStructure | An example of the Mocha structure |
mochaHooks | An example of the Mocha available hooks |
chaiExpect | Examples of how to use the Chai's expect API |
The intermediate tests covers real case testing scenarios.
File | Description |
---|---|
privateFunction | Testing private functions with Rewire library |
sinonStubs | Mocking external resources using Sinon stubs |
testingErrors | Mocking functions to test errors syncronous and asyncronous |
testingPromises | Testing Promises (asyncronous code) |
The advanced tests cover more complex scenarios that we can find in real world.
File | Description |
---|---|
sinonTimers | Testing Date dependent functions |
testingFactories | Testing libraries that uses the factory pattern |