Goglides Dev 🌱

Goglides Dev 🌱 is a community of amazing users

Shaping the future of IT, one connection at a time.

Create account Log in
Cover image for What is the purpose of the 'test' command when creating a package.json file for your MERN stack project?
Balkrishna Pandey
Balkrishna Pandey

Posted on

What is the purpose of the 'test' command when creating a package.json file for your MERN stack project?

When building a MERN stack application, it's essential to ensure that the code works as expected and that changes to the codebase don't break existing functionality. That's where testing comes in - by writing automated tests; developers can ensure that the application behaves correctly and can catch issues before they make it to production. In a MERN stack project context, the test command in the package.json file is critical for running tests. In this blog post, we'll explore the test command, how it works, and how you can use it to test your MERN stack application.

What is the 'test' command in package.json?

When you create a new MERN stack project, one of the first things you'll do is initialize a new Node.js project with the npm init command. This creates a package.json file containing various metadata about your project, dependencies, and other configuration options.

npm init

One of the fields in package.json is "scripts", which allows you to define custom scripts that can be run with the npm run command. One common script is the 'test' script, which runs automated tests.

By default, the 'test' script in package.json points to the echo "Error: no test specified" && exit 1 command. This is because when you run the npm test without defining a test command, the default behavior is to exit with an error. However, you can customize the 'test' script to run whatever testing framework or script you prefer.

How to Use the 'test' Command with Jest

One popular testing framework for Node.js projects is Jest. Jest is a simple yet powerful testing framework that provides much functionality, including assertions, mocking, and coverage reports. It's easy to set up and integrate into your MERN stack project and can help you catch bugs and issues early on.
You'll need to install Jest as a development dependency to use Jest with your MERN stack project. You can do this by running the following command:

npm install --save-dev jest
Enter fullscreen mode Exit fullscreen mode

This is your final package.json looks like after this,

{
  "name": "mern-stack-project",
  "version": "1.0.0",
  "description": "Demo for jest",
  "main": "index.js",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "test": "jest"
  },
  "keywords": [
    "jest"
  ],
  "author": "Balkrishna Pandey",
  "license": "Apache-2.0",
  "devDependencies": {
    "jest": "^29.5.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

Once Jest is installed, you can define your tests in a __tests__ directory or any file ending with .test.js or .spec.js. Jest will automatically pick up and run these files when you run the 'test' script.

Here's a sample Jest test that you can include in your project's test suite without writing any other functions.

// test/sample.test.js

describe('sample test', () => {
  it('should pass', () => {
    expect(true).toBe(true);
  });

  it('should fail', () => {
    expect(false).toBe(true);
  });
});
Enter fullscreen mode Exit fullscreen mode

In this example, we're testing whether the true value is equal to true (which should pass), as well as whether the false value is equal to true (which should fail). You can modify this sample test to fit your specific needs, and create additional tests as needed.

Now you can run your tests using the npm test command, which will run the test script specified in your package.json file.

npm test using jest

Here is another example, let's say you have a simple function (file add.js) in your MERN stack project that adds two numbers together:

function add(a, b) {
  return a + b;
}

module.exports = add;

Enter fullscreen mode Exit fullscreen mode

You could write a test for this function in a file called add.test.js:

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

test('adds 1 + 2 to equal 3', () => {
  expect(add(1, 2)).toBe(3);
});
Enter fullscreen mode Exit fullscreen mode

Here, we're importing the add function from add.js, then defining a test that expects the function to return 3 when given the inputs 1 and 2.

I am assuming following folder structure

127.0.0.1 $ tree .
.
├── __test__
│   ├── add.test.js
│   └── sample.test.js
├── add.js
├── node_modules
│   ├── @ampproject
│   ├── ....
├── package-lock.json
└── package.json

1 directory, 5 files
Enter fullscreen mode Exit fullscreen mode

You can run this test by running the npm test in your terminal. Jest will automatically detect and run any test files that match the .test.js or .spec.js pattern and will output a report indicating whether the tests passed or failed. Your output should look like this,

npm test jest 2

Conclusion

The 'test' command in package.json is a powerful tool for running automated tests in your MERN stack project. By defining custom scripts and using a testing framework like Jest, you can catch issues early on and ensure your code works as expected. Whether you're building a small personal project or a large-scale application, setting up testing can save you a lot of headaches down the line.

Discussion (0)