Skip to content

Instantly share code, notes, and snippets.

@ninadpchaudhari
Last active April 23, 2025 17:30
Show Gist options
  • Save ninadpchaudhari/405751aa6accdb15e850d7cec74586c5 to your computer and use it in GitHub Desktop.
Save ninadpchaudhari/405751aa6accdb15e850d7cec74586c5 to your computer and use it in GitHub Desktop.

Jest Assignment: JavaScript Queue

Project Description

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.


Task 1: Create Passing Test Cases

Write Jest unit tests to achieve 100% statement and branch coverage for the following methods:

  • Constructor
  • enqueue
  • dequeue
  • peek
  • length
  • isEmpty
  • removeAll

Your tests should:

  • Use proper Jest assertions.
  • Validate method outputs and side effects (e.g., queue length).
  • Cover all true/false branches.

How to run:

npm test
npm test -- --coverage

Upload Requirements:

  • Screenshot of code coverage report showing 100%.

Task 2: Create a Failing Test Case

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.

Upload Requirements:

  • Screenshot of coverage report showing failure due to this test.

Required Submission

  1. GitHub repository link with code and tests.
  2. Screenshot of coverage after Task 1.
  3. Screenshot of coverage after Task 2.
  4. Ensure files are logically organized (e.g., src/Queue.js, tests/Queue.test.js).

Depth of Knowledge (DOK) Questions

Recall

  • What is the purpose of isEmpty() in a queue?

Skill/Concept

  • How does checking length() help validate enqueue() and dequeue()?

Strategic Thinking

  • If removeAll() clears the queue, what test cases should you design?

Extended Thinking

  • How would your tests change if dequeue() was asynchronous (e.g., await dequeueAsync())?

References


Notes

To generate coverage reports:

  • Run: npm test -- --coverage
    • Note the "--" !
  • Check: coverage/lcov-report/index.html

Fault Classification

Type of Fault in dequeue() : Omission Fault

Types of faults:

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.


Implementation Faults Classification

In software engineering, implementation faults can be classified in several ways:

Classification Types

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.

Queue-related Examples

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment