CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jest-expect-message

Add custom message to Jest expects

Overall
score

99%

Overview
Eval results
Files

task.mdevals/scenario-8/

API Response Validator Test Suite

Overview

Build a test suite for an API response handler that processes data from multiple external services. The handler returns structured response objects that need to be validated against expected patterns. Your tests should verify the response structure and content using flexible pattern matching that checks types and partial content without requiring exact value matches.

Dependencies { .dependencies }

jest-expect-message { .dependency }

Provides enhanced assertion messaging for Jest tests.

Requirements

Module Under Test

Create a module api-handler.js that exports a processApiResponse function:

function processApiResponse(apiName, rawData) {
  const timestamp = Date.now();

  return {
    source: apiName,
    timestamp: timestamp,
    data: rawData,
    metadata: {
      processedAt: new Date().toISOString(),
      version: "1.0"
    }
  };
}

module.exports = { processApiResponse };

Test Suite Requirements

Create a test file api-handler.test.js with tests that verify the response structure using flexible pattern matching:

  1. Response Structure Test: Verify that the processed response has the correct shape with appropriate types for each field, without hardcoding specific timestamp or date values.

  2. Partial Data Matching: Test that the response contains expected data properties while allowing additional properties to exist.

  3. Array Content Validation: When the raw data includes arrays, verify they contain elements of the expected types.

  4. Nested Object Patterns: Test that nested objects (like metadata) match expected patterns with correct property types.

Test Requirements

  • Use custom error messages for all assertions to clarify what pattern is being validated
  • Validate object structures using type matchers rather than exact values
  • Test with data that includes numbers, strings, arrays, and nested objects
  • Write at least 4 distinct test cases

Test Cases

Test Case 1: Basic Response Structure { .testcase @test }

Input:

const response = processApiResponse("weather-api", { temp: 72, conditions: "sunny" });

Expected Output: Response should match pattern:

{
  source: "weather-api",
  timestamp: <any number>,
  data: { temp: 72, conditions: "sunny" },
  metadata: {
    processedAt: <any string>,
    version: "1.0"
  }
}

Test Case 2: Response with Array Data { .testcase @test }

Input:

const response = processApiResponse("users-api", {
  users: [
    { id: 1, name: "Alice" },
    { id: 2, name: "Bob" }
  ]
});

Expected Output:

  • Response data should contain an array with objects
  • Each array element should have numeric id and string name properties
  • Should match pattern without exact value comparison

Test Case 3: Partial Object Matching { .testcase @test }

Input:

const response = processApiResponse("products-api", {
  productId: 101,
  name: "Widget",
  price: 29.99,
  inStock: true
});

Expected Output:

  • Response data should contain at least productId (number) and name (string)
  • Pattern matching should succeed even with additional properties present
  • Should verify types without checking exact values

Test Case 4: Nested Metadata Validation { .testcase @test }

Input:

const response = processApiResponse("orders-api", { orderId: 5001 });

Expected Output:

  • Response metadata should be an object
  • Metadata should contain processedAt as a string and version as "1.0"
  • Should match expected nested structure pattern

Deliverables

  1. api-handler.js - The API handler module (code provided above)
  2. api-handler.test.js - Test suite using pattern matching with custom messages
  3. All tests should pass when run with Jest

Install with Tessl CLI

npx tessl i tessl/npm-jest-expect-message

tile.json