-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.test.ts
More file actions
110 lines (96 loc) · 3.23 KB
/
index.test.ts
File metadata and controls
110 lines (96 loc) · 3.23 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { start } from '../src/index.js'
import * as utils from '../src/utils.js'
vi.mock('../src/utils.js', () => ({
getDevtoolsApp: vi.fn()
}))
vi.mock('ws')
describe('backend index', () => {
beforeEach(() => {
vi.clearAllMocks()
})
afterEach(() => {
// Clean up any running servers
})
describe('start', () => {
it('should handle start errors', async () => {
vi.mocked(utils.getDevtoolsApp).mockRejectedValue(
new Error('Package not found')
)
await expect(start()).rejects.toThrow('Package not found')
})
})
describe('API endpoints', () => {
it('should handle test run and stop requests with validation', async () => {
vi.mocked(utils.getDevtoolsApp).mockResolvedValue('/mock/app/path')
const server = await start({ port: 0 })
const { testRunner } = await import('../src/runner.js')
const runSpy = vi.spyOn(testRunner, 'run').mockResolvedValue()
const stopSpy = vi.spyOn(testRunner, 'stop')
// Test invalid payload - missing uid
const invalidResponse = await server?.inject({
method: 'POST',
url: '/api/tests/run',
payload: { entryType: 'test' }
})
expect(invalidResponse?.statusCode).toBe(400)
expect(JSON.parse(invalidResponse?.body || '{}')).toEqual({
error: 'Invalid run payload'
})
expect(runSpy).not.toHaveBeenCalled()
// Test valid run request with all parameters
const runPayload = {
uid: 'test-123',
entryType: 'test',
specFile: '/test.spec.ts'
}
const runResponse = await server?.inject({
method: 'POST',
url: '/api/tests/run',
payload: runPayload
})
expect(runResponse?.statusCode).toBe(200)
expect(JSON.parse(runResponse?.body || '{}')).toEqual({ ok: true })
expect(runSpy).toHaveBeenCalledWith(
expect.objectContaining({
uid: 'test-123',
entryType: 'test',
specFile: '/test.spec.ts',
devtoolsHost: expect.any(String),
devtoolsPort: expect.any(Number)
})
)
// Test stop request
const stopResponse = await server?.inject({
method: 'POST',
url: '/api/tests/stop'
})
expect(stopResponse?.statusCode).toBe(200)
expect(JSON.parse(stopResponse?.body || '{}')).toEqual({ ok: true })
expect(stopSpy).toHaveBeenCalled()
await server?.close()
})
it('should handle test run errors gracefully', async () => {
vi.mocked(utils.getDevtoolsApp).mockResolvedValue('/mock/app/path')
const server = await start({ port: 0 })
const { testRunner } = await import('../src/runner.js')
vi.spyOn(testRunner, 'run').mockRejectedValue(
new Error('Test execution failed')
)
const response = await server?.inject({
method: 'POST',
url: '/api/tests/run',
payload: {
uid: 'test-456',
entryType: 'test',
specFile: '/test.spec.ts'
}
})
expect(response?.statusCode).toBe(500)
expect(JSON.parse(response?.body || '{}')).toEqual({
error: 'Test execution failed'
})
await server?.close()
})
})
})