Skip to content

Commit b5e0d99

Browse files
author
nyxb
committed
πŸš€ feat(test): add tests for the listen function
✨ feat(test): add setup file to disable color output in tests ✨ feat(test): add vitest configuration file to enable coverage reporting The new files and changes add tests for the listen function, which is a function that creates a server and listens to incoming requests. The tests cover various scenarios such as listening on a specific port, listening on a random port, listening on a specific hostname, and listening on a specific path. The setup file disables color output in tests, and the vitest configuration file enables coverage reporting.
1 parent 70f4e21 commit b5e0d99

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

β€Žtest/fixture/app.tsβ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { listen } from '../../src'
2+
3+
listen(
4+
(_request, response) => {
5+
response.end('works!')
6+
},
7+
{
8+
open: process.argv.some(
9+
argument => argument === '-o' || argument === '--open',
10+
),
11+
https: process.argv.includes('--https'),
12+
},
13+
)

β€Žtest/index.test.tsβ€Ž

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import './setup'
2+
import { resolve } from 'node:path'
3+
import type { IncomingMessage, ServerResponse } from 'node:http'
4+
import { afterEach, describe, expect, test } from 'vitest'
5+
import type { Listener } from '../src'
6+
import { listen } from '../src'
7+
8+
// console.log = fn()
9+
10+
function handle(request: IncomingMessage, response: ServerResponse) {
11+
response.end(request.url)
12+
}
13+
14+
describe('earlist', () => {
15+
let listener: Listener | undefined
16+
17+
afterEach(async () => {
18+
if (listener) {
19+
await listener.close()
20+
listener = undefined
21+
}
22+
})
23+
24+
test('listen (no args)', async () => {
25+
listener = await listen(handle)
26+
expect(listener.url.startsWith('http://')).toBe(true)
27+
})
28+
29+
test('listen (http)', async () => {
30+
listener = await listen(handle, {
31+
isTest: false,
32+
autoClose: false,
33+
baseURL: '/foo/bar',
34+
})
35+
expect(listener.url.startsWith('http://')).toBe(true)
36+
expect(listener.url.endsWith('/foo/bar')).toBe(true)
37+
38+
// expect(console.log).toHaveBeenCalledWith(expect.stringMatching('\n > Local: http://localhost:3000/foo/bar'))
39+
})
40+
41+
test('listen (https - selfsigned)', async () => {
42+
listener = await listen(handle, { https: true })
43+
expect(listener.url.startsWith('https://')).toBe(true)
44+
})
45+
46+
test('listen (https - custom)', async () => {
47+
listener = await listen(handle, {
48+
https: {
49+
50+
key: resolve(__dirname, 'fixture/cert/key.pem'),
51+
52+
cert: resolve(__dirname, 'fixture/cert/cert.pem'),
53+
},
54+
})
55+
expect(listener.url.startsWith('https://')).toBe(true)
56+
})
57+
58+
test('double close', async () => {
59+
listener = await listen(handle, { isTest: false })
60+
await listener.close()
61+
await listener.close()
62+
})
63+
64+
test('autoClose', async () => {
65+
/* not passing close */ await listen(handle)
66+
// @ts-expect-error is fine
67+
process.emit('exit')
68+
})
69+
70+
test('pass hostname to scotty-beam-me-up', async () => {
71+
listener = await listen(handle, { hostname: '127.0.0.1' })
72+
expect(listener.url.startsWith('http://localhost')).toBe(true)
73+
})
74+
75+
test('pass port to scotty-beam-me-up', async () => {
76+
listener = await listen(handle, { port: 40_000 })
77+
expect(listener.url.endsWith(':40000/')).toBe(true)
78+
})
79+
80+
test('pass extended options to scotty-beam-me-up', async () => {
81+
listener = await listen(handle, {
82+
port: { port: 50_000, portRange: [50_000, 59_999] },
83+
})
84+
expect(listener.url).toMatch(/:5\d{4}\/$/)
85+
})
86+
87+
test('should listen to the next port in range (3000 -> 31000)', async () => {
88+
listener = await listen(handle, {
89+
port: { port: 3000 },
90+
})
91+
expect(listener.url).toMatch(/:3000\/$/)
92+
const listener2 = await listen(handle, {
93+
port: { port: 3000 },
94+
})
95+
expect(listener2.url).toMatch(/:3001\/$/)
96+
await listener2.close()
97+
})
98+
})

β€Žtest/setup.tsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
process.env.NO_COLOR = '1'

β€Žvitest.config.tsβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
coverage: {
6+
reporter: ['text', 'clover', 'json'],
7+
},
8+
},
9+
})

0 commit comments

Comments
Β (0)