In this guide, we will walk through the process of setting up a Node.js project with Test-Driven Development (TDD) and ESLint for consistent code styling. We will be using the popular Mocha test framework, Chai assertion library, and ESLint for linting.
Prerequisites
- Basic knowledge of JavaScript and Node.js
- Node.js and npm installed on your machine
Steps
1 Create a new directory for your project and navigate to it in your terminal.
2 Initialize your Node.js project with the following command:
npm init -y
3 Install the required dependencies:
npm i eslint mocha chai --save-dev
4 Initialize your ESLint configuration:
npx eslint --init
Follow the prompts and choose a style guide that you prefer (e.g., Airbnb, Standard, or Google). This will create an .eslintrc.json file in your project directory.
5 Add the following scripts to your package.json file:
For Mac:
"scripts": {
"lint": "eslint '**/*.js'",
"lint-watch": "nodemon --exec 'npm run lint --silent'",
"test": "mocha test.spec.js"
}
For Windows:
"scripts": {
"lint": "eslint \"**/*.js\"",
"test": "mocha test.spec.js"
}
6 Create two new files in your project directory: index.js and test.spec.js.
7 To prevent ESLint from linting your test files, you can either add /* eslint-disable */ at the top of your test.spec.js file or create an .eslintignore file in the root directory of your project with the following content:
test.spec.js
This will tell ESLint to ignore the specified test file(s) when running the linting process.
8 In your test.spec.js file, import the required modules for testing:
const assert = require('assert');
const testFunction = require('./index');
const should = require('chai').should();
9 In your index.js file, add the following line at the bottom to export your main function:
module.exports.Function = Function;
Replace Function with the actual name of the function you'll be testing.
10 Write your first test case in the test.spec.js file. Here's an example:
describe('Test suite for convertToRoman function', () => {
it('should return MCMXCIX if we pass 1999', () => {
// Arrange
const returnData = 'MCMXCIX';
// Act
const result = testFunction.convertToRoman(1999);
// Assert
result.should.equal(returnData);
});
});
This example assumes you have a convertToRoman function in your index.js file. Replace it with the actual function you want to test.
11 Now you can write the function in the index.js file that satisfies the test case. As you continue developing your application, write more test cases to ensure your code is working as expected.
12 To run the test cases use the following command in your terminal:
npm test
This will execute the Mocha test runner and run all the test cases in your test.spec.js file. The output will show you the results of each test, including any failed tests.
13 To check your code for any linting issues, run the following command:
npm run lint
This will execute ESLint on your project files and display any code style violations based on the configuration you chose earlier.
14 To automatically fix some of the linting issues, you can run:
npx eslint --fix "**/*.js"
This command will attempt to fix some of the simpler issues, such as indentation or spacing. However, it won't be able to fix all issues, and you may need to manually address some of the more complex problems.
15 As you continue to develop your application, make sure to write tests for new functionality and run the tests and linting processes regularly. This will help you maintain high-quality code and minimize the likelihood of introducing bugs.
That's it! You now have a Node.js project set up with Test-Driven Development and ESLint for consistent code styling. Good luck 😄!
Top comments (0)