The next-gen flux-based test runner for Jest that provides test framework globals and event-driven test execution
82
Build a test suite that uses automatic retry functionality to handle tests for a flaky API client.
You are testing an API client that occasionally fails due to network issues. Rather than having your test suite fail on every transient error, you need to configure tests to automatically retry when they fail.
Implement a test suite in test/api-client.test.js that:
First, create a file src/api-client.js that exports an ApiClient class with the following behavior:
fetchUser(userId) - Returns user data { id: userId, name: "User" }, but fails ~50% of the time with "Network timeout"fetchPosts(userId) - Returns posts array [{ id: 1, title: "Post" }], but fails ~50% of the time with "Service unavailable"createPost(title) - Returns new post { id: 2, title }, but fails ~70% of the time with "Rate limited"The simulated failures should be random to mimic real flaky behavior.
Write tests in test/api-client.test.js that cover the following scenarios:
Configure retry counts appropriately: use 3 retries for most tests, but 5 retries for the highly flaky createPost test.
@generates
/**
* API client with methods that may fail intermittently
*/
class ApiClient {
/**
* Fetches user data by ID
* @param {string} userId - The user ID
* @returns {Promise<object>} User data object
*/
fetchUser(userId) {}
/**
* Fetches posts for a user
* @param {string} userId - The user ID
* @returns {Promise<array>} Array of post objects
*/
fetchPosts(userId) {}
/**
* Creates a new post
* @param {string} title - The post title
* @returns {Promise<object>} Created post object
*/
createPost(title) {}
}
module.exports = { ApiClient };Provides the testing framework and retry functionality.
Install with Tessl CLI
npx tessl i tessl/npm-jest-circusevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10