Complete the code to define a unit test that mocks the database call.
test('should return user data', () => { const mockDb = jest.fn().mockReturnValue({ id: 1, name: 'Alice' }); const result = getUser([1]); expect(result.name).toBe('Alice'); });
In unit tests, you pass the mock function itself, not its call result, to replace the real database call.
Complete the code to write an integration test that sends a request to the Express app.
const request = require('supertest'); const app = require('./app'); test('GET /users returns 200', async () => { const response = await request(app).[1]('/users'); expect(response.statusCode).toBe(200); });
To test a GET request, use the get method from supertest.
Fix the error in the unit test by completing the mock setup correctly.
jest.mock('./db', () => ({ fetchUser: jest.fn().[1]({ id: 2, name: 'Bob' }) }));
Since fetchUser returns a promise, use mockResolvedValue to mock an async resolved value.
Fill both blanks to create a test that checks the response body and status code in an integration test.
test('POST /login returns token', async () => { const response = await request(app).post('/login').send({ username: 'user', password: 'pass' }); expect(response.statusCode).toBe([1]); expect(response.body).toHaveProperty([2]); });
The expected status code for a successful login is 200, and the response body should have a 'token' property.
Fill all three blanks to write a unit test that mocks a service call and verifies the result.
const service = require('./service'); jest.mock('./service'); test('should return processed data', async () => { service.processData.mock[1]({ success: true }); const result = await processHandler([2]); expect(result).toEqual([3]); });
Use mockResolvedValue to mock async service calls. Pass the input data string, and expect the mocked success object.