Bird
Raised Fist0
Expressframework~20 mins

Why testing APIs matters in Express - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
API Testing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is testing APIs important?

Which of the following is the main reason to test APIs in an Express application?

ATo ensure the API endpoints return the correct data and handle errors properly
BTo change the API endpoints frequently
CTo reduce the size of the API codebase
DTo make the API run faster by optimizing code
Attempts:
2 left
💡 Hint

Think about what testing helps you verify about your API responses and behavior.

❓ component_behavior
intermediate
2:00remaining
What happens if an API endpoint is not tested?

Consider an Express API endpoint that is not tested. What is the most likely outcome when a client sends unexpected data?

AThe API will log the error but still return correct data
BThe API will automatically fix the data and respond correctly
CThe API will reject the request with a clear error every time
DThe API may crash or return incorrect responses without warning
Attempts:
2 left
💡 Hint

Think about what happens when code is not checked for handling bad inputs.

❓ state_output
advanced
3:00remaining
What is the output of this Express API test code?

Given the following Express API test using a testing library, what will be the test result?

Express
const request = require('supertest');
const express = require('express');
const app = express();

app.get('/hello', (req, res) => {
  res.status(200).json({ message: 'Hello World' });
});

describe('GET /hello', () => {
  it('responds with JSON containing message', async () => {
    const response = await request(app).get('/hello');
    expect(response.status).toBe(200);
    expect(response.body.message).toBe('Hello World');
  });
});
AThe test fails because the endpoint returns status 404
BThe test passes because the endpoint returns status 200 and correct message
CThe test fails because the response body does not contain 'message'
DThe test throws a runtime error due to missing middleware
Attempts:
2 left
💡 Hint

Check the endpoint code and what the test expects.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this Express API test snippet

What is the syntax error in the following Express API test code?

Express
const request = require('supertest');
const app = require('./app');

describe('GET /users', () => {
  it('should return users list', async () => {
    const response = await request(app).get('/users');
    expect(response.status).toBe(200);
    expect(response.body).toBeInstanceOf(Array);
  });
});
AMissing parentheses after describe function
BMissing await keyword before request(app).get('/users')
CMissing semicolon after the get('/users') call
DMissing closing bracket for the it function
Attempts:
2 left
💡 Hint

Look carefully at the line with the get('/users') call.

🔧 Debug
expert
3:00remaining
Why does this Express API test fail with a timeout?

Review the following Express API test code. Why does the test fail with a timeout error?

Express
const request = require('supertest');
const express = require('express');
const app = express();

app.get('/data', (req, res) => {
  // Missing res.send or res.json call
});

describe('GET /data', () => {
  it('should respond with data', async () => {
    const response = await request(app).get('/data');
    expect(response.status).toBe(200);
  });
});
AThe endpoint does not send any response, causing the request to hang and timeout
BThe test is missing the done callback to finish the test
CThe app is not listening on a port, so the request fails
DThe test expects status 200 but the endpoint returns 404
Attempts:
2 left
💡 Hint

Check what the endpoint does when it receives a request.

Practice

(1/5)
1. Why is testing APIs important in Express applications?
easy
A. It automatically updates the API documentation.
B. It makes the app run faster on the server.
C. It helps find and fix problems early before users see them.
D. It reduces the size of the API code.

Solution

  1. Step 1: Understand the purpose of API testing

    Testing APIs helps catch bugs and errors early in development.
  2. Step 2: Identify the benefit of early problem detection

    Finding problems early prevents issues from reaching users and causing failures.
  3. Final Answer:

    It helps find and fix problems early before users see them. -> Option C
  4. Quick Check:

    API testing = early bug detection [OK]
Hint: Testing finds bugs early to avoid user issues [OK]
Common Mistakes:
  • Thinking testing speeds up server runtime
  • Confusing testing with documentation generation
  • Believing testing reduces code size
