-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.mjs
76 lines (61 loc) · 2.83 KB
/
index.test.mjs
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
// import required modules and functions
import { describe, it } from 'mocha'
import defaultFunction from './index.mjs'
import { Support } from './index.mjs'
import {expect} from 'chai'
import sinon from 'sinon'
import os from 'os'
describe('Test index.mjs default function', () => {
let consoleStub
beforeEach(() => {
consoleStub = sinon.stub(console, 'log')
})
afterEach(() => {
consoleStub.restore()
})
it('should be an async function', () => {
expect(defaultFunction).to.be.a('AsyncFunction')
})
it('should trigger an error if the port parameter is missing', async () => {
await defaultFunction().catch(err => {
expect(err.message).to.equal('No process running on port undefined')
})
})
it('should terminate process on port 9000', async () => {
let getProcessId = sinon.stub(Support.prototype, os.platform() === 'win32' ? 'getProcessIdWin32': 'getProcessId')
getProcessId.withArgs(9000).returns(12345)
const killProcess = sinon.stub(Support.prototype, "killProcess").returns(true)
await defaultFunction(9000, [], true, false)
expect( console.log.calledWith('Terminated process: 12345') ).to.be.true
sinon.assert.calledWith(getProcessId, 9000)
sinon.assert.calledWith(killProcess, 12345, true, false)
getProcessId.restore()
killProcess.restore()
})
it('should kill process on port 9000', async () => {
let getProcessId = sinon.stub(Support.prototype, os.platform() === 'win32' ? 'getProcessIdWin32': 'getProcessId')
getProcessId.withArgs(9000).returns(12345)
const killProcess = sinon.stub(Support.prototype, "killProcess").returns(true)
await defaultFunction(9000, [], true, true)
expect( console.log.calledWith('Killed process: 12345') ).to.be.true
sinon.assert.calledWith(getProcessId, 9000)
sinon.assert.calledWith(killProcess, 12345, true, true)
getProcessId.restore()
killProcess.restore()
})
it('should terminate parent process of process on port 9010', async () => {
let getProcessId = sinon.stub(Support.prototype, os.platform() === 'win32' ? 'getProcessIdWin32': 'getProcessId')
getProcessId.withArgs(9010).returns(12345)
const killProcess = sinon.stub(Support.prototype, "killProcess").returns(true)
const getParentProcess = sinon.stub(Support.prototype, "getParentProcess")
getParentProcess.withArgs(12345, ['node','node.exe']).returns({name: 'node', parentProcessId: 707111})
await defaultFunction(9010, ['node','node.exe'], true, false)
expect( console.log.calledWith('Terminated parent process \'node\': 707111') ).to.be.true
sinon.assert.calledWith(getProcessId, 9010)
sinon.assert.calledWith(getParentProcess, 12345, ['node','node.exe'])
sinon.assert.calledWith(killProcess, 707111, true, false)
getProcessId.restore()
getParentProcess.restore()
killProcess.restore()
})
})