{"library":"mocha","title":"Mocha Test Framework","description":"Mocha is a highly flexible and feature-rich JavaScript test framework designed for both Node.js environments and browsers. It facilitates writing and running tests with support for various styles, including Behavior Driven Development (BDD) and Test Driven Development (TDD). The current stable version is 11.7.5, with frequent beta releases for version 12 indicating active development. Mocha differentiates itself by being unopinionated about the assertion library, allowing developers to choose their preferred tools (e.g., Chai, Node's built-in `assert`). It provides robust capabilities for asynchronous testing, hooks for setup/teardown, and comprehensive reporting, making it a popular choice for defining test suites and individual test cases.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install mocha"],"cli":{"name":"mocha","version":null}},"imports":["// No import needed, these are global by default in Mocha tests.\n// For TypeScript, install @types/mocha and configure tsconfig for 'types'.","import Mocha from 'mocha';\n// or\nconst Mocha = require('mocha');","// In package.json: { \"type\": \"module\" }\nimport { myFunction } from './myModule.js';\n\ndescribe('ESM Test', () => {\n  it('should run an ES module test', () => {\n    // ...\n  });\n});"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { expect } from 'chai';\n\n// A simple utility function to test\nfunction add(a: number, b: number): number {\n  return a + b;\n}\n\nfunction subtract(a: number, b: number): number {\n  return a - b;\n}\n\ndescribe('Math Operations', () => {\n  it('should correctly add two numbers', () => {\n    expect(add(2, 3)).to.equal(5);\n    expect(add(-1, 1)).to.equal(0);\n  });\n\n  it('should correctly subtract two numbers', () => {\n    expect(subtract(5, 2)).to.equal(3);\n    expect(subtract(10, 20)).to.equal(-10);\n  });\n\n  it('should handle zero correctly in addition', () => {\n    expect(add(0, 7)).to.equal(7);\n  });\n\n  // Demonstrate an asynchronous test using Promises\n  it('should eventually return a value after a delay', async () => {\n    const delayedAdd = (a: number, b: number, delay: number) => {\n      return new Promise<number>((resolve) => {\n        setTimeout(() => resolve(a + b), delay);\n      });\n    };\n    const result = await delayedAdd(1, 1, 50);\n    expect(result).to.equal(2);\n  }).timeout(100); // Set a specific timeout for this async test\n});","lang":"typescript","description":"This quickstart demonstrates how to set up a basic Mocha test file using TypeScript and Chai for assertions. It includes synchronous and asynchronous test examples with `describe` and `it` blocks, illustrating common testing patterns.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}