CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jest-each

Parameterised tests for Jest that enable running the same test multiple times with different data sets using arrays or tagged template literals

85

1.10x
Overview
Eval results
Files

task.mdevals/scenario-7/

User Service Test Suite

Create a test suite for a user service that validates data handling with external API dependencies.

Context

You are testing a UserService class that depends on an external API client. The service has methods that perform various operations and you need to test these operations by mocking the API client's behavior.

Requirements

Create a test file that tests a UserService class with the following behavior:

UserService Class

The service will have these methods that you need to test:

  • getUserById(id) - returns user data for a given ID
  • createUser(userData) - creates a new user and returns the created user object
  • deleteUser(id) - deletes a user and throws an error if the user doesn't exist

Test Cases

Your test suite should include the following test cases:

  1. Test synchronous return values: When calling getUserById(), the mocked API client should return a user object { id: 1, name: 'Alice', email: 'alice@example.com' } @test

  2. Test async resolved values: When calling createUser(), the mocked API client should resolve with a user object that includes the provided data plus an auto-generated ID @test

  3. Test async rejected values: When calling deleteUser() with a non-existent user ID, the mocked API client should reject with an error message 'User not found' @test

Implementation

The test file should:

  • Create appropriate mocks for the API client
  • Configure the mocks to return the specified values
  • Call the service methods
  • Assert that the results match expectations

@generates

API

// Test file structure
describe('UserService', () => {
  // Setup and mock configuration

  test('getUserById returns user data', () => {
    // Test implementation
  });

  test('createUser resolves with created user', async () => {
    // Test implementation
  });

  test('deleteUser rejects when user not found', async () => {
    // Test implementation
  });
});

Dependencies { .dependencies }

jest { .dependency }

Provides testing framework and mocking capabilities.

Install with Tessl CLI

npx tessl i tessl/npm-jest-each

tile.json