Give a detailed example of documenting a JSDoc class with unit tests written in JS and…
- User
Give a detailed example of documenting a JSDoc class with unit tests written in JS and using the jest testing framework.
Assistant
I'll provide a comprehensive example of documenting a JSDoc class with unit tests using Jest. We'll create a simple
BankAccountclass to demonstrate.First, let's create the
BankAccount.jsfile:/** * Represents a bank account with basic financial operations. * @class */ class BankAccount { /** * Create a new bank account. * @constructor * @param {string} accountHolder - The name of the account holder * @param {number} [initialBalance=0] - The initial balance of the account */ constructor(accountHolder, initialBalance = 0) { /** * The name of the account holder * @type {string} * @private */ this._accountHolder = accountHolder; /** * The current balance of the account * @type {number} * @private */ this._balance = initialBalance; } /** * Deposit money into the account. * @param {number} amount - The amount to deposit * @throws {Error} If the deposit amount is negative or zero * @returns {number} The new account balance */ deposit(amount) { if (amount <= 0) { throw new Error('Deposit amount must be positive'); } this._balance += amount; return this._balance; } /** * Withdraw money from the account. * @param {number} amount - The amount to withdraw * @throws {Error} If the withdrawal amount is negative, zero, or exceeds the current balance * @returns {number} The new account balance */ withdraw(amount) { if (amount <= 0) { throw new Error('Withdrawal amount must be positive'); } if (amount > this._balance) { throw new Error('Insufficient funds'); } this._balance -= amount; return this._balance; } /** * Get the current account balance. * @returns {number} The current balance */ getBalance() { return this._balance; } /** * Get the account holder's name. * @returns {string} The name of the account holder */ getAccountHolder() { return this._accountHolder; } } module.exports = BankAccount;Now, let's create the corresponding test file
BankAccount.test.js:const BankAccount = require('./BankAccount'); describe('BankAccount', () => { /** * Test suite for BankAccount class constructor */ describe('Constructor', () => { /** * Should create an account with initial balance */ test('creates account with initial balance', () => { const account = new BankAccount('John Doe', 100); expect(account.getAccountHolder()).toBe('John Doe'); expect(account.getBalance()).toBe(100); }); /** * Should create an account with zero balance by default */ test('creates account with zero balance by default', () => { const account = new BankAccount('Jane Doe'); expect(account.getAccountHolder()).toBe('Jane Doe'); expect(account.getBalance()).toBe(0); }); }); /** * Test suite for deposit method */ describe('Deposit', () => { /** * Should successfully deposit positive amount */ test('deposits positive amount correctly', () => { const account = new BankAccount('John Doe', 50); const newBalance = account.deposit(100); expect(newBalance).toBe(150); expect(account.getBalance()).toBe(150); }); /** * Should throw error for zero deposit */ test('throws error for zero deposit', () => { const account = new BankAccount('John Doe'); expect(() => account.deposit(0)).toThrow('Deposit amount must be positive'); }); /** * Should throw error for negative deposit */ test('throws error for negative deposit', () => { const account = new BankAccount('John Doe'); expect(() => account.deposit(-50)).toThrow('Deposit amount must be positive'); }); }); /** * Test suite for withdraw method */ describe('Withdraw', () => { /** * Should successfully withdraw amount */ test('withdraws amount correctly', () => { const account = new BankAccount('John Doe', 200); const newBalance = account.withdraw(100); expect(newBalance).toBe(100); expect(account.getBalance()).toBe(100); }); /** * Should throw error for insufficient funds */ test('throws error for insufficient funds', () => { const account = new BankAccount('John Doe', 50); expect(() => account.withdraw(100)).toThrow('Insufficient funds'); }); /** * Should throw error for zero withdrawal */ test('throws error for zero withdrawal', () => { const account = new BankAccount('John Doe', 100); expect(() => account.withdraw(0)).toThrow('Withdrawal amount must be positive'); }); }); });To set this up, you'll need to:
- Install Jest:
npm install --save-dev jest- Add a test script to
package.json:
{ "scripts": { "test": "jest" } }- Run tests:
npm testKey JSDoc and Testing Highlights:
- Comprehensive class and method documentation
- Use of
@class,@constructor,@param,@returns,@throwstags - Private properties marked with underscore
- Type annotations for parameters and return values
- Descriptive test cases covering various scenarios
- Error case testing
- Nested
describeblocks for logical test grouping
This example demonstrates:
- Thorough documentation using JSDoc
- Comprehensive unit testing with Jest
- Error handling
- Different test scenarios (success, edge cases, error conditions)
The tests cover:
- Constructor behavior
- Deposit method (positive, zero, and negative amounts)
- Withdraw method (successful withdrawal, insufficient funds)
- Getter methods
Remember that good documentation and testing help improve code quality, make the code more maintainable, and catch potential issues early in the development process.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.