Mocha Testing

Mocha Testing

ComeOnOliver Updated 61 repo stars

File contents

Mocha Testing

Setup

npm install -D mocha @types/mocha chai ts-node

.mocharc.yml

require: ts-node/register
spec: "test/**/*.test.ts"
timeout: 5000
recursive: true

Tests

import { expect } from 'chai';

describe('Calculator', () => {
    let calc: Calculator;
    beforeEach(() => { calc = new Calculator(); });

    it('should add numbers', () => { expect(calc.add(2, 3)).to.equal(5); });
    it('should throw on div by zero', () => {
        expect(() => calc.divide(1, 0)).to.throw('Division by zero');
    });
});

describe('API', () => {
    it('should fetch data', async () => {
        const data = await api.get('/users');
        expect(data).to.be.an('array');
    });
});

Hooks: before, after, beforeEach, afterEach

Run: npx mocha / npx mocha --grep "pattern" / npx mocha --watch

ComeOnOliver/skillshub/tree/main/skills/skillshub-team/catalog-batch5/mocha-testing commit 44ca47d037

Frequently asked questions

npx skillmds@latest add comeonoliver/mocha-testing