Skip to content

Instantly share code, notes, and snippets.

@DHANRAJCHOUDHARY244
Created July 3, 2024 11:47
Show Gist options
  • Save DHANRAJCHOUDHARY244/80eb6615bfe7364381f9fbb1557dced5 to your computer and use it in GitHub Desktop.
Save DHANRAJCHOUDHARY244/80eb6615bfe7364381f9fbb1557dced5 to your computer and use it in GitHub Desktop.
Why Choosing Jest

Why we Go with jest

Favor for Jest: Speed and Performance

Against Mocha: Sequential Test Execution

Jest:

Jest runs tests in parallel by default, making it significantly faster for large test suites. It also uses intelligent test caching to minimize unnecessary test runs.

// Jest example
// __tests__/sum.test.js
const sum = require('../sum');

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

// __tests__/difference.test.js
const difference = require('../difference');

test('subtracts 5 - 2 to equal 3', () => {
  expect(difference(5, 2)).toBe(3);
});

// Running `jest` will execute these tests in parallel.

Mocha:

Mocha runs tests sequentially by default, which can be slower, especially for large test suites.

// Mocha example
// test/sum.test.js
const assert = require('assert');
const sum = require('../sum');

describe('Sum', function () {
  it('should return 3 when adding 1 and 2', function () {
    assert.strictEqual(sum(1, 2), 3);
  });
});

// test/difference.test.js
const difference = require('../difference');

describe('Difference', function () {
  it('should return 3 when subtracting 2 from 5', function () {
    assert.strictEqual(difference(5, 2), 3);
  });
});

// Running `mocha` will execute these tests sequentially.

Favor for Jest: Simplicity and Integration

Against Mocha: More Setup Required

Jest:

Jest is an all-in-one solution, including a test runner, assertion library, and mocking/stubbing tools out of the box.

// Jest example
// __tests__/sum.test.js
const sum = require('../sum');

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

// No additional setup required.

Mocha:

Mocha requires separate setup for the test runner, assertion library, and mocking/stubbing tools.

// Mocha example
// test/sum.test.js
const assert = require('assert');
const sum = require('../sum');

describe('Sum', function () {
  it('should return 3 when adding 1 and 2', function () {
    assert.strictEqual(sum(1, 2), 3);
  });
});

// Additional setup for assertions and mocking needed.

Favor for Jest: Built-in Mocking

Against Mocha: Manual Mocking

Jest:

Jest provides built-in mocking capabilities, making it easier to test functions that depend on external modules, APIs, or databases.

// Jest example
// __tests__/api.test.js
const api = require('../api');
jest.mock('../api');

test('calls API and returns data', async () => {
  api.fetchData.mockResolvedValue({ data: 'mocked data' });
  
  const result = await api.fetchData();
  expect(result.data).toBe('mocked data');
});

Mocha:

Mocha requires third-party libraries like Sinon for mocking, adding to the setup complexity.

// Mocha example
// test/api.test.js
const sinon = require('sinon');
const assert = require('assert');
const api = require('../api');

describe('API', function () {
  it('should call API and return data', async function () {
    const fetchDataStub = sinon.stub(api, 'fetchData').resolves({ data: 'mocked data' });
    
    const result = await api.fetchData();
    assert.strictEqual(result.data, 'mocked data');
    
    fetchDataStub.restore();
  });
});

Favor for Jest: Snapshot Testing

Against Mocha: No Built-in Snapshot Testing

Jest:

Jest's built-in snapshot testing is useful for ensuring that large objects, especially UI components or API responses, do not change unexpectedly.

// Jest example
// __tests__/snapshot.test.js
const api = require('../api');

test('matches the snapshot', () => {
  const data = api.fetchData();
  expect(data).toMatchSnapshot();
});

Mocha:

Mocha does not have built-in support for snapshot testing, requiring additional libraries and setup.

// Mocha example
// test/snapshot.test.js
const assert = require('assert');
const api = require('../api');
const fs = require('fs');

describe('Snapshot', function () {
  it('should match the snapshot', function () {
    const data = api.fetchData();
    const snapshot = JSON.parse(fs.readFileSync('snapshot.json', 'utf8'));
    
    assert.deepStrictEqual(data, snapshot);
  });
});

Favor for Jest: Great Developer Experience

Against Mocha: Complex Test Environment Setup

Jest:

Jest provides clear and descriptive error messages and output, along with a powerful watch mode that reruns tests related to changed files.

// Jest example
// __tests__/error.test.js
const sum = require('../sum');

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(4); // This will fail and provide a clear error message.
});

// Running `jest --watch` will automatically rerun tests on file changes.

Mocha:

Setting up a comprehensive test environment with Mocha often requires more effort and can be less straightforward.

// Mocha example
// test/error.test.js
const assert = require('assert');
const sum = require('../sum');

describe('Sum', function () {
  it('should return 3 when adding 1 and 2', function () {
    assert.strictEqual(sum(1, 2), 4); // This will fail but might not provide as clear an error message.
  });
});

// Additional configuration needed for a similar watch mode.

Conclusion

For projects prioritizing speed, simplicity, and ease of integration, Jest is generally the better choice. Its all-in-one approach, parallel execution, built-in mocking, and snapshot testing capabilities provide a more streamlined and efficient testing experience.

On the other hand, Mocha offers greater flexibility and customization but comes with the trade-offs of increased setup complexity, slower default execution, and the need for multiple dependencies. If your project has very specific testing requirements that Jest cannot meet, Mocha might be worth considering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment