DEV Community

Cover image for Setting up Node.js TDD with ESLint
Armin Afazeli
Armin Afazeli

Posted on

Setting up Node.js TDD with ESLint

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
Enter fullscreen mode Exit fullscreen mode

3 Install the required dependencies:

npm i eslint mocha chai --save-dev
Enter fullscreen mode Exit fullscreen mode

4 Initialize your ESLint configuration:

npx eslint --init
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

For Windows:

"scripts": {
  "lint": "eslint \"**/*.js\"",
  "test": "mocha test.spec.js"
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

9 In your index.js file, add the following line at the bottom to export your main function:

module.exports.Function = Function;
Enter fullscreen mode Exit fullscreen mode

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);
  });
});
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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)