Skip to content

Instantly share code, notes, and snippets.

@davidcsejtei
Created June 18, 2021 10:14
Show Gist options
  • Save davidcsejtei/6fbe89dcd7820adc46a3345ce760cc95 to your computer and use it in GitHub Desktop.
Save davidcsejtei/6fbe89dcd7820adc46a3345ce760cc95 to your computer and use it in GitHub Desktop.
GetAllUsers endpoint and test
import { Request, Response } from 'express';
import getAllUsers from './getAllUsers';
describe('Get all users request', () => {
let mockRequest: Partial<Request>;
let mockResponse: Partial<Response>;
let responseObject = {};
beforeEach(() => {
mockRequest = {
};
mockResponse = {
statusCode: 0,
send: jest.fn().mockReturnValue({
json: jest.fn().mockImplementation((JSONdata) => {
responseObject = JSONdata;
})
})
};
});
test('200 - users', async () => {
const expectedStatusCode = 200;
const expectedReponse = {
users: [
{
name: 'John',
age: 30
},
{
name: 'Peter',
age: 40
}
]
};
getAllUsers(mockRequest as Request, mockResponse as Response);
expect(mockResponse.statusCode).toBe(expectedStatusCode);
expect(responseObject).toEqual(expectedReponse);
});
});
import { Request, Response } from "express";
import { findAllUsers } from "../services/userService";
export default function getAllUsers(request: Request, response: Response) {
const users = findAllUsers();
response.statusCode = 200;
response.send().json({ users });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment