describe() is used to group related tests (test suite), while it() defines individual test cases. Example: describe('Calculator', () => { it('should add numbers correctly', () => { /* test */ }); }). They help organize tests hierarchically and provide clear test structure.
Mocha works with various assertion libraries: 1) Node's assert module, 2) Chai for BDD/TDD assertions, 3) Should.js for BDD style, 4) Expect.js for expect() style. Example with Chai: const { expect } = require('chai'); expect(value).to.equal(expected);
Tests can be skipped/pending using: 1) it.skip() - skip test, 2) describe.skip() - skip suite, 3) it() without callback - mark pending, 4) .only() - run only specific tests. Example: it.skip('test to skip', () => { /* test */ });
Exclusive tests using .only(): 1) it.only() runs only that test, 2) describe.only() runs only that suite, 3) Multiple .only() creates subset of tests to run, 4) Useful for debugging specific tests. Example: it.only('exclusive test', () => { /* test */ });
Parallel execution: 1) Use --parallel flag, 2) Configure worker count, 3) Handle shared resources, 4) Manage test isolation, 5) Consider file-level parallelization. Improves test execution speed.
Custom interfaces: 1) Define interface methods, 2) Register with Mocha, 3) Handle test definition, 4) Manage context, 5) Support hooks and suites. Allows custom test syntax.
Suite composition: 1) Share common tests, 2) Extend test suites, 3) Compose test behaviors, 4) Manage suite hierarchy, 5) Handle shared context. Enables test reuse.
Mocha provides hooks: 1) before() - runs once before all tests, 2) beforeEach() - runs before each test, 3) after() - runs once after all tests, 4) afterEach() - runs after each test. Used for setup and cleanup operations. Example: beforeEach(() => { /* setup */ });
Test retries configured through: 1) this.retries(n) in test/suite, 2) --retries option in CLI, 3) Retries count for failed tests, 4) Useful for flaky tests, 5) Can be set globally or per test. Example: this.retries(3);
File setup options: 1) Use before/after hooks, 2) Require helper files, 3) Use mocha.opts for configuration, 4) Implement setup modules, 5) Use root hooks plugin. Ensures proper test environment setup.
Config options include: 1) .mocharc.js/.json file, 2) package.json mocha field, 3) CLI arguments, 4) Environment variables, 5) Programmatic options. Control test execution, reporting, and behavior.
Custom reporters: 1) Extend Mocha's Base reporter, 2) Implement required methods, 3) Handle test events, 4) Format output as needed, 5) Register reporter with Mocha. Allows customized test reporting.
Data management: 1) Use fixtures, 2) Implement data factories, 3) Clean up test data, 4) Isolate test data, 5) Manage data dependencies. Ensures reliable test execution.
Advanced filtering: 1) Use regex patterns, 2) Filter by suite/test name, 3) Implement custom grep, 4) Use test metadata, 5) Filter by file patterns. Helps focus test execution.
Event testing patterns: 1) Listen for events, 2) Verify event data, 3) Test event ordering, 4) Handle event timing, 5) Test error events. Important for event-driven code.
Setup involves: 1) Installing Mocha: npm install --save-dev mocha, 2) Adding test script to package.json: { 'scripts': { 'test': 'mocha' } }, 3) Creating test directory, 4) Choosing assertion library (e.g., Chai), 5) Creating test files with .test.js or .spec.js extension.
Mocha offers various reporters: 1) spec - hierarchical test results, 2) dot - minimal dots output, 3) nyan - fun nyan cat reporter, 4) json - JSON test results, 5) html - HTML test report. Select using --reporter option or configure in mocha.opts.
Dynamic tests created by: 1) Generating it() calls in loops, 2) Using test data arrays, 3) Programmatically creating describe blocks, 4) Using forEach for test cases, 5) Generating tests from data sources.
Mocha is a feature-rich JavaScript test framework running on Node.js and browser. Key features include: 1) Flexible test structure with describe/it blocks, 2) Support for asynchronous testing, 3) Multiple assertion library support, 4) Test hooks (before, after, etc.), 5) Rich reporting options, 6) Browser support, 7) Plugin architecture.
Timeout handling: 1) Set suite timeout: this.timeout(ms), 2) Set test timeout: it('test', function(done) { this.timeout(ms); }), 3) Default is 2000ms, 4) Set to 0 to disable timeout, 5) Can be set globally or per test.
Common CLI options: 1) --watch for watch mode, 2) --reporter for output format, 3) --timeout for test timeout, 4) --grep for filtering tests, 5) --bail to stop on first failure, 6) --require for requiring modules. Example: mocha --watch --reporter spec
Root hook plugin: 1) Runs hooks for all test files, 2) Configured in mocha.opts or CLI, 3) Used for global setup/teardown, 4) Affects all suites, 5) Useful for shared resources. Example: --require ./root-hooks.js
Organization practices: 1) Group related tests in describes, 2) Use clear test descriptions, 3) Maintain test independence, 4) Follow consistent naming, 5) Structure tests hierarchically. Improves maintainability.
Complex async handling: 1) Chain promises properly, 2) Manage async timeouts, 3) Handle parallel operations, 4) Control execution flow, 5) Implement proper error handling. Important for reliable async tests.