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-9/

Custom Matchers for Product Validation

Build a validation system for an e-commerce product catalog that uses custom Jest matchers to verify product data integrity.

Requirements

You need to implement custom Jest matchers that validate product objects according to business rules. Product objects have the following structure:

{
  name: string,
  price: number,
  category: string,
  stock: number,
  tags: string[]
}

Custom Matchers

Implement the following custom matchers:

  1. toBeValidPrice: Checks if a number is a valid price (positive, at most 2 decimal places)
  2. toBeInStock: Checks if a product has stock available (stock > 0)
  3. toHaveCategory: Checks if a product belongs to a specific category
  4. toHaveAllTags: Checks if a product has all specified tags

All matchers should work with .not negation and provide clear error messages when assertions fail.

Test Cases

Your implementation should pass the following test cases:

  • A price of 19.99 should be validated as a valid price @test
  • A price of -5 should fail validation as an invalid price @test
  • A product with stock of 10 should be considered in stock @test
  • A product with stock of 0 should not be considered in stock @test
  • A product with category "electronics" should match that category @test
  • A product with tags ["new", "sale", "featured"] should have all tags ["new", "sale"] @test

Implementation

@generates

API

/**
 * Sets up custom Jest matchers for product validation.
 * Call this function to register all custom matchers with Jest.
 */
function setupMatchers() {
  // Implementation here
}

module.exports = { setupMatchers };

Dependencies { .dependencies }

jest-expect-message { .dependency }

Provides custom message support for Jest assertions, including compatibility with custom matchers added via expect.extend.

@satisfied-by

Install with Tessl CLI

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

tile.json