Parameterised tests for Jest that enable running the same test multiple times with different data sets using arrays or tagged template literals
85
Create a test suite for a user service that validates data handling with external API dependencies.
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.
Create a test file that tests a UserService class with the following behavior:
The service will have these methods that you need to test:
getUserById(id) - returns user data for a given IDcreateUser(userData) - creates a new user and returns the created user objectdeleteUser(id) - deletes a user and throws an error if the user doesn't existYour test suite should include the following test cases:
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
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
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
The test file should:
@generates
// 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
});
});Provides testing framework and mocking capabilities.
Install with Tessl CLI
npx tessl i tessl/npm-jest-eachdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10