-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathindex.test.ts
More file actions
74 lines (60 loc) · 2.57 KB
/
index.test.ts
File metadata and controls
74 lines (60 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { describe, expect, it } from 'vitest';
import app from '../src/index';
import { SELF, createExecutionContext, env, waitOnExecutionContext } from 'cloudflare:test';
describe('Hono app on Cloudflare Workers', () => {
describe('Unit Tests', () => {
it('should return welcome message', async () => {
const res = await app.request('/', {}, env);
expect(res.status).toBe(200);
const data = await res.json();
expect(data).toEqual({ message: 'Welcome to Hono API' });
});
it('should greet a user with their name', async () => {
const res = await app.request('/hello/tester', {}, env);
expect(res.status).toBe(200);
const data = await res.json();
expect(data).toEqual({ message: 'Hello, tester!' });
});
it('should handle errors with custom error handler', async () => {
const res = await app.request('/error', {}, env);
expect(res.status).toBe(500);
const data = await res.json();
expect(data).toHaveProperty('error', 'This is a test error');
});
it('should handle 404 with custom not found handler', async () => {
const res = await app.request('/non-existent-route', {}, env);
expect(res.status).toBe(404);
const data = await res.json();
expect(data).toEqual({ message: 'Not Found' });
});
});
// Integration test style with worker.fetch
describe('Integration Tests', () => {
it('should fetch the root endpoint', async () => {
// Create request and context
const request = new Request('http://localhost/');
const ctx = createExecutionContext();
const response = await app.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
expect(response.status).toBe(200);
const data = await response.json();
expect(data).toEqual({ message: 'Welcome to Hono API' });
});
it('should handle a parameter route', async () => {
// Create request and context
const request = new Request('http://localhost/hello/cloudflare');
const ctx = createExecutionContext();
const response = await app.fetch(request, env, ctx);
await waitOnExecutionContext(ctx);
expect(response.status).toBe(200);
const data = await response.json();
expect(data).toEqual({ message: 'Hello, cloudflare!' });
});
it('should handle errors gracefully', async () => {
const response = await SELF.fetch('http://localhost/error');
expect(response.status).toBe(500);
const data = await response.json();
expect(data).toHaveProperty('error', 'This is a test error');
});
});
});