0
0
Expressframework~20 mins

Why testing APIs matters in Express - Challenge Your Understanding

Choose your learning style9 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.