test: added supertest
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
import { formatResponse } from './call';
|
||||
|
||||
describe('call', () => {
|
||||
it('returns the formatted response string', async () => {
|
||||
const response = formatResponse('test');
|
||||
|
||||
expect(response).toBe('This is the string from GET: test');
|
||||
});
|
||||
});
|
||||
@@ -1,8 +0,0 @@
|
||||
/**
|
||||
* Format a response message containing a user input.
|
||||
* @param string - The user input string.
|
||||
* @returns The formatted message.
|
||||
*/
|
||||
export function formatResponse(str: string) {
|
||||
return `This is the string from GET: ${str}`;
|
||||
}
|
||||
15
src/index.ts
15
src/index.ts
@@ -1,16 +1,5 @@
|
||||
import express, { Request, Response } from 'express';
|
||||
import { formatResponse } from './call/call';
|
||||
import server from './server/server';
|
||||
|
||||
const app = express();
|
||||
|
||||
app.get('/', (req: Request, res: Response) => {
|
||||
if (!req.query.string) {
|
||||
return res.status(400).send('Missing query parameter: str');
|
||||
}
|
||||
|
||||
return res.send(formatResponse(req.query.string as string));
|
||||
});
|
||||
|
||||
app.listen(3000, () => {
|
||||
server.listen(3000, () => {
|
||||
console.log('Server running on port 3000');
|
||||
});
|
||||
|
||||
26
src/server/server.test.ts
Normal file
26
src/server/server.test.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import requests from 'supertest';
|
||||
import server from './server';
|
||||
|
||||
beforeAll(() => {
|
||||
jest.mock('../utils/addition', () => ({
|
||||
addition: jest.fn((value: number) => value + 1)
|
||||
}));
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('server', () => {
|
||||
it('returns input value increased by one', async () => {
|
||||
const value = 1;
|
||||
const res = await requests(server)
|
||||
.post('/')
|
||||
.send({ value: value })
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Accept', 'application/json');
|
||||
|
||||
expect(res.body.response).toBeDefined();
|
||||
expect(res.body.response).toBe(value + 1);
|
||||
});
|
||||
});
|
||||
32
src/server/server.ts
Normal file
32
src/server/server.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import * as bodyParser from 'body-parser';
|
||||
import cors from 'cors';
|
||||
import express, { Request, Response } from 'express';
|
||||
import { Validator } from 'jsonschema';
|
||||
import { addition } from '../utils/addition';
|
||||
|
||||
const server = express();
|
||||
server.use(cors());
|
||||
server.use(express.json());
|
||||
server.use(bodyParser.json());
|
||||
|
||||
const validator = new Validator();
|
||||
const schema = {
|
||||
id: '/PostRequest',
|
||||
type: 'object',
|
||||
properties: {
|
||||
value: { type: 'number' }
|
||||
},
|
||||
required: ['value']
|
||||
};
|
||||
|
||||
server.post('/', (req: Request, res: Response) => {
|
||||
if (!validator.validate(req.body, schema).valid) {
|
||||
return res.status(400).json({ message: 'Malformed query parameters' });
|
||||
}
|
||||
|
||||
return res.json({
|
||||
response: addition(parseInt(req.body.value))
|
||||
});
|
||||
});
|
||||
|
||||
export default server;
|
||||
9
src/utils/addition.test.ts
Normal file
9
src/utils/addition.test.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { addition } from './addition';
|
||||
|
||||
describe('call', () => {
|
||||
it('returns the input value increased by one', async () => {
|
||||
const response = addition(3);
|
||||
|
||||
expect(response).toBe(4);
|
||||
});
|
||||
});
|
||||
8
src/utils/addition.ts
Normal file
8
src/utils/addition.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Format a response message containing a user input.
|
||||
* @param number - The user input value.
|
||||
* @returns The value increased by one.
|
||||
*/
|
||||
export function addition(value: number) {
|
||||
return value + 1;
|
||||
}
|
||||
Reference in New Issue
Block a user