This repository demonstrates how to configure and execute unit tests using Mocha and Chai in a Node.js environment. The project includes step-by-step instructions for initializing a Node.js project, installing dependencies, and setting up Mocha and Chai for testing.
Before you begin, ensure you have Node.js installed. You can download it from Node.js official website.
- Initialize the Project
-
Start by creating a
package.json
file:npm init
-
Follow the prompts to complete the setup.
- Install Mocha and Chai
-
Install Mocha as the test runner and Chai for assertions:
npm install npm install chai npm install mocha
- To run your tests using Mocha, use the following command:
npm run test
- This will execute all tests within the project.
For the purposes of this project, it doesn't matter which standard you'd prefer to use โ ESM or CommonJS. This time I used CommonJS.
If you chose the CommonJS standard, you don't have to change the file extension of the test files and you don't need to add "type": "module"
in the package.json
file.
CommonJS is a module system that is widely used in Node.js. In this project, I have used the CommonJS module format. This means you can require modules using require()
and export modules using module.exports
. Here is an example:
// Requiring a module
const myModule = require('./myModule');
// Exporting a module
module.exports = myFunction;
ECMAScript Modules (ESM) is the official standard for JavaScript modules. If you prefer to use ESM, you can do so by changing the file extensions to .mjs
or by adding "type": "module"
in the package.json
file. Here is an example:
// Importing a module
import myModule from './myModule';
// Exporting a module
export default myFunction;
Contributions are welcome! If you have any improvements or bug fixes, feel free to open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
For any questions or suggestions, please reach out to the course instructor or open an issue in the repository.