Skip to content

Instantly share code, notes, and snippets.

@up1
Last active February 27, 2025 10:59
Show Gist options
  • Save up1/1dbec9123aa00d96339733e6f6d62cbf to your computer and use it in GitHub Desktop.
Save up1/1dbec9123aa00d96339733e6f6d62cbf to your computer and use it in GitHub Desktop.
Claude code :: Demo
> Add test case with jest and supertest library for app.js in folder __tests__
⏺ I'll create a test case for app.js using Jest and Supertest. First, let's check if these
libraries are installed.
Bash(npm list jest supertest || echo "Libraries not installed")…
╭────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Bash command │
│ │
│ npm list jest supertest || echo "Libraries not installed" │
│ Checks if Jest and Supertest are installed, shows error if not │
│ │
│ Do you want to proceed? │
│ ❯ Yes │
│ Yes, and don't ask again for npm list commands in │
│ /Users/somkiatpuisungnoen/data/slide/AI/demo-20250226/demo-claude │
│ No, and tell Claude what to do differently (esc) │
│ │
╰────────────────────────────────────────────────────────────────────────────────────────────────────╯
⏺ Create(file_path: src/__tests__/app.test.js)…
⎿ Updated src/__tests__/app.test.js with 23 additions
1 const request = require('supertest');
2 const app = require('../app');
3
4 describe('Products API', () => {
5 describe('GET /products/:id', () => {
6 test('should return a product when valid ID is provided', async () => {
7 const response = await request(app).get('/products/1');
8
9 expect(response.status).toBe(200);
10 expect(response.body).toHaveProperty('id', 1);
11 expect(response.body).toHaveProperty('name');
12 expect(response.body).toHaveProperty('price');
13 });
14
15 test('should return 404 with correct error message when product not found', async (
) => {
16 const nonExistentId = 999;
17 const response = await request(app).get(`/products/${nonExistentId}`);
18
19 expect(response.status).toBe(404);
20 expect(response.body).toHaveProperty('message', `Product id = ${nonExistentId} no
t found in system`);
21 });
22 });
23 });
24 \ No newline at end of file
⏺ Now let's update the package.json to add a test script:
⏺ Read(file_path: "package.json")…
  ⎿  {
"name": "demo-claude",
"version": "1.0.0",
... (+18 lines)
⏺ Update(file_path: package.json)…
╭────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Edit file │
│ ╭────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│ │ package.json │ │
│ │ │ │
│ │ 4 "main": "src/index.js", │ │
│ │ 5 "scripts": { │ │
│ │ 6 "start": "node src/index.js", │ │
│ │ 7 "test": "echo \"Error: no test specified\" && exit 1" │ │
│ │ 7 "test": "jest" │ │
│ │ 8 }, │ │
│ │ 9 "keywords": [], │ │
│ │ 10 "author": "", │ │
│ ╰────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│ Do you want to make this edit to package.json? │
│ ❯ Yes │
│ Yes, and don't ask again this session │
│ No, and tell Claude what to do differently (esc) │
│ │
╰────────────────────────────────────────────────────────────────────────────────────────────────────╯
⏺ Let's run the tests to make sure they work:
⏺ Bash(npm test)…
  ⎿  > [email protected] test
> jest
  ⎿  PASS src/__tests__/app.test.js
Products API
GET /products/:id
✓ should return a product when valid ID is provided (11 ms)
✓ should return 404 with correct error message when product not found (1 ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 0.252 s
Ran all test suites.
⏺ Great! I've created a test suite for your app.js file with two test cases:
1. Testing successful product retrieval (status 200) when requesting an existing product ID
2. Testing the 404 response with the correct error message when requesting a non-existent
product ID
The tests run successfully, verifying that your API behaves as expected.
> run test with coverage
⏺ I'll run the tests with coverage reporting:
⏺ Bash(npm test -- --coverage)…
⏺ Bash(npm test -- --coverage)…
  ⎿  > [email protected] test
> jest --coverage
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
app.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
  ⎿  PASS src/__tests__/app.test.js
Products API
GET /products/:id
✓ should return a product when valid ID is provided (11 ms)
✓ should return 404 with correct error message when product not found (4 ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 0.278 s, estimated 1 s
Ran all test suites.
⏺ Perfect! Your tests have achieved 100% code coverage for app.js:
- 100% statement coverage
- 100% branch coverage
- 100% function coverage
- 100% line coverage
This means all code paths in your application are being tested, including both the success case
and the error case for retrieving products.
Improve code to add case 404 for product not found
GET /products/2
Success case with response code = 404
{
"message": "Product id = 2 not found in system"
}
# บอกให้ใช้ Conventional Commits
aa2217f (HEAD -> main) refactor: split code into app.js and index.js with test coverage
07b13f5 fix(api): improve product not found error message to include ID
# ไม่มีบอกให้ใช้ Conventional Commits
da769b9 Create Express REST API with products endpoint
$npm install -g @anthropic-ai/claude-code
$claude
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment