AI generated prompt for Jest Unit Test Generator
**Context**:
You are tasked with developing an artificial intelligence-powered Jest unit test generator. The goal is to create a system that can automatically generate unit tests for JavaScript functions using the Jest testing framework. The input will be a JavaScript function, and the output will be a set of Jest unit tests that thoroughly cover the function's behavior.
**Detailed Instructions**:
1. Analyze the input JavaScript function to identify its parameters, return types, and any potential edge cases.
2. Generate a set of unit tests that cover various scenarios, including:
- Happy path: Test the function with valid inputs and expected outputs.
- Error handling: Test the function with invalid inputs, edge cases, and error scenarios.
- Boundary values: Test the function with boundary values, such as minimum, maximum, and zero.
3. Utilize Jest's API to write the generated tests, including `describe`, `it`, `expect`, and `toThrow`.
4. Ensure the generated tests are readable, maintainable, and follow best practices for unit testing.
5. Handle functions with async/await syntax and generate tests accordingly.
**Output Format**:
The output should be a string containing the generated Jest unit tests in JavaScript format. The tests should be wrapped in a `describe` block, and each test case should be defined using the `it` function. The output should include the necessary `import` statements and any required setup or teardown code.
**Examples**:
Input:
```javascript
function add(a, b) {
return a + b;
}
```
Output:
```javascript
import { add } from './add';
describe('add', () => {
it('should return the sum of two numbers', () => {
expect(add(2, 3)).toBe(5);
});
it('should handle negative numbers', () => {
expect(add(-2, 3)).toBe(1);
});
it('should handle zero', () => {
expect(add(0, 0)).toBe(0);
});
it('should throw an error for non-numeric inputs', () => {
expect(() => add('a', 2)).toThrow();
});
});
```
Input:
```javascript
async function fetchData(url) {
const response = await fetch(url);
return response.json();
}
```
Output:
```javascript
import { fetchData } from './fetchData';
describe('fetchData', () => {
it('should return the fetched data', async () => {
const url = 'https://example.com/api/data';
const data = await fetchData(url);
expect(data).toBeInstanceOf(Object);
});
it('should handle network errors', async () => {
const url = 'https://example.com/api/error';
await expect(fetchData(url)).rejects.toThrow();
});
it('should handle invalid URLs', async () => {
const url = 'invalid-url';
await expect(fetchData(url)).rejects.toThrow();
});
});
```This coding prompt is designed to help you get better results from AI assistants like ChatGPT, Claude, and Gemini. Here's how to make the most of it:
💡 Pro tip: Save this prompt to your collection to use it again later. Well-crafted prompts can save hours of back-and-forth with AI.
Adjust the prompt to match your specific industry, audience, or use case. Adding relevant context improves output quality.
Specify your desired output length (e.g., "in 200 words" or "in 3 bullet points") to get more targeted responses.
Add tone instructions like "professional," "casual," or "technical" to match your brand voice.
Include an example of the output format you want to help the AI understand your expectations.
This prompt has been tested and optimized for all major AI models. For best results with coding-related prompts, consider using an AI-powered IDE like Cursor or Windsurf.
Learn more about using prompts effectively with our comprehensive guides:
0 people found this prompt helpful
Based on 0 reviews
Be the first to share your experience with this prompt!
This prompt was reviewed and verified to work with current AI models.
Tested with ChatGPT, Claude & Gemini. Reviewed by community users.
AI prompts work best when you customize them for your specific situation. Follow these steps to get the most out of Jest Unit Test Generator.