test: added supertest
This commit is contained in:
@@ -5,7 +5,7 @@ It contains basic configurations for the following:
|
||||
- TypeScript (typechecking and building)
|
||||
- Eslint (linting)
|
||||
- Prettier (formatting)
|
||||
- Jest (testing)
|
||||
- Jest/Supertest (testing)
|
||||
- Husky (pre-commit hooks to run linting, typechecking, and commit message format)
|
||||
- GitHub Actions (CI/CD)
|
||||
|
||||
|
||||
14040
package-lock.json
generated
Normal file
14040
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
10
package.json
10
package.json
@@ -12,7 +12,7 @@
|
||||
"lint": "eslint --ext .ts . --fix",
|
||||
"typecheck": "tsc",
|
||||
"format": "prettier --config .prettierrc 'src/**/*.ts' --write",
|
||||
"test": "jest --runInBand",
|
||||
"test": "jest --runInBand --detectOpenHandles",
|
||||
"prepare": "husky install"
|
||||
},
|
||||
"lint-staged": {
|
||||
@@ -24,16 +24,21 @@
|
||||
]
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.18.2"
|
||||
"body-parser": "^1.20.2",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.18.2",
|
||||
"jsonschema": "^1.4.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@commitlint/cli": "^17.6.6",
|
||||
"@commitlint/config-conventional": "^17.6.6",
|
||||
"@swc/core": "^1.3.69",
|
||||
"@swc/jest": "^0.2.26",
|
||||
"@types/cors": "^2.8.13",
|
||||
"@types/express": "^4.17.17",
|
||||
"@types/jest": "^29.5.3",
|
||||
"@types/node": "^20.4.2",
|
||||
"@types/supertest": "^2.0.12",
|
||||
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
||||
"@typescript-eslint/parser": "^6.0.0",
|
||||
"eslint": "^8.44.0",
|
||||
@@ -44,6 +49,7 @@
|
||||
"lint-staged": "^13.2.3",
|
||||
"nodemon": "^3.0.1",
|
||||
"prettier": "^3.0.0",
|
||||
"supertest": "^6.3.3",
|
||||
"ts-jest": "^29.1.1",
|
||||
"ts-node": "^10.9.1",
|
||||
"typescript": "^5.1.6"
|
||||
|
||||
@@ -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