Skip to content

gridu/Advanced-Unit-Testing-Capstone-project-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Capstone Project: Advanced Unit Testing with Jest

Welcome to your final project! 🎉

In this capstone, you will apply everything you learned about unit testing in JavaScript using Jest.
The project contains several small but realistic modules — your job is to write comprehensive tests for them.


📂 Project Structure

advanced-unit-testing/
│── package.json # Project setup with Jest
│── jest.config.js # Jest configuration
│── README.md # Instructions (this file)
│
├── src/utils # Source code (functions/classes to test)
│ ├── math.js
│ ├── stringUtils.js
│ ├── userService.js
│ ├── asyncService.js
│ └── eventEmitter.js
│
└── tests/ # Your test files (currently empty)
├── math.test.js
├── stringUtils.test.js
├── userService.test.js
├── asyncService.test.js
└── eventEmitter.test.js


🧑‍💻 Your Mission

Your goal is to write Jest tests inside the tests/ folder.
Each source file in src/ has a corresponding empty test file.

The challenge is to test:

  • Pure functions (no dependencies, just logic).
  • Async functions (Promises, async/await).
  • Class methods with dependencies (mocking).
  • Event-driven behavior (event emitters).

📌 Task Breakdown

1. math.jsBasic Functions

Functions: add, divide, factorial

👉 What to test:

  • ✅ Normal cases (e.g., add(2, 3) → 5)
  • ✅ Edge cases (e.g., divide by zero should throw an error)
  • ✅ Recursion & boundaries (e.g., factorial(0) → 1, negative numbers should throw)

2. stringUtils.jsString Manipulation

Functions: capitalize, sanitize, reverse

👉 What to test:

  • ✅ Capitalization of lowercase & mixed case words
  • sanitize removes HTML tags (but leaves plain text)
  • reverse works with normal & special characters

3. userService.jsService with Dependency

Class: UserService (depends on a database object)

👉 What to test:

  • createUser creates a valid user object
  • ✅ Throws error if input is missing
  • getUser calls database and returns result
  • ✅ Use mocks (Jest fn() or jest.mock) instead of a real database

4. asyncService.jsAsync Testing

Function: fetchData(url)

👉 What to test:

  • ✅ Resolves with correct response for a valid URL
  • ✅ Rejects with an error for invalid URL
  • ✅ Works with async/await and .then/.catch

5. eventEmitter.jsEvent-Driven Behavior

Class: MyEmitter (extends EventEmitter)

👉 What to test:

  • ✅ Event listeners (message event should receive the message)
  • ✅ Error handling (error event emits errors)
  • ✅ Multiple listeners get called correctly

🚀 Getting Started

Install dependencies:

   npm install

Run all tests:

npm run test 

Run only one file:

npm run test math.test.js

🚀 Good Luck

About

In this capstone, you will apply everything you learned about unit testing in JavaScript using Jest. The project contains several small but realistic modules — your job is to write comprehensive tests for them.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors