Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
.DS_Store

# Testing
node_modules/
coverage/
.jest-cache/
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "battleship-game",
"version": "1.0.0",
"description": "Battleship game with comprehensive unit tests",
"type": "module",
"scripts": {
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test:watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch",
"test:coverage": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage"
},
"jest": {
"testEnvironment": "jsdom",
"transform": {},
"extensionsToTreatAsEsm": [".js"],
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
"coveragePathIgnorePatterns": [
"/node_modules/",
"/__tests__/"
],
"testMatch": [
"**/__tests__/**/*.test.js"
]
},
"devDependencies": {
"jest": "^29.0.0",
"jest-environment-jsdom": "^29.0.0"
}
}
52 changes: 52 additions & 0 deletions src/__tests__/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Battleship Game Test Suite

This directory contains comprehensive unit tests for all modules in the Battleship game.

## Test Coverage

### Core Modules
- **constants.test.js** - Tests for game constants, ship definitions, and configuration
- **ship.test.js** - Tests for Ship class including hit tracking and sinking logic
- **board.test.js** - Tests for Board class including placement, attacks, and coordinate management
- **ai-controller.test.js** - Tests for AI targeting logic and hunt/target strategy
- **ui-controller.test.js** - Tests for UI rendering and DOM manipulation
- **game.test.js** - Tests for main game controller and game flow

## Running Tests

```bash
# Install dependencies (Jest)
npm install

# Run all tests
npm test

# Run tests in watch mode
npm run test:watch

# Run tests with coverage report
npm run test:coverage
```

## Test Structure

Each test file follows a consistent structure:
- **Unit tests** for individual methods and functions
- **Integration tests** for component interactions
- **Edge case tests** for boundary conditions and error handling

## Test Coverage Goals

The test suite aims for:
- High code coverage (>90%)
- Comprehensive edge case testing
- Clear, descriptive test names
- Proper mocking of dependencies
- Fast execution times

## Notes

- Tests use Jest with ES modules support
- DOM manipulation is tested with jsdom environment
- Mocks are created for UI controllers to isolate game logic
- Random behavior is tested probabilistically
Loading