myHotTake

Tag: Mocha tests

  • How to Run Jest and Mocha Tests Using npm Scripts?

    If you find this story helpful, feel free to like or share it with anyone who might need a little JavaScript inspiration!


    I am faced with a daunting math problem. It looks complex at first, with numbers and variables scattered like puzzle pieces. I know the best way to tackle it is to break it down into manageable steps. That’s exactly what I do with running tests in Jest or Mocha using npm scripts.

    First, I gather my tools, just like I would with a math problem. In the world of JavaScript testing, these tools are Jest and Mocha, both reliable calculators that help me verify my code. I ensure they are installed by checking and adding them to my project with a simple command, like npm install jest or npm install mocha, treating it like sharpening my pencils before starting my math work.

    Now, I’m ready to set up my test script, just as I would outline the steps to solve my math problem. In the package.json file, I create a script section. This is akin to writing down the sequence of operations to follow. I add a line for Jest: "test": "jest", or for Mocha: "test": "mocha". This is my roadmap, guiding me through the process, just like writing “Step 1, Step 2…” in my math notebook.

    With everything in place, it’s time to run the tests, similar to working through the math problem step by step. I open my terminal, my digital blackboard, and type npm test. Jest or Mocha begins to calculate, just as I would solve each equation methodically. It checks each line of code, ensuring that the solution makes sense.

    Finally, when I see the results, it’s like the satisfaction of a math problem solved. If all the tests pass, it’s as if I’ve arrived at the correct answer. If not, I know exactly where I went wrong, much like spotting an error in my arithmetic. I can go back, adjust, and run the tests again, refining my solution until it’s perfect.


    Writing Tests with Jest

    Let’s say I have a function that adds two numbers together:

    function add(a, b) {
      return a + b;
    }
    
    module.exports = add;

    I create a file named add.test.js and write a test using Jest:

    const add = require('./add');
    
    test('adds 1 + 2 to equal 3', () => {
      expect(add(1, 2)).toBe(3);
    });

    This test checks if adding 1 and 2 indeed gives me 3, just like verifying a step in my math problem. I run this with npm test, and Jest tells me if my function passes the test.

    Writing Tests with Mocha

    Now, if I’m using Mocha, the approach is slightly different. Here’s my add function again:

    function add(a, b) {
      return a + b;
    }
    
    module.exports = add;

    I set up a test file add.test.js like this:

    const assert = require('assert');
    const add = require('./add');
    
    describe('Add Function', function() {
      it('should add 1 and 2 to equal 3', function() {
        assert.strictEqual(add(1, 2), 3);
      });
    });

    With Mocha, I use describe and it blocks to structure the tests logically, like paragraphs in an essay. I run these tests with npm test, and Mocha gives me the results, confirming if my logic holds up.

    Key Takeaways

    • Preparation is Key: Just like solving a math problem, setting up your testing environment with npm scripts ensures you have a solid foundation.
    • Breaking Down Code: Writing tests allows you to break down your code into understandable, testable parts, ensuring each function does exactly what it’s meant to do.
    • Iterate and Improve: Tests give immediate feedback, letting you refine and improve your code just like reworking a math problem until it’s correct.