Setting up Node.js TDD with ESLint
Tags: javascript, node, tutorial, beginners
Published: 2023-03-22 10:02:53



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

<u>**1**</u> Create a new directory for your project and navigate to it in your terminal.

**<u>2</u>** Initialize your Node.js project with the following command:

```
npm init -y
```

**<u>3</u>** Install the required dependencies:

```
npm i eslint mocha chai --save-dev
```

**<u>4</u>** 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.

**<u>5</u>** Add the following scripts to your package.json file:

For Mac:

```json
"scripts": {
  "lint": "eslint '**/*.js'",
  "lint-watch": "nodemon --exec 'npm run lint --silent'",
  "test": "mocha test.spec.js"
}
```

For Windows:

```json
"scripts": {
  "lint": "eslint \"**/*.js\"",
  "test": "mocha test.spec.js"
}
```

**<u>6</u>** Create two new files in your project directory: index.js and test.spec.js.

**<u>7</u>** 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.


**<u>8</u>** In your test.spec.js file, import the required modules for testing:

```js
const assert = require('assert');
const testFunction = require('./index');
const should = require('chai').should();
```

**<u>9</u>** In your index.js file, add the following line at the bottom to export your main function:

```js
module.exports.Function = Function;
```
Replace Function with the actual name of the function you'll be testing.


**<u>10</u>** Write your first test case in the test.spec.js file. Here's an example:

```js
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.

**<u>11</u>** 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.

**<u>12</u>** 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.

**<u>13</u>** 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.


**<u>14</u>** 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.

**<u>15</u>** 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 😄!