The test function is async but does not return or await the request promise, so Jest won't wait for it.
Step 2: Confirm correct fix
Adding return before request(...) or making the test async and awaiting fixes the issue.
Final Answer:
Missing return or await to handle async test -> Option C
Quick Check:
Async test needs return or await [OK]
Hint: Always return or await supertest calls in tests [OK]
Common Mistakes:
Not returning or awaiting async calls in tests
Assuming path or method is wrong without checking
Confusing import errors with test async errors
5. You want to test an Express GET endpoint that returns a list of users, but only those with active status true. Which test code correctly checks this filtering?
app.get('/active-users', (req, res) => {
const users = [
{ id: 1, active: true },
{ id: 2, active: false },
{ id: 3, active: true }
];
res.json(users.filter(u => u.active));
});
// Test code options:
A) await request(app).get('/active-users').expect(200).expect(res => {
if (!res.body.every(u => u.active)) throw new Error('Inactive user found');
});
B) await request(app).get('/active-users').expect(200).expect(res => {
if (res.body.length !== 3) throw new Error('Wrong number of users');
});
C) await request(app).get('/active-users').expect(404);
D) await request(app).get('/active-users').expect(200).expect(res => {
if (!res.body.some(u => u.active === false)) throw new Error('Inactive user found');
});
hard
A. Incorrectly throws error when no inactive users found
B. Incorrectly expects all users, including inactive
C. Wrong status code expectation
D. Checks for inactive users but throws error if found
Solution
Step 1: Understand endpoint filtering
The endpoint returns only users with active: true, so no inactive users should appear.
Step 2: Analyze test options
Checks for inactive users but throws error if found uses every(u => u.active) and throws if any inactive, correctly verifying the filter. Incorrectly expects all users, including inactive expects length !== 3 (wrong, it's 2). Wrong status code expectation expects 404 (wrong). Incorrectly throws error when no inactive users found incorrectly throws error when no inactive users found.
Step 3: Verify A logic
!every(u => u.active) detects any inactive user, matching the requirement.
Final Answer:
Checks for inactive users but throws error if found -> Option D
Quick Check:
Test filters out inactive users = no inactive found [OK]
Hint: Check response has no inactive users with some() or every() [OK]
Common Mistakes:
Expecting all users instead of filtered active only
Using wrong status code in test
Confusing every() and some() logic in tests
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.