This repository contains a JavaScript implementation of a Queue data structure inspired by Princeton's Intro to Programming. You are the developer and must write robust unit tests using Jest.
Bug: The
dequeue()method does not throw an error when called on an empty queue. This violates expected behavior and must be caught through testing.
Write Jest unit tests to achieve 100% statement and branch coverage for the following methods:
- Constructor
enqueuedequeuepeeklengthisEmptyremoveAll
- Use proper Jest assertions.
- Validate method outputs and side effects (e.g., queue length).
- Cover all true/false branches.
npm test
npm test -- --coverage- Screenshot of code coverage report showing 100%.
Write a test that expects dequeue() to throw an error on an empty queue:
expect(() => queue.dequeue()).toThrow("Queue is empty");This test should fail, revealing a bug due to the missing exception.
- Screenshot of coverage report showing failure due to this test.
- GitHub repository link with code and tests.
- Screenshot of coverage after Task 1.
- Screenshot of coverage after Task 2.
- Ensure files are logically organized (e.g.,
src/Queue.js,tests/Queue.test.js).
- What is the purpose of
isEmpty()in a queue?
- How does checking
length()help validateenqueue()anddequeue()?
- If
removeAll()clears the queue, what test cases should you design?
- How would your tests change if
dequeue()was asynchronous (e.g.,await dequeueAsync())?
To generate coverage reports:
- Run:
npm test -- --coverage- Note the "--" !
- Check:
coverage/lcov-report/index.html
Type of Fault in
dequeue(): Omission Fault
This is a semantic fault or behavioral fault, more specifically an omission fault, where a necessary operation (throwing an error when the queue is empty) is missing from the implementation.
In software engineering, implementation faults can be classified in several ways:
| Classification Type | Description |
|---|---|
| Omission Fault | A required operation is missing. |
| Commission Fault | An incorrect operation is performed. |
| Timing Fault | An operation is performed at an incorrect time. |
| Syntactic Fault | The code violates the syntax rules of the language. |
| Semantic Fault | The code compiles and runs but does not behave as expected. |
| Classification Type | Queue-specific Example |
|---|---|
| Omission Fault | dequeue() does not throw an error on an empty queue. |
| Commission Fault | enqueue() adds a new item to the front instead of the back. |
| Timing Fault | Queue is cleared before dequeue() is called due to misplacement of removeAll(). |
| Syntactic Fault | A missing closing brace } or a typo like funtion() in Queue.js. |
| Semantic Fault | length() returns this.items.length + 1 instead of the actual length. |
In a briad categorization, other faults that can exists in a SDLC is Specification, Design (Logical), Integration, Regression.