I Used to Skip Testing Entirely (Oops)
Honest confession: for the first year I wrote any kind of code, I tested things the old-fashioned way — I ran the program, poked around, and hoped nothing broke. Automated tests felt like something "real developers" did, with fancy setups and weird syntax I didn't understand.
Then I started using AI coding tools, and everything changed. I asked Claude to write a test for a function I'd built, and it not only wrote the test — it explained every line, told me how to run it, and showed me what a failing test looks like versus a passing one. In about 15 minutes I went from zero testing knowledge to having actual working tests in my project.
If you've been skipping tests because they feel intimidating, this article is for you. We're going to use AI to write your very first automated test, step by step, with zero assumed knowledge.
What Even Is an Automated Test?
Before we jump in, let me give you a one-paragraph explanation that actually makes sense. An automated test is just a small piece of code that checks whether another piece of your code does what you expect it to do. That's it.
Instead of you manually running your app and clicking around every time you change something, the test does that checking for you automatically. You write it once, and you can run it a thousand times.
For example, if you have a function that adds two numbers together, a test would say: "Hey, does add(2, 3) actually return 5?" If it does, the test passes. If something broke and it returns undefined instead, the test fails and you know immediately.
Simple idea. AI just makes writing them incredibly fast.
The Function We're Going to Test
Let's use a real example. Here's a simple JavaScript function that formats a user's full name from a first and last name. It's the kind of thing you might actually build:
// A simple function to format a full name
function formatName(firstName, lastName) {
if (!firstName || !lastName) {
return null;
}
return `${firstName.trim()} ${lastName.trim()}`;
}
module.exports = { formatName };It handles missing inputs by returning null, and it trims extra whitespace. Seems fine — but "seems fine" is exactly what tests are for.
The AI Prompt That Does the Heavy Lifting
Here's the exact prompt I use when I want AI to write tests for a function. The key is giving it the full context — your code, your environment, and your experience level. Don't be shy about saying you're a beginner. AI gives much better answers when it knows who it's talking to.
I'm a beginner and I've never written automated tests before.
I have this JavaScript function in a file called formatName.js:
[paste your function here]
Please write automated tests for this function using Jest.
Include tests for:
- Normal expected input
- Edge cases like empty strings or missing values
- Any other cases you think are worth checking
Also explain what each test is doing in plain English,
and tell me exactly how to install Jest and run the tests.That last line — asking for install instructions — is something beginners always forget to ask for. AI will happily write perfect tests and leave you staring at them wondering how to actually run them. Ask upfront.
Why Jest?
Jest is the most popular JavaScript testing tool, and it's what most AI tools will default to for JS projects. If you're using Python, ask for pytest instead. Ruby? RSpec. Just swap the name in the prompt and the same approach works.
What AI Will Write for You
When I ran this prompt, here's the kind of test file Claude generated. Read the comments — that's AI explaining its own output in plain English, which is exactly what you asked for:
const { formatName } = require('./formatName');
// Group related tests together under one label
describe('formatName', () => {
// Test 1: Does it work with normal input?
test('combines first and last name with a space', () => {
expect(formatName('Dan', 'Byers')).toBe('Dan Byers');
});
// Test 2: Does it trim extra whitespace?
test('trims whitespace from names', () => {
expect(formatName(' Dan ', ' Byers ')).toBe('Dan Byers');
});
// Test 3: What if first name is missing?
test('returns null if firstName is missing', () => {
expect(formatName('', 'Byers')).toBeNull();
});
// Test 4: What if last name is missing?
test('returns null if lastName is missing', () => {
expect(formatName('Dan', '')).toBeNull();
});
// Test 5: What if both are missing?
test('returns null if both arguments are missing', () => {
expect(formatName()).toBeNull();
});
});Look at that. Five tests, covering the happy path and all the edge cases, with plain-English descriptions for each one. I didn't write a single line of that test logic — I just reviewed it and made sure I understood what it was doing.
Installing Jest and Running Your Tests
AI should give you these steps automatically if you included them in your prompt, but here's what the setup looks like in a typical Node.js project:
# Step 1: Initialize a package.json if you don't have one
npm init -y
# Step 2: Install Jest
npm install --save-dev jest
# Step 3: Run your tests
npx jest
→ PASS formatName.test.js
→ ✓ combines first and last name with a space (3ms)
→ ✓ trims whitespace from names (1ms)
→ ✓ returns null if firstName is missing (1ms)
→ ✓ returns null if lastName is missing (1ms)
→ ✓ returns null if both arguments are missing (1ms)
→ Tests: 5 passed, 5 totalThat green output is one of the most satisfying things you'll see as a beginner. All five tests passing on the first run felt genuinely exciting to me. It's like a little proof that your code actually works.
Intentionally Break a Test
Want to really understand how tests work? Go into your formatName.js file and change the return statement to something wrong — like returning just firstName with no last name. Run the tests again and watch them fail. Seeing a failure makes passing tests feel much more meaningful.
How to Use AI to Test Your Own Code
Now that you've seen the pattern, here's how to apply it to anything you've already built. The process is the same every time:
1. Paste your function into the AI chat. The whole thing — don't summarize it. AI needs to see the actual code to write meaningful tests.
2. Tell AI what you're worried about. If you're not sure a specific edge case is handled correctly, say so. Something like "I'm not sure what happens if someone passes a number instead of a string" will prompt AI to write a test specifically for that.
3. Ask AI to explain any test you don't understand. If a test uses syntax you haven't seen before, just ask: "What does .toBeNull() mean?" You'll learn testing concepts naturally as you go.
4. Review before you copy. Don't blindly paste tests in. Read through them first. If a test description doesn't match what you expect your code to do, that's a red flag worth investigating.
The Bigger Picture: Why This Matters
Here's the thing I didn't expect when I started writing tests with AI help: it made me a better code writer, not just a better tester. When AI writes test cases, it often covers scenarios I hadn't thought about. Seeing those edge cases made me realize there were gaps in my logic I never would have caught manually.
Once you start testing, you also get braver about making changes. Before I had tests, I was nervous to refactor anything because I might break something silently. Now I change code, run my tests, and know immediately if something went wrong. That feedback loop is incredibly freeing.
Start small. Pick one function you've already written — something simple — and ask AI to write tests for it today. Once you see that green "all tests passing" output, you'll be hooked.
More tutorials in this category, or explore the full field guide.