Skip to content

Commit 148e4b1

Browse files
committed
create unit test
1 parent 02632ef commit 148e4b1

File tree

5 files changed

+654
-2
lines changed

5 files changed

+654
-2
lines changed

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
],
2828
"scripts": {
2929
"build": "rm -rf dist && bun build src/index.ts --outdir ./dist --target bun --minify --external elysia && tsc",
30-
"format": "prettier --write '**/*.ts' --ignore-path .gitignore"
30+
"format": "prettier --write '**/*.ts' --ignore-path .gitignore",
31+
"test": "bun test",
32+
"test:unit": "bun test tests/unit",
33+
"test:e2e": "bun test tests/e2e",
34+
"test:watch": "bun test --watch",
35+
"test:coverage": "bun test --coverage"
3136
},
3237
"devDependencies": {
3338
"@types/bun": "latest",
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { describe, expect, it } from 'bun:test';
2+
import { Elysia } from 'elysia';
3+
import { httpExceptionPlugin } from '../../src/http-exception-plugin';
4+
import {
5+
HttpException,
6+
BadRequestException,
7+
UnauthorizedException,
8+
NotFoundException,
9+
InternalServerErrorException
10+
} from '../../src';
11+
12+
describe('HTTP Exception Plugin', () => {
13+
it('should return an Elysia instance', () => {
14+
const plugin = httpExceptionPlugin();
15+
expect(plugin).toBeInstanceOf(Elysia);
16+
});
17+
18+
it('should be composable with other Elysia apps', () => {
19+
const app = new Elysia();
20+
const result = app.use(httpExceptionPlugin());
21+
expect(result).toBeInstanceOf(Elysia);
22+
});
23+
24+
it('should provide httpException decorator', () => {
25+
const app = new Elysia().use(httpExceptionPlugin());
26+
27+
// Check if the decorator is available in the app's decorator store
28+
expect(app.decorator).toHaveProperty('httpException');
29+
});
30+
31+
it('should handle HttpException in decorator', () => {
32+
const plugin = httpExceptionPlugin();
33+
const httpException = plugin.decorator.httpException;
34+
35+
const error = new BadRequestException('Invalid input');
36+
const response = httpException(error);
37+
38+
expect(response).toBeInstanceOf(Response);
39+
expect(response.status).toBe(400);
40+
});
41+
42+
it('should handle generic errors in decorator', () => {
43+
const plugin = httpExceptionPlugin();
44+
const httpException = plugin.decorator.httpException;
45+
46+
const error = new Error('Some error');
47+
const response = httpException(error);
48+
49+
expect(response).toBeInstanceOf(Response);
50+
expect(response.status).toBe(500);
51+
});
52+
53+
it('should handle non-error values in decorator', () => {
54+
const plugin = httpExceptionPlugin();
55+
const httpException = plugin.decorator.httpException;
56+
57+
const response = httpException('Some error message');
58+
59+
expect(response).toBeInstanceOf(Response);
60+
expect(response.status).toBe(500);
61+
});
62+
63+
it('should create plugin with correct name', () => {
64+
const plugin = httpExceptionPlugin();
65+
expect(plugin.config.name).toBe('elysia-http-exception');
66+
});
67+
});

tests/unit/http-exception.test.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { describe, it, expect } from 'bun:test';
2+
import { HttpException } from '../../src/exceptions/http-exception';
3+
import { HttpError } from '../../src/types/http-error';
4+
5+
describe('HttpException', () => {
6+
it('should create instance with string message', () => {
7+
const exception = new HttpException(HttpError.BAD_REQUEST, 'Bad Request');
8+
9+
expect(exception.statusCode).toBe(400);
10+
expect(exception.message).toBe('Bad Request');
11+
expect(exception.name).toBe('HttpException');
12+
expect(exception instanceof Error).toBe(true);
13+
expect(exception instanceof HttpException).toBe(true);
14+
});
15+
16+
it('should create instance with object message', () => {
17+
const messageObject = {
18+
error: 'VALIDATION_FAILED',
19+
field: 'email',
20+
details: 'Invalid email format'
21+
};
22+
23+
const exception = new HttpException(HttpError.BAD_REQUEST, messageObject);
24+
25+
expect(exception.statusCode).toBe(400);
26+
expect(exception.data).toEqual(messageObject);
27+
});
28+
29+
it('should create instance with Error object', () => {
30+
const originalError = new Error('Original error message');
31+
const exception = new HttpException(HttpError.INTERNAL_SERVER_ERROR, originalError);
32+
33+
expect(exception.statusCode).toBe(500);
34+
expect(exception.message).toBe('Original error message');
35+
});
36+
37+
it('should return correct body for string message', () => {
38+
const exception = new HttpException(HttpError.BAD_REQUEST, 'Bad Request');
39+
const body = exception.toBody();
40+
41+
expect(body).toEqual({
42+
statusCode: 400,
43+
message: 'Bad Request'
44+
});
45+
});
46+
47+
it('should return correct body for object message', () => {
48+
const messageObject = {
49+
error: 'VALIDATION_FAILED',
50+
field: 'email'
51+
};
52+
53+
const exception = new HttpException(HttpError.BAD_REQUEST, messageObject);
54+
const body = exception.toBody();
55+
56+
expect(body).toEqual(messageObject);
57+
});
58+
59+
it('should return correct body for Error object message', () => {
60+
const originalError = new Error('Original error');
61+
const exception = new HttpException(HttpError.INTERNAL_SERVER_ERROR, originalError);
62+
const body = exception.toBody();
63+
64+
expect(body).toEqual({
65+
statusCode: 500,
66+
message: 'Original error'
67+
});
68+
});
69+
70+
it('should handle null message', () => {
71+
const exception = new HttpException(HttpError.BAD_REQUEST, null as any);
72+
const body = exception.toBody();
73+
74+
expect(body).toEqual({
75+
statusCode: 400,
76+
message: 'Bad Request'
77+
});
78+
});
79+
80+
it('should handle undefined message', () => {
81+
const exception = new HttpException(HttpError.BAD_REQUEST, undefined as any);
82+
const body = exception.toBody();
83+
84+
expect(body).toEqual({
85+
statusCode: 400,
86+
message: 'Bad Request'
87+
});
88+
});
89+
90+
it('should preserve stack trace', () => {
91+
const exception = new HttpException(HttpError.INTERNAL_SERVER_ERROR, 'Internal Error');
92+
93+
expect(exception.stack).toBeDefined();
94+
expect(typeof exception.stack).toBe('string');
95+
expect(exception.stack).toContain('HttpException');
96+
});
97+
98+
it('should be serializable to JSON', () => {
99+
const exception = new HttpException(HttpError.BAD_REQUEST, { error: 'TEST_ERROR' });
100+
const serialized = JSON.stringify(exception);
101+
const parsed = JSON.parse(serialized);
102+
103+
expect(parsed.statusCode).toBe(400);
104+
expect(parsed.data).toEqual({ error: 'TEST_ERROR' });
105+
});
106+
});

0 commit comments

Comments
 (0)