Challenge - 5 Problems
Integration Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this integration test snippet?
Consider this Node.js integration test using Jest and Supertest. What will be the test result output?
Node.js
import request from 'supertest'; import app from './app'; test('GET /users returns 2 users', async () => { const response = await request(app).get('/users'); expect(response.status).toBe(200); expect(response.body.length).toBe(2); });
Attempts:
2 left
💡 Hint
Check what the test expects and what the endpoint returns.
✗ Incorrect
The test expects a 200 status and exactly 2 users in the response body. If the app's /users endpoint returns that, the test passes.
❓ lifecycle
intermediate1:30remaining
Which lifecycle hook is best for setting up a database connection before integration tests?
In a Node.js integration test suite using Jest, which lifecycle method should you use to open a database connection before all tests run?
Attempts:
2 left
💡 Hint
Think about a setup that runs once before all tests.
✗ Incorrect
beforeAll() runs once before all tests, perfect for opening a database connection once.
🔧 Debug
advanced2:30remaining
Why does this integration test fail with a timeout?
Look at this test code snippet. Why does it fail with a timeout error?
Node.js
test('POST /login returns 200', async () => { const response = await request(app).post('/login').send({ username: 'user', password: 'pass' }); expect(response.status).toBe(200); });
Attempts:
2 left
💡 Hint
Consider what happens if the server is not properly closed between tests.
✗ Incorrect
If the server from previous tests is not closed, it can cause port conflicts or hanging requests, leading to timeouts.
📝 Syntax
advanced2:30remaining
Which option correctly mocks a database call in an integration test?
You want to mock the database call getUserById in your integration test. Which code snippet correctly mocks it using Jest?
Node.js
import * as db from './db'; jest.mock('./db'); // Your test here
Attempts:
2 left
💡 Hint
Check how to mock a function's resolved value with Jest.
✗ Incorrect
After jest.mock('./db'), you can set the mock implementation with mockResolvedValue on the mocked function.
❓ state_output
expert3:00remaining
What is the final value of 'count' after running this integration test sequence?
Given this test suite that modifies a shared state, what is the final value of the variable 'count' after all tests run?
Node.js
let count = 0; describe('Counter tests', () => { beforeAll(() => { count = 5; }); test('Increment count', () => { count += 3; expect(count).toBe(8); }); test('Decrement count', () => { count -= 2; expect(count).toBe(6); }); afterAll(() => { count += 1; }); });
Attempts:
2 left
💡 Hint
Follow the changes to 'count' step by step through the lifecycle hooks and tests.
✗ Incorrect
Initial count is set to 5 before all tests. First test adds 3 → 8. Second test subtracts 2 → 6. After all tests, add 1 → 7.