This repo contains a collection of JS testing examples showing different testing techniques, patterns, and frameworks. Each subfolder presents a different testing scenario to showcase best practices in JS unit testing. It is organized into 6 subfolders, each containing an implementation file (app.js) and its corresponding test file (appTest.js).
This repository demonstrates testing with:
- Mocha - Test runner for organizing and executing tests
- Chai - Assertion library with multiple styles (assert, expect, should)
- Chai as Promised - Extension for testing promises
- Node's built-in assert - For basic assertions
The examples showcase different testing approaches:
- TDD (Test-Driven Development) - Using
assertstyle - BDD (Behavior-Driven Development) - Using
expectandshouldstyles - Asynchronous Testing - With callbacks, Promises, and async/await
- API Testing - Testing external API responses
- Node.js (v12 or higher recommended)
- npm (usually comes with Node.js)
-
Clone the repository:
git clone https://github.com/reMRKableDev/node-testing-mocha-chai.git cd node-testing-mocha-chai -
Install dependencies:
npm install
To run all tests:
npm test
To run tests for a specific example:
cd <subfolder-name>
npx mocha appTest.js
- Function Testing Testing return values and types
- String Operations: Case sensitivity and string comparison
- Asynchronous Operations: Handling callbacks and file operations
- Promises: Testing resolved and rejected promises
- Object Validation: Checking object structure and properties
- API Testing: Making real API calls and validating responses
- Test Organization: Using
describeanditblocks - Different Assertion Styles: Using multiple assertion approaches
- Using descriptive test names
- Organizing tests with nested
describeblocks - Using setup hooks like
beforeandbeforeEach - Testing both happy paths and error cases
- Writing independent tests
- Using test caching for external resources
- Providing meaningful error messages
- Proper error handling in implementation code
Contributions are welcome! Please feel free to submit a Pull Request.