Skip to content

Latest commit

Β 

History

History
106 lines (71 loc) Β· 2.2 KB

File metadata and controls

106 lines (71 loc) Β· 2.2 KB

Mocha Unit Testing - Node.js

This repository demonstrates my studies on unit tests in Node.js using the Mocha framework and the built-in assert module.

βš™οΈ How It Works

In this project, I used a Test-Driven Development to build a method that calculates factorial expressions. I put into practice the assert module and Mocha test framework to drive my development by constructing an automated test suite that is reliable, maintainable and expressive.

πŸ“Œ Technologies Used

  • Node.js - JavaScript runtime environment
  • Mocha - Testing framework
  • Assert - Built-in Node.js assertion module

πŸ“‚ Project Structure

mocha-unit-testing/
β”‚-- test/
β”‚   └── index-test.js   # Unit tests
β”‚-- index.js            # Function implementation
β”‚-- package.json        # Project configuration
β”‚-- .gitignore          # Ignored files for Git
β”‚-- README.md           # Project documentation

πŸš€ How to Set Up the Project

1️⃣ Clone the Repository

git clone https://github.com/miottto/mocha-unit-testing.git
cd mocha-unit-testing

2️⃣ Install Dependencies

npm install --global mocha

3️⃣ Edit Scripts Session In package.json file

"scripts": {
  "test": "mocha"
}

4️⃣ Run the Tests

npm test

πŸ“œ Example Implementation (index.js)

function add(a, b) {
    return a + b;
}
module.exports = add;

βœ… Example Test (test/index-test.js)

const assert = require('assert');
const add = require('../index');

describe('Addition Function', function() {
    it('should return 5 when adding 2 and 3', function() {
        assert.strictEqual(add(2, 3), 5);
    });
});

πŸ“– References

πŸ† Contribution

Feel free to contribute with improvements! Open an issue or submit a pull request. πŸš€


πŸ“Œ Author: miottto