-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.test.ts
83 lines (70 loc) 路 2.26 KB
/
index.test.ts
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
// @ts-nocheck
import './setup'
import { resolve } from 'path'
import { listen } from '../src'
jest.mock('clipboardy')
const clipboardy = require('clipboardy')
clipboardy.write = jest.fn().mockImplementation(() => Promise.resolve())
jest.mock('open')
const open = require('open')
open.mockImplementation(() => Promise.resolve())
// eslint-disable-next-line no-console
console.log = jest.fn()
function handle (req, res) {
res.end(req.url)
}
describe('listhen', () => {
let listener
afterEach(async () => {
if (listener) {
await listener.close()
listener = null
}
})
test('listen (no args)', async () => {
listener = await listen(handle)
expect(listener.url.startsWith('http://')).toBe(true)
})
test('listen (http)', async () => {
listener = await listen(handle, {
isTest: false,
autoClose: false,
clipboard: true,
open: true,
baseURL: '/foo/bar'
})
expect(listener.url.startsWith('http://')).toBe(true)
expect(listener.url.endsWith('/foo/bar')).toBe(true)
// eslint-disable-next-line no-console
// expect(console.log).toHaveBeenCalledWith(expect.stringMatching('\n > Local: http://localhost:3000/foo/bar'))
})
test('listen (https - selfsigned)', async () => {
listener = await listen(handle, { https: true })
expect(listener.url.startsWith('https://')).toBe(true)
})
test('listen (https - custom)', async () => {
listener = await listen(handle, {
https: true,
certificate: {
key: resolve(__dirname, 'fixture/cert/key.pem'),
cert: resolve(__dirname, 'fixture/cert/cert.pem')
}
})
expect(listener.url.startsWith('https://')).toBe(true)
})
test('silent errors', async () => {
clipboardy.write.mockImplementationOnce(() => Promise.reject(new Error('Error')))
open.mockImplementationOnce(() => Promise.reject(new Error('Error')))
listener = await listen(handle, { isTest: false, clipboard: true, open: true })
})
test('double close', async () => {
listener = await listen(handle, { isTest: false, clipboard: true, open: true })
await listener.close()
await listener.close()
})
test('autoClose', async () => {
/* not passing close */ await listen(handle)
// @ts-ignore
process.emit('exit')
})
})