0
0
Node.jsframework~20 mins

Integration testing patterns in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Integration Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
});
ATest passes because the /users endpoint returns exactly 2 users with status 200.
BTest fails because response.status is not 200.
CTest fails because response.body.length is not 2.
DTest throws an error because app is not imported correctly.
Attempts:
2 left
💡 Hint
Check what the test expects and what the endpoint returns.
lifecycle
intermediate
1: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?
AbeforeEach()
BafterEach()
CafterAll()
DbeforeAll()
Attempts:
2 left
💡 Hint
Think about a setup that runs once before all tests.
🔧 Debug
advanced
2: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);
});
AThe app server is not closed after previous tests, causing port conflicts and hanging requests.
BThe test is missing a done callback to signal completion.
CThe request is missing headers, so the server never responds.
DThe test uses async/await incorrectly, causing the promise to never resolve.
Attempts:
2 left
💡 Hint
Consider what happens if the server is not properly closed between tests.
📝 Syntax
advanced
2: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
Adb.getUserById = jest.fn(() => ({ id: 1, name: 'Alice' }));
Bjest.mockResolvedValue(db.getUserById, { id: 1, name: 'Alice' });
Cdb.getUserById.mockResolvedValue({ id: 1, name: 'Alice' });
Djest.fn(db.getUserById).mockReturnValue({ id: 1, name: 'Alice' });
Attempts:
2 left
💡 Hint
Check how to mock a function's resolved value with Jest.
state_output
expert
3: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;
  });
});
A9
B7
C6
D8
Attempts:
2 left
💡 Hint
Follow the changes to 'count' step by step through the lifecycle hooks and tests.