Skip to content

Commit ff1dee3

Browse files
test: add validation unit tests
1 parent e46a6d0 commit ff1dee3

File tree

5 files changed

+2024
-11
lines changed

5 files changed

+2024
-11
lines changed

jest.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} **/
2+
module.exports = {
3+
testEnvironment: 'node',
4+
transform: {
5+
'^.+.tsx?$': ['ts-jest', {}],
6+
},
7+
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"format": "prettier --write \"**/*.{js,json,ts,tsx}\"",
88
"start": "yarn node dist/index.js",
99
"test:types": "yarn tsc --noEmit",
10-
"test": "yarn test:types"
10+
"test": "yarn test:types && yarn jest"
1111
},
1212
"bin": "dist/start.js",
1313
"files": [
@@ -21,11 +21,14 @@
2121
"zod": "^3.24.1"
2222
},
2323
"devDependencies": {
24+
"@jest/globals": "^29.7.0",
2425
"@types/cors": "^2.8.17",
2526
"@types/express": "^5.0.0",
2627
"@types/node": "^22.10.9",
2728
"eslint": "^9.18.0",
29+
"jest": "^29.7.0",
2830
"prettier": "^3.4.2",
31+
"ts-jest": "^29.2.5",
2932
"typescript": "^5.7.3"
3033
}
3134
}

src/handler.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { describe, expect, test } from '@jest/globals'
2+
3+
import { parseRequestBody } from './handler'
4+
5+
describe('parseRequestBody', () => {
6+
test('correctly extracts expected fields', () => {
7+
expect(
8+
parseRequestBody({
9+
url: 'url',
10+
answer: 'answer',
11+
response: 'response',
12+
params: { param: 'param' },
13+
}),
14+
).toStrictEqual({
15+
url: 'url',
16+
evaluationFunctionRequestData: {
17+
answer: 'answer',
18+
response: 'response',
19+
params: { param: 'param' },
20+
},
21+
})
22+
})
23+
24+
test('rejects requests with missing fields', () => {
25+
expect(
26+
parseRequestBody({
27+
answer: 'answer',
28+
response: 'response',
29+
params: { param: 'param' },
30+
}),
31+
).toBeNull()
32+
expect(
33+
parseRequestBody({
34+
url: 'url',
35+
response: 'response',
36+
params: { param: 'param' },
37+
}),
38+
).toBeNull()
39+
expect(
40+
parseRequestBody({
41+
url: 'url',
42+
answer: 'answer',
43+
params: { param: 'param' },
44+
}),
45+
).toBeNull()
46+
expect(
47+
parseRequestBody({
48+
url: 'url',
49+
answer: 'answer',
50+
response: 'response',
51+
}),
52+
).toBeNull()
53+
})
54+
})

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
"strict": true,
2121
"target": "es2016"
2222
},
23-
"exclude": ["dist", "node_modules"]
23+
"exclude": ["dist", "node_modules"],
24+
"include": ["src"]
2425
}

0 commit comments

Comments
 (0)