Skip to content

rpereiracoffee/unit-tests-training

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NodeJS Unit Tests

This training was presented on November 2021.

Getting Started

To run this project, make sure that you met the following requirements:

  • NodeJS v14
  • NPM

Installing Dependencies

To install the project dependencies run the following command at the project root folder:

npm install

Test stack

  • Mocha: Test runner
  • Chai: Assertion library
  • Sinon: Mock library
  • Rewire: Library to test private functions
  • NYC: Code coverage

Project structure

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, and advanced 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.

Running the tests

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

Basic tests

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

Intermediate tests

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)

Advanced tests

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

About

NodeJS unit test training in NOV/2021

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published