Skip to content

Commit cbb4cd9

Browse files
committed
test: First run at tests
1 parent f815cb7 commit cbb4cd9

File tree

6 files changed

+91
-4
lines changed

6 files changed

+91
-4
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ yarn.lock
44
.vs/
55
.idea/
66
dist/
7+
test/*.js
8+
test/*.js.map
9+
test/*.tsbuildinfo

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
"types": "dist/index.d.ts",
77
"scripts": {
88
"prepublishOnly": "yarn build",
9+
"test": "yarn build && yarn build:test && yarn test:run",
10+
"test:run": "node test/suite",
911
"test:lint": "eslint --ext ts src examples",
1012
"lint": "eslint --ext ts src examples --fix",
11-
"build": "tsc -p ."
13+
"build": "tsc -p .",
14+
"build:test": "tsc -p test"
1215
},
1316
"repository": {
1417
"type": "git",
@@ -32,10 +35,12 @@
3235
"homepage": "https://github.com/kyranet/veza#readme",
3336
"devDependencies": {
3437
"@types/node": "^12.0.0",
38+
"@types/tape": "^4.2.33",
3539
"@typescript-eslint/eslint-plugin": "^1.9.0",
3640
"eslint": "^5.16.0",
3741
"eslint-config-klasa": "github:dirigeants/klasa-lint",
3842
"eslint-config-marine": "^2.2.0",
43+
"tape": "^4.10.1",
3944
"typescript": "^3.4.5"
4045
}
4146
}

src/lib/Node.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ export class Node extends EventEmitter {
1919
/**
2020
* The server for this Node, if serving
2121
*/
22-
public server: NodeServer | null = null;
22+
public server!: NodeServer | null;
2323

2424
/**
2525
* The servers this Node is connected to
2626
*/
27-
public servers: Map<string, NodeSocket> = new Map();
27+
public readonly servers!: Map<string, NodeSocket>;
2828

2929
/**
3030
* The time between connection retries
@@ -42,7 +42,7 @@ export class Node extends EventEmitter {
4242
this.retryTime = retryTime;
4343
Object.defineProperties(this, {
4444
server: { value: null, writable: true },
45-
servers: { value: null, writable: true }
45+
servers: { value: new Map() }
4646
});
4747
}
4848

test/HelloWorld.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Node } from '../dist/index';
2+
import * as test from 'tape';
3+
4+
test('Basic Empty Node', t => {
5+
t.plan(5);
6+
7+
const node = new Node('Test', { maxRetries: 10, retryTime: 1000 });
8+
t.equal(node.name, 'Test', 'Expected name to be Test, as configured.');
9+
t.equal(node.maxRetries, 10, 'Expected maxRetries to be 10, as configured.');
10+
t.equal(node.retryTime, 1000, 'Expected retryTime to be 1000, as configured.');
11+
t.equal(node.server, null, 'Expected server to be null, as it has not served.');
12+
t.equal(node.servers.size, 0, 'Expected servers to be zero, as it has not connected.');
13+
});
14+
15+
test('Basic Empty Server (Connect and Disconnect)', async t => {
16+
t.plan(16);
17+
18+
const node = new Node('Server');
19+
node.once('server.ready', server => {
20+
t.equal(server.clients.size, 0, 'The server should not have a connected client.');
21+
t.equal(server.name, 'Server', 'The server should be named after the node.');
22+
t.equal(server.node, node, 'The Servers node should be its parent.');
23+
t.notEqual(server.server, null, 'The internal server should not be null.');
24+
});
25+
node.once('server.destroy', server => {
26+
t.equal(server.clients.size, 0, 'The server should not have a connected client.');
27+
t.equal(server.name, 'Server', 'The server should be named after the node.');
28+
t.equal(server.node, node, 'The Servers node should be its parent.');
29+
t.equal(server.server, null, 'The internal server should be null.');
30+
});
31+
32+
try {
33+
t.equal(node.server, null, 'The server should be null as this is not serving.');
34+
35+
// Connect
36+
await node.serve(8001);
37+
t.notEqual(node.server, null, 'The server should now not be null as this is now serving.');
38+
t.equal(node.server!.clients.size, 0, 'The server should not have a connected client.');
39+
t.equal(node.server!.name, 'Server', 'The server should be named after the node.');
40+
t.equal(node.server!.node, node, 'The Servers node should be its parent.');
41+
t.notEqual(node.server!.server, null, 'The internal server should not be null.');
42+
43+
// Disconnect
44+
t.true(node.server!.disconnect(), 'The disconnection should be successful.');
45+
t.equal(node.server, null, 'The server should be null as it has disconnected.');
46+
} catch {
47+
t.fail('Server should not crash.');
48+
}
49+
});

test/suite.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './HelloWorld';

test/tsconfig.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"compileOnSave": true,
3+
"compilerOptions": {
4+
"allowSyntheticDefaultImports": true,
5+
"alwaysStrict": true,
6+
"declaration": false,
7+
"emitDecoratorMetadata": false,
8+
"experimentalDecorators": false,
9+
"incremental": true,
10+
"lib": [
11+
"esnext",
12+
"esnext.array",
13+
"esnext.asynciterable",
14+
"esnext.intl",
15+
"esnext.symbol"
16+
],
17+
"module": "commonjs",
18+
"moduleResolution": "node",
19+
"noUnusedLocals": true,
20+
"pretty": false,
21+
"resolveJsonModule": true,
22+
"sourceMap": true,
23+
"strict": true,
24+
"strictBindCallApply": true,
25+
"target": "esnext",
26+
"newLine": "LF",
27+
"types": ["node", "tape"]
28+
}
29+
}

0 commit comments

Comments
 (0)