2. Which of the following is the correct way to write a simple test for an Express API endpoint using a testing library like Jest and Supertest?
easy
A. app.get('/api').expect(200);
B. request(app).get('/api').expect(200).end(done);
C. request('/api').get(app).expect(200);
D. expect(request(app).get('/api')).toBe(200);

Solution

  1. Step 1: Recognize the correct testing syntax

    Using Supertest, you call request(app).get('/api') to test the endpoint.
  2. Step 2: Confirm chaining with expect and end

    The correct pattern chains .expect(200) and .end(done) to complete the test.
  3. Final Answer:

    request(app).get('/api').expect(200).end(done); -> Option B
  4. Quick Check:

    Supertest syntax = request(app).get().expect() [OK]
Hint: Supertest uses request(app).get(url).expect(status) [OK]
Common Mistakes:
  • Calling expect directly on app.get instead of request
  • Swapping app and URL in request call
  • Using expect incorrectly without chaining
3. Consider this Express API test snippet using Jest and Supertest:
test('GET /users returns 200 and JSON', async () => {
  const response = await request(app).get('/users');
  expect(response.status).toBe(200);
  expect(response.headers['content-type']).toMatch(/json/);
});

What will happen if the API returns status 404 instead of 200?
medium
A. The test will fail because the status is not 200.
B. The test will pass because content-type is checked.
C. The test will throw a syntax error.
D. The test will ignore the status and pass.

Solution

  1. Step 1: Analyze the status check in the test

    The test expects response.status to be exactly 200.
  2. Step 2: Understand test behavior on mismatch

    If the API returns 404, the expect(response.status).toBe(200) assertion fails and the test fails.
  3. Final Answer:

    The test will fail because the status is not 200. -> Option A
  4. Quick Check:

    Status check fails test if not 200 [OK]
Hint: Expect status 200; else test fails [OK]
Common Mistakes:
  • Assuming test passes if content-type matches
  • Thinking 404 triggers syntax error
  • Believing test ignores status mismatch
4. You wrote this Express API test but it always times out:
test('POST /login returns 200', (done) => {
  request(app)
    .post('/login')
    .send({ username: 'user', password: 'pass' })
    .expect(200);
});

What is the main reason for the timeout?
medium
A. Missing call to done() to signal test completion.
B. Incorrect HTTP method used for /login endpoint.
C. The .send() method is not supported in Supertest.
D. The test expects 200 but server returns 500.

Solution

  1. Step 1: Check test callback usage

    The test uses done callback but never calls done(), so Jest waits forever.
  2. Step 2: Understand how to end async tests

    Calling done() inside .end() or returning a promise is needed to finish the test.
  3. Final Answer:

    Missing call to done() to signal test completion. -> Option A
  4. Quick Check:

    Call done() to end async test [OK]
Hint: Always call done() or return promise in async tests [OK]
Common Mistakes:
  • Forgetting to call done() in callback tests
  • Assuming .send() is unsupported
  • Guessing wrong HTTP method without checking
5. You want to ensure your Express API handles errors correctly. Which testing approach best helps catch unexpected server errors early?
hard
A. Skip testing and rely on console logs for errors.
B. Manually test the API endpoints once after deployment.
C. Only test the API when users report bugs.
D. Write automated tests that simulate invalid inputs and check for 4xx or 5xx responses.

Solution

  1. Step 1: Identify best practice for error handling tests

    Automated tests simulating invalid inputs help verify API returns proper error codes.
  2. Step 2: Understand benefits of early automated error detection

    This approach finds bugs before deployment and prevents user impact.
  3. Final Answer:

    Write automated tests that simulate invalid inputs and check for 4xx or 5xx responses. -> Option D
  4. Quick Check:

    Automated error tests catch bugs early [OK]
Hint: Test invalid inputs automatically to catch errors early [OK]
Common Mistakes:
  • Relying only on manual or post-deployment testing
  • Ignoring error cases in tests
  • Depending on logs instead of tests
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard