Skip to content

Commit

Permalink
Add basic unit tests with Ava runner
Browse files Browse the repository at this point in the history
  • Loading branch information
elmccd authored and laggingreflex committed Nov 18, 2016
1 parent 2c73a12 commit 9fdbdc3
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ The example directory contains a more detailed example.

## Test

There are currently no tests.
If you have ideas,
please open an issue.
```sh
npm test
```

## Future

Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "portscanner",
"description": "Asynchronous port scanner for Node.js",
"scripts": {
"test": "ava"
},
"keywords": [
"portscanner",
"port",
Expand Down Expand Up @@ -29,7 +32,9 @@
"dependencies": {
"async": "0.1.15"
},
"devDependencies": {},
"devDependencies": {
"ava": "^0.4.2"
},
"engines": {
"node": ">=0.4",
"npm": ">=1.0.0"
Expand Down
89 changes: 89 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import path from 'path';
import fs from 'fs';
import net from 'net';

import test from 'ava';

import portScanner from './lib/portscanner.js'

test.before('Set #1 test server', t => {
const server = net.createServer();
server.listen(3000, '127.0.0.1', () => t.end());
});

test.before('Set #2 test server', t => {
const server2 = net.createServer();
server2.listen(2999, '127.0.0.1', () => t.end());
});


/* checkPortStatus */

test('checkPortStatus - taken', t => {
t.plan(2);

portScanner.checkPortStatus(3000, '127.0.0.1', (error, port) => {
t.is(error, null);
t.is(port, 'open');
});
});

test('checkPortStatus - free', t => {
t.plan(2);

portScanner.checkPortStatus(3001, '127.0.0.1', (error, port) => {
t.is(error, null);
t.is(port, 'closed');
});
});


/* findPortInUse */

test('findPortInUse - taken port in range', t => {
t.plan(2);

portScanner.findAPortInUse(3000, 3010, '127.0.0.1', (error, port) => {
t.is(error, null);
t.is(port, 3000);
});
});

test('findPortInUse - all ports in range free', t => {
t.plan(2);

portScanner.findAPortInUse(3001, 3010, '127.0.0.1', (error, port) => {
t.is(error, null);
t.false(port);
});
});


/* findPortNotInUse */

test('findAPortNotInUse - start from free port', t => {
t.plan(2);

portScanner.findAPortNotInUse(3001, 3010, '127.0.0.1', (error, port) => {
t.is(error, null);
t.true(port >= 3001 && port <= 3010);
});
});

test('findAPortNotInUse - start from taken port', t => {
t.plan(2);

portScanner.findAPortNotInUse(3000, 3010, '127.0.0.1', (error, port) => {
t.is(error, null);
t.true(port >= 3001 && port <= 3010);
});
});

test('findAPortNotInUse - all ports in range taken', t => {
t.plan(2);

portScanner.findAPortNotInUse(2999, 3000, '127.0.0.1', (error, port) => {
t.is(error, null);
t.false(port);
});
});

0 comments on commit 9fdbdc3

Please sign in to comment.