Skip to content

hkevin01/javascript-unit-test-jasmine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

JavaScript Unit Testing with Jasmine

Build Status License: MIT JavaScript Jasmine

A comprehensive demonstration project for JavaScript unit testing using the Jasmine testing framework. This project showcases behavior-driven development (BDD) principles, testing best practices, and provides practical examples for learning effective testing strategies.

🎯 Project Overview

This repository serves as a complete guide and reference for JavaScript unit testing with Jasmine. It includes:

  • Real-world examples of classes and functions with comprehensive test suites
  • Best practices for organizing and structuring tests
  • Advanced testing patterns including spies, mocks, and asynchronous testing
  • CI/CD integration with GitHub Actions
  • Comprehensive documentation and guides

πŸš€ Quick Start

Prerequisites

  • Web browser (Chrome, Firefox, Safari, or Edge)
  • Git
  • Node.js (optional, for npm scripts and CI/CD)

Installation

  1. Clone the repository:

    git clone git@github.com:hkevin01/javascript-unit-test-jasmine.git
    cd javascript-unit-test-jasmine
  2. Open in browser:

    # Open SpecRunner.html in your browser
    open SpecRunner.html
    # or
    firefox SpecRunner.html
  3. Or use a local server (recommended):

    # Using Python 3
    python -m http.server 8000
    
    # Using Node.js (if you have http-server installed)
    npx http-server
    
    # Then open http://localhost:8000/SpecRunner.html

πŸ“ Project Structure

javascript-unit-test-jasmine/
β”œβ”€β”€ .github/                 # GitHub workflows and templates
β”‚   β”œβ”€β”€ workflows/
β”‚   β”‚   └── test.yml        # CI/CD pipeline
β”‚   └── ISSUE_TEMPLATE.md   # Bug report template
β”œβ”€β”€ .copilot/               # GitHub Copilot configuration
β”‚   └── copilot-instructions.md
β”œβ”€β”€ assets/                 # Static assets
β”‚   β”œβ”€β”€ css/
β”‚   β”‚   └── jasmine-custom.css  # Custom Jasmine styling
β”‚   β”œβ”€β”€ images/             # Project images
β”‚   └── lib/                # Third-party libraries
β”œβ”€β”€ docs/                   # Documentation
β”‚   β”œβ”€β”€ project-plan.md     # Project roadmap
β”‚   └── testing-guide.md    # Comprehensive testing guide
β”œβ”€β”€ spec/                   # Test specifications
β”‚   β”œβ”€β”€ support/            # Test support files
β”‚   └── *.spec.js          # Test files
β”œβ”€β”€ src/                    # Source code
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── Person.js       # Person class example
β”‚   β”œβ”€β”€ utils/              # Utility functions
β”‚   └── Calculator.js       # Calculator class example
β”œβ”€β”€ .gitignore             # Git ignore rules
β”œβ”€β”€ SpecRunner.html        # Jasmine test runner
└── README.md              # This file

πŸ§ͺ Example Classes

Calculator Class

A comprehensive calculator with mathematical operations:

  • Basic arithmetic (add, subtract, multiply, divide)
  • Advanced operations (square root, power)
  • Error handling (division by zero, negative square roots)
  • Operation history tracking
  • Power state management

Person Class

An object-oriented example demonstrating:

  • Constructor patterns and property management
  • Friend and hobby management
  • Input validation and error handling
  • String manipulation and formatting
  • Complex object interactions

πŸ“š Learning Path

1. Basic Testing Concepts

  • Understanding describe and it blocks
  • Writing your first test
  • Basic matchers and assertions

2. Intermediate Patterns

  • Setup and teardown with beforeEach/afterEach
  • Testing error conditions
  • Custom matchers

3. Advanced Techniques

  • Spies and mocking
  • Asynchronous testing
  • Integration testing

4. Best Practices

  • Test organization
  • Performance considerations
  • Maintainable test code

πŸ”§ Testing Features Demonstrated

Core Jasmine Features

  • Describe blocks for test organization
  • It blocks for individual specifications
  • Matchers for assertions (toEqual, toBe, toContain, etc.)
  • Setup/Teardown with before/after hooks
  • Spies for function monitoring and mocking
  • Custom matchers for domain-specific assertions

Testing Patterns

  • AAA Pattern (Arrange, Act, Assert)
  • Test isolation and independence
  • Edge case testing
  • Error condition testing
  • Asynchronous code testing

🎨 Custom Styling

The project includes custom CSS styling for the Jasmine test runner with:

  • Modern, clean interface
  • Responsive design for mobile devices
  • Color-coded test results
  • Progress indicators
  • Enhanced readability

πŸ”„ Continuous Integration

Automated testing with GitHub Actions:

  • Runs tests on multiple Node.js versions (16.x, 18.x, 20.x)
  • Generates coverage reports
  • Validates code quality
  • Automatic deployment of test results

πŸ“– Documentation

🀝 Contributing

Contributions are welcome! Please read our contributing guidelines:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

🌟 Acknowledgments

πŸ“ž Support

If you have questions or need help:

  1. Check the documentation
  2. Browse existing issues
  3. Create a new issue using our template

πŸš€ Next Steps

Ready to start testing? Here's what you can do:

  1. Explore the examples - Look at the Calculator and Person classes
  2. Run the tests - Open SpecRunner.html and see tests in action
  3. Read the guides - Check out our comprehensive testing documentation
  4. Write your own tests - Use our examples as templates for your projects
  5. Contribute - Help improve this learning resource

Happy Testing! πŸ§ͺ✨

About

A comprehensive demonstration project for JavaScript unit testing using the Jasmine testing framework. This project showcases behavior-driven development (BDD) principles, testing best practices, and provides practical examples for learning effective testing strategies.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors