Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ jobs:
run_install: false
- name: Install dependencies
run: pnpm install
- name: Lint
run: pnpm lint
- name: 📦 Bundle
run: pnpm -r --workspace-concurrency=1 build
- name: 🧪 Run Tests
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
"demo": "wdio run ./example/wdio.conf.ts",
"dev": "pnpm --parallel dev",
"preview": "pnpm --parallel preview",
"test": "pnpm --parallel test",
"test": "vitest run",
"test:watch": "vitest",
"test:coverage": "vitest run --coverage",
"test:ui": "vitest --ui",
"test:debug": "node --inspect-brk ./node_modules/.bin/vitest run",
"lint": "pnpm --parallel lint",
"watch": "pnpm build --watch"
},
"pnpm": {
Expand All @@ -26,6 +31,7 @@
"eslint-plugin-import": "^2.32.0",
"eslint-plugin-prettier": "^5.5.4",
"eslint-plugin-unicorn": "^62.0.0",
"happy-dom": "^20.0.11",
"npm-run-all": "^4.1.5",
"postcss": "^8.5.6",
"postcss-lit": "^1.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dev": "vite build --watch",
"build": "tsc && vite build",
"preview": "vite preview",
"test": "eslint ."
"lint": "eslint ."
},
"dependencies": {
"@codemirror/lang-javascript": "^6.2.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dev:ts": "tsc --watch",
"dev:app": "nodemon --watch ./dist ./dist/index.js",
"build": "tsc -p ./tsconfig.json",
"test": "eslint ."
"lint": "eslint ."
},
"dependencies": {
"@fastify/static": "^9.0.0",
Expand Down
110 changes: 110 additions & 0 deletions packages/backend/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()
})
})
})
Loading