Skip to content

Commit 9fdbdc3

Browse files
elmccdlaggingreflex
authored andcommitted
Add basic unit tests with Ava runner
1 parent 2c73a12 commit 9fdbdc3

File tree

3 files changed

+98
-4
lines changed

3 files changed

+98
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ The example directory contains a more detailed example.
4545

4646
## Test
4747

48-
There are currently no tests.
49-
If you have ideas,
50-
please open an issue.
48+
```sh
49+
npm test
50+
```
5151

5252
## Future
5353

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"name": "portscanner",
33
"description": "Asynchronous port scanner for Node.js",
4+
"scripts": {
5+
"test": "ava"
6+
},
47
"keywords": [
58
"portscanner",
69
"port",
@@ -29,7 +32,9 @@
2932
"dependencies": {
3033
"async": "0.1.15"
3134
},
32-
"devDependencies": {},
35+
"devDependencies": {
36+
"ava": "^0.4.2"
37+
},
3338
"engines": {
3439
"node": ">=0.4",
3540
"npm": ">=1.0.0"

test.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
import net from 'net';
4+
5+
import test from 'ava';
6+
7+
import portScanner from './lib/portscanner.js'
8+
9+
test.before('Set #1 test server', t => {
10+
const server = net.createServer();
11+
server.listen(3000, '127.0.0.1', () => t.end());
12+
});
13+
14+
test.before('Set #2 test server', t => {
15+
const server2 = net.createServer();
16+
server2.listen(2999, '127.0.0.1', () => t.end());
17+
});
18+
19+
20+
/* checkPortStatus */
21+
22+
test('checkPortStatus - taken', t => {
23+
t.plan(2);
24+
25+
portScanner.checkPortStatus(3000, '127.0.0.1', (error, port) => {
26+
t.is(error, null);
27+
t.is(port, 'open');
28+
});
29+
});
30+
31+
test('checkPortStatus - free', t => {
32+
t.plan(2);
33+
34+
portScanner.checkPortStatus(3001, '127.0.0.1', (error, port) => {
35+
t.is(error, null);
36+
t.is(port, 'closed');
37+
});
38+
});
39+
40+
41+
/* findPortInUse */
42+
43+
test('findPortInUse - taken port in range', t => {
44+
t.plan(2);
45+
46+
portScanner.findAPortInUse(3000, 3010, '127.0.0.1', (error, port) => {
47+
t.is(error, null);
48+
t.is(port, 3000);
49+
});
50+
});
51+
52+
test('findPortInUse - all ports in range free', t => {
53+
t.plan(2);
54+
55+
portScanner.findAPortInUse(3001, 3010, '127.0.0.1', (error, port) => {
56+
t.is(error, null);
57+
t.false(port);
58+
});
59+
});
60+
61+
62+
/* findPortNotInUse */
63+
64+
test('findAPortNotInUse - start from free port', t => {
65+
t.plan(2);
66+
67+
portScanner.findAPortNotInUse(3001, 3010, '127.0.0.1', (error, port) => {
68+
t.is(error, null);
69+
t.true(port >= 3001 && port <= 3010);
70+
});
71+
});
72+
73+
test('findAPortNotInUse - start from taken port', t => {
74+
t.plan(2);
75+
76+
portScanner.findAPortNotInUse(3000, 3010, '127.0.0.1', (error, port) => {
77+
t.is(error, null);
78+
t.true(port >= 3001 && port <= 3010);
79+
});
80+
});
81+
82+
test('findAPortNotInUse - all ports in range taken', t => {
83+
t.plan(2);
84+
85+
portScanner.findAPortNotInUse(2999, 3000, '127.0.0.1', (error, port) => {
86+
t.is(error, null);
87+
t.false(port);
88+
});
89+
});

0 commit comments

Comments
 (0)