Przeglądaj źródła

add chai to tests

master
nitowa 3 lat temu
rodzic
commit
9f53abe0e3
3 zmienionych plików z 337 dodań i 189 usunięć
  1. 213
    7
      package-lock.json
  2. 4
    1
      package.json
  3. 120
    181
      test/Test.ts

+ 213
- 7
package-lock.json Wyświetl plik

@@ -1,15 +1,18 @@
1 1
 {
2 2
   "name": "rpclibrary",
3
-  "version": "2.3.1",
3
+  "version": "2.4.1",
4 4
   "lockfileVersion": 2,
5 5
   "requires": true,
6 6
   "packages": {
7 7
     "": {
8
-      "version": "2.3.1",
8
+      "version": "2.4.1",
9 9
       "license": "MIT",
10 10
       "dependencies": {
11
+        "@types/chai": "^4.2.21",
11 12
         "@types/socket.io": "^2.1.8",
12 13
         "@types/socket.io-client": "^1.4.33",
14
+        "chai": "^4.3.4",
15
+        "chai-as-promised": "^7.1.1",
13 16
         "http": "0.0.0",
14 17
         "socket.io": "^2.3.0",
15 18
         "socket.io-client": "^2.3.0",
@@ -295,6 +298,11 @@
295 298
         "node": ">=8"
296 299
       }
297 300
     },
301
+    "node_modules/@types/chai": {
302
+      "version": "4.2.21",
303
+      "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.2.21.tgz",
304
+      "integrity": "sha512-yd+9qKmJxm496BOV9CMNaey8TWsikaZOwMRwPHQIjcOJM9oV+fi9ZMNw3JsVnbEEbo2gRTDnGEBv8pjyn67hNg=="
305
+    },
298 306
     "node_modules/@types/color-name": {
299 307
       "version": "1.1.1",
300 308
       "resolved": "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz",
@@ -784,6 +792,14 @@
784 792
         "inherits": "2.0.1"
785 793
       }
786 794
     },
795
+    "node_modules/assertion-error": {
796
+      "version": "1.1.0",
797
+      "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz",
798
+      "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==",
799
+      "engines": {
800
+        "node": "*"
801
+      }
802
+    },
787 803
     "node_modules/assign-symbols": {
788 804
       "version": "1.0.0",
789 805
       "resolved": "https://registry.npmjs.org/assign-symbols/-/assign-symbols-1.0.0.tgz",
@@ -1285,6 +1301,33 @@
1285 1301
         "node": ">=6"
1286 1302
       }
1287 1303
     },
1304
+    "node_modules/chai": {
1305
+      "version": "4.3.4",
1306
+      "resolved": "https://registry.npmjs.org/chai/-/chai-4.3.4.tgz",
1307
+      "integrity": "sha512-yS5H68VYOCtN1cjfwumDSuzn/9c+yza4f3reKXlE5rUg7SFcCEy90gJvydNgOYtblyf4Zi6jIWRnXOgErta0KA==",
1308
+      "dependencies": {
1309
+        "assertion-error": "^1.1.0",
1310
+        "check-error": "^1.0.2",
1311
+        "deep-eql": "^3.0.1",
1312
+        "get-func-name": "^2.0.0",
1313
+        "pathval": "^1.1.1",
1314
+        "type-detect": "^4.0.5"
1315
+      },
1316
+      "engines": {
1317
+        "node": ">=4"
1318
+      }
1319
+    },
1320
+    "node_modules/chai-as-promised": {
1321
+      "version": "7.1.1",
1322
+      "resolved": "https://registry.npmjs.org/chai-as-promised/-/chai-as-promised-7.1.1.tgz",
1323
+      "integrity": "sha512-azL6xMoi+uxu6z4rhWQ1jbdUhOMhis2PvscD/xjLqNMkv3BPPp2JyyuTHOrf9BOosGpNQ11v6BKv/g57RXbiaA==",
1324
+      "dependencies": {
1325
+        "check-error": "^1.0.2"
1326
+      },
1327
+      "peerDependencies": {
1328
+        "chai": ">= 2.1.2 < 5"
1329
+      }
1330
+    },
1288 1331
     "node_modules/chalk": {
1289 1332
       "version": "2.4.2",
1290 1333
       "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
@@ -1311,6 +1354,14 @@
1311 1354
         "node": ">=4"
1312 1355
       }
1313 1356
     },
1357
+    "node_modules/check-error": {
1358
+      "version": "1.0.2",
1359
+      "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz",
1360
+      "integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=",
1361
+      "engines": {
1362
+        "node": "*"
1363
+      }
1364
+    },
1314 1365
     "node_modules/chokidar": {
1315 1366
       "version": "2.1.8",
1316 1367
       "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-2.1.8.tgz",
@@ -1320,7 +1371,6 @@
1320 1371
         "anymatch": "^2.0.0",
1321 1372
         "async-each": "^1.0.1",
1322 1373
         "braces": "^2.3.2",
1323
-        "fsevents": "^1.2.7",
1324 1374
         "glob-parent": "^3.1.0",
1325 1375
         "inherits": "^2.0.3",
1326 1376
         "is-binary-path": "^1.0.0",
@@ -1713,6 +1763,17 @@
1713 1763
         "node": ">=0.10"
1714 1764
       }
1715 1765
     },
1766
+    "node_modules/deep-eql": {
1767
+      "version": "3.0.1",
1768
+      "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-3.0.1.tgz",
1769
+      "integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==",
1770
+      "dependencies": {
1771
+        "type-detect": "^4.0.0"
1772
+      },
1773
+      "engines": {
1774
+        "node": ">=0.12"
1775
+      }
1776
+    },
1716 1777
     "node_modules/default-require-extensions": {
1717 1778
       "version": "3.0.0",
1718 1779
       "resolved": "https://registry.npmjs.org/default-require-extensions/-/default-require-extensions-3.0.0.tgz",
@@ -2726,7 +2787,71 @@
2726 2787
       "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.9.tgz",
2727 2788
       "integrity": "sha512-oeyj2H3EjjonWcFjD5NvZNE9Rqe4UW+nQBU2HNeKw0koVLEFIhtyETyAakeAM3de7Z/SW5kcA+fZUait9EApnw==",
2728 2789
       "bundleDependencies": [
2729
-        "node-pre-gyp"
2790
+        "node-pre-gyp",
2791
+        "abbrev",
2792
+        "ansi-regex",
2793
+        "aproba",
2794
+        "are-we-there-yet",
2795
+        "balanced-match",
2796
+        "brace-expansion",
2797
+        "chownr",
2798
+        "code-point-at",
2799
+        "concat-map",
2800
+        "console-control-strings",
2801
+        "core-util-is",
2802
+        "debug",
2803
+        "deep-extend",
2804
+        "delegates",
2805
+        "detect-libc",
2806
+        "fs-minipass",
2807
+        "fs.realpath",
2808
+        "gauge",
2809
+        "glob",
2810
+        "has-unicode",
2811
+        "iconv-lite",
2812
+        "ignore-walk",
2813
+        "inflight",
2814
+        "inherits",
2815
+        "ini",
2816
+        "is-fullwidth-code-point",
2817
+        "isarray",
2818
+        "minimatch",
2819
+        "minimist",
2820
+        "minipass",
2821
+        "minizlib",
2822
+        "mkdirp",
2823
+        "ms",
2824
+        "needle",
2825
+        "nopt",
2826
+        "npm-bundled",
2827
+        "npm-packlist",
2828
+        "npmlog",
2829
+        "number-is-nan",
2830
+        "object-assign",
2831
+        "once",
2832
+        "os-homedir",
2833
+        "os-tmpdir",
2834
+        "osenv",
2835
+        "path-is-absolute",
2836
+        "process-nextick-args",
2837
+        "rc",
2838
+        "readable-stream",
2839
+        "rimraf",
2840
+        "safe-buffer",
2841
+        "safer-buffer",
2842
+        "sax",
2843
+        "semver",
2844
+        "set-blocking",
2845
+        "signal-exit",
2846
+        "string_decoder",
2847
+        "string-width",
2848
+        "strip-ansi",
2849
+        "strip-json-comments",
2850
+        "tar",
2851
+        "util-deprecate",
2852
+        "wide-align",
2853
+        "wrappy",
2854
+        "yallist"
2730 2855
       ],
2731 2856
       "dev": true,
2732 2857
       "hasInstallScript": true,
@@ -3578,6 +3703,14 @@
3578 3703
         "node": "6.* || 8.* || >= 10.*"
3579 3704
       }
3580 3705
     },
3706
+    "node_modules/get-func-name": {
3707
+      "version": "2.0.0",
3708
+      "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz",
3709
+      "integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=",
3710
+      "engines": {
3711
+        "node": "*"
3712
+      }
3713
+    },
3581 3714
     "node_modules/get-stream": {
3582 3715
       "version": "4.1.0",
3583 3716
       "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz",
@@ -3713,7 +3846,6 @@
3713 3846
         "minimist": "^1.2.5",
3714 3847
         "neo-async": "^2.6.0",
3715 3848
         "source-map": "^0.6.1",
3716
-        "uglify-js": "^3.1.4",
3717 3849
         "wordwrap": "^1.0.0"
3718 3850
       },
3719 3851
       "bin": {
@@ -5962,6 +6094,14 @@
5962 6094
       "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=",
5963 6095
       "dev": true
5964 6096
     },
6097
+    "node_modules/pathval": {
6098
+      "version": "1.1.1",
6099
+      "resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.1.tgz",
6100
+      "integrity": "sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ==",
6101
+      "engines": {
6102
+        "node": "*"
6103
+      }
6104
+    },
5965 6105
     "node_modules/pbkdf2": {
5966 6106
       "version": "3.0.17",
5967 6107
       "resolved": "https://registry.npmjs.org/pbkdf2/-/pbkdf2-3.0.17.tgz",
@@ -7436,8 +7576,7 @@
7436 7576
       "integrity": "sha512-ZCtJK8WXxHNbFNjvUKQIXZby/+ybQQkaBcM/3QhBQUfwjpdGFE9F6iWsHhF5ifQNFV/lWiOODi2VMD5AyPcQyg==",
7437 7577
       "dev": true,
7438 7578
       "dependencies": {
7439
-        "ts-node": "7.0.1",
7440
-        "tsconfig-paths": "^3.5.0"
7579
+        "ts-node": "7.0.1"
7441 7580
       },
7442 7581
       "bin": {
7443 7582
         "ts-mocha": "bin/ts-mocha"
@@ -7496,6 +7635,14 @@
7496 7635
       "integrity": "sha1-oVe6QC2iTpv5V/mqadUk7tQpAaY=",
7497 7636
       "dev": true
7498 7637
     },
7638
+    "node_modules/type-detect": {
7639
+      "version": "4.0.8",
7640
+      "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz",
7641
+      "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==",
7642
+      "engines": {
7643
+        "node": ">=4"
7644
+      }
7645
+    },
7499 7646
     "node_modules/type-fest": {
7500 7647
       "version": "0.8.1",
7501 7648
       "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz",
@@ -8529,6 +8676,11 @@
8529 8676
       "integrity": "sha512-tsAQNx32a8CoFhjhijUIhI4kccIAgmGhy8LZMZgGfmXcpMbPRUqn5LWmgRttILi6yeGmBJd2xsPkFMs0PzgPCw==",
8530 8677
       "dev": true
8531 8678
     },
8679
+    "@types/chai": {
8680
+      "version": "4.2.21",
8681
+      "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.2.21.tgz",
8682
+      "integrity": "sha512-yd+9qKmJxm496BOV9CMNaey8TWsikaZOwMRwPHQIjcOJM9oV+fi9ZMNw3JsVnbEEbo2gRTDnGEBv8pjyn67hNg=="
8683
+    },
8532 8684
     "@types/color-name": {
8533 8685
       "version": "1.1.1",
8534 8686
       "resolved": "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz",
@@ -8980,6 +9132,11 @@
8980 9132
         }
8981 9133
       }
8982 9134
     },
9135
+    "assertion-error": {
9136
+      "version": "1.1.0",
9137
+      "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz",
9138
+      "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw=="
9139
+    },
8983 9140
     "assign-symbols": {
8984 9141
       "version": "1.0.0",
8985 9142
       "resolved": "https://registry.npmjs.org/assign-symbols/-/assign-symbols-1.0.0.tgz",
@@ -9419,6 +9576,27 @@
9419 9576
       "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==",
9420 9577
       "dev": true
9421 9578
     },
9579
+    "chai": {
9580
+      "version": "4.3.4",
9581
+      "resolved": "https://registry.npmjs.org/chai/-/chai-4.3.4.tgz",
9582
+      "integrity": "sha512-yS5H68VYOCtN1cjfwumDSuzn/9c+yza4f3reKXlE5rUg7SFcCEy90gJvydNgOYtblyf4Zi6jIWRnXOgErta0KA==",
9583
+      "requires": {
9584
+        "assertion-error": "^1.1.0",
9585
+        "check-error": "^1.0.2",
9586
+        "deep-eql": "^3.0.1",
9587
+        "get-func-name": "^2.0.0",
9588
+        "pathval": "^1.1.1",
9589
+        "type-detect": "^4.0.5"
9590
+      }
9591
+    },
9592
+    "chai-as-promised": {
9593
+      "version": "7.1.1",
9594
+      "resolved": "https://registry.npmjs.org/chai-as-promised/-/chai-as-promised-7.1.1.tgz",
9595
+      "integrity": "sha512-azL6xMoi+uxu6z4rhWQ1jbdUhOMhis2PvscD/xjLqNMkv3BPPp2JyyuTHOrf9BOosGpNQ11v6BKv/g57RXbiaA==",
9596
+      "requires": {
9597
+        "check-error": "^1.0.2"
9598
+      }
9599
+    },
9422 9600
     "chalk": {
9423 9601
       "version": "2.4.2",
9424 9602
       "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
@@ -9441,6 +9619,11 @@
9441 9619
         }
9442 9620
       }
9443 9621
     },
9622
+    "check-error": {
9623
+      "version": "1.0.2",
9624
+      "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz",
9625
+      "integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII="
9626
+    },
9444 9627
     "chokidar": {
9445 9628
       "version": "2.1.8",
9446 9629
       "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-2.1.8.tgz",
@@ -9793,6 +9976,14 @@
9793 9976
       "integrity": "sha1-6zkTMzRYd1y4TNGh+uBiEGu4dUU=",
9794 9977
       "dev": true
9795 9978
     },
9979
+    "deep-eql": {
9980
+      "version": "3.0.1",
9981
+      "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-3.0.1.tgz",
9982
+      "integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==",
9983
+      "requires": {
9984
+        "type-detect": "^4.0.0"
9985
+      }
9986
+    },
9796 9987
     "default-require-extensions": {
9797 9988
       "version": "3.0.0",
9798 9989
       "resolved": "https://registry.npmjs.org/default-require-extensions/-/default-require-extensions-3.0.0.tgz",
@@ -11336,6 +11527,11 @@
11336 11527
       "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==",
11337 11528
       "dev": true
11338 11529
     },
11530
+    "get-func-name": {
11531
+      "version": "2.0.0",
11532
+      "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz",
11533
+      "integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE="
11534
+    },
11339 11535
     "get-stream": {
11340 11536
       "version": "4.1.0",
11341 11537
       "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz",
@@ -13258,6 +13454,11 @@
13258 13454
       "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=",
13259 13455
       "dev": true
13260 13456
     },
13457
+    "pathval": {
13458
+      "version": "1.1.1",
13459
+      "resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.1.tgz",
13460
+      "integrity": "sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ=="
13461
+    },
13261 13462
     "pbkdf2": {
13262 13463
       "version": "3.0.17",
13263 13464
       "resolved": "https://registry.npmjs.org/pbkdf2/-/pbkdf2-3.0.17.tgz",
@@ -14560,6 +14761,11 @@
14560 14761
       "integrity": "sha1-oVe6QC2iTpv5V/mqadUk7tQpAaY=",
14561 14762
       "dev": true
14562 14763
     },
14764
+    "type-detect": {
14765
+      "version": "4.0.8",
14766
+      "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz",
14767
+      "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g=="
14768
+    },
14563 14769
     "type-fest": {
14564 14770
       "version": "0.8.1",
14565 14771
       "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz",

+ 4
- 1
package.json Wyświetl plik

@@ -30,7 +30,7 @@
30 30
     "frontend": "node js/test/TestFrontend.js",
31 31
     "build": "npm run clean && tsc && npm run webpack",
32 32
     "clean": "rm -rf js",
33
-    "test": "npm run clean && npm run build && mocha js/test/Test.js",
33
+    "test": "npm run clean && npm run build && mocha --bail=true js/test/Test.js",
34 34
     "docs": "typedoc --out docs ./src --readme ./README.md --plugin typedoc-plugin-markdown --mode file --hideBreadcrumbs --hideSources"
35 35
   },
36 36
   "license": "MIT",
@@ -52,8 +52,11 @@
52 52
     "why-is-node-running": "^2.1.2"
53 53
   },
54 54
   "dependencies": {
55
+    "@types/chai": "^4.2.21",
55 56
     "@types/socket.io": "^2.1.8",
56 57
     "@types/socket.io-client": "^1.4.33",
58
+    "chai": "^4.3.4",
59
+    "chai-as-promised": "^7.1.1",
57 60
     "http": "0.0.0",
58 61
     "socket.io": "^2.3.0",
59 62
     "socket.io-client": "^2.3.0",

+ 120
- 181
test/Test.ts Wyświetl plik

@@ -8,6 +8,8 @@ import * as express from 'express';
8 8
 import * as fetch from 'node-fetch';
9 9
 import { PromiseIO } from "../src/PromiseIO/Server";
10 10
 import { PromiseIOClient } from "../src/PromiseIO/Client";
11
+import { assert, expect } from 'chai';
12
+var should = require('chai').should();
11 13
 
12 14
 const noop = (...args) => { }
13 15
 
@@ -20,6 +22,11 @@ function makeServer(onCallback = noop, connectionHandler = noop, hookCloseHandle
20 22
             {
21 23
                 name: 'echo',
22 24
                 call: async (s: string) => s,
25
+            }, {
26
+                name: 'complexSignature',
27
+                call: async ([a, b]) => {
28
+                    return [b, a]
29
+                }
23 30
             }, {
24 31
                 name: 'simpleSubscribe',
25 32
                 hook: async (callback) => {
@@ -140,34 +147,26 @@ describe('RPCServer', () => {
140 147
         done()
141 148
     })
142 149
 
143
-    it('should be able to use all kinds of RPC definitions', (done) => {
144
-        client.connect().then(async () => {
145
-            const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
146
-            const r1 = await client['HelloWorldRPCGroup'].echof('World')
147
-            const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R', 'P', 'C!')
148
-
150
+    it('should be able to use all kinds of RPC definitions', async () => {
151
+        await client.connect()
152
+        const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
153
+        const r1 = await client['HelloWorldRPCGroup'].echof('World')
154
+        const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R', 'P', 'C!')
149 155
 
150
-            if (r0 === 'Hello' && r1 === 'World' && r2.join('') === 'RPC!') {
151
-                done()
152
-            } else {
153
-                done(new Error("Bad response"))
154
-            }
155
-        })
156
+        expect(r0).to.be.equal('Hello')
157
+        expect(r1).to.be.equal('World')
158
+        expect(r2.join('')).to.be.equal('RPC!')
156 159
     })
157 160
 
158
-    it('new RPCServer() should fail on bad RPC', (done) => {
159
-        try {
161
+    it('new RPCServer() should fail on unnamed RPC', async () => {
162
+        expect(() => {
160 163
             const sv = new RPCServer([{
161 164
                 name: 'bad',
162 165
                 RPCs: () => [
163 166
                     (aaa, bbb, ccc) => { return aaa + bbb + ccc }
164 167
                 ]
165 168
             }])
166
-            sv.listen(20001)
167
-            done(new Error("Didn't fail with bad RPC"))
168
-        } catch (badRPCError) {
169
-            done()
170
-        }
169
+        }).to.throw()
171 170
     })
172 171
 })
173 172
 
@@ -228,31 +227,21 @@ describe('RPCServer with premade http server', () => {
228 227
         done()
229 228
     })
230 229
 
231
-    it('should serve REST', (done) => {
232
-        fetch('http://localhost:8080/REST_ping').then(response => {
233
-            response.text().then(text => {
234
-                if (text === "REST_pong")
235
-                    done()
236
-                else
237
-                    done(new Error("REST repsonse was " + text))
238
-            })
239
-        })
230
+    it('should serve REST', async () => {
231
+        const response = await fetch('http://localhost:8080/REST_ping')
232
+        const text = await response.text()
233
+        expect(text).to.be.equal("REST_pong")
240 234
     })
241 235
 
242 236
 
243
-    it('should be able to use all kinds of RPC definitions', (done) => {
244
-        client.connect().then(async () => {
245
-            const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
246
-            const r1 = await client['HelloWorldRPCGroup'].echof('World')
247
-            const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R', 'P', 'C!')
248
-
249
-
250
-            if (r0 === 'Hello' && r1 === 'World' && r2.join('') === 'RPC!') {
251
-                done()
252
-            } else {
253
-                done(new Error("Bad response"))
254
-            }
255
-        })
237
+    it('should be able to use all kinds of RPC definitions', async () => {
238
+        await client.connect()
239
+        const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
240
+        const r1 = await client['HelloWorldRPCGroup'].echof('World')
241
+        const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R', 'P', 'C!')
242
+        expect(r0).to.be.equal('Hello')
243
+        expect(r1).to.be.equal('World')
244
+        expect(r2.join('')).to.be.equal('RPC!')
256 245
     })
257 246
 })
258 247
 
@@ -272,8 +261,8 @@ describe('should be able to attach to non-standard path', () => {
272 261
                 }
273 262
             ]
274 263
         }])
275
-        server.listen(21003, {path: '/test'})
276
-        client = new RPCSocket(21003, 'localhost', {path: '/test'})
264
+        server.listen(21003, { path: '/test' })
265
+        client = new RPCSocket(21003, 'localhost', { path: '/test' })
277 266
         done()
278 267
     })
279 268
 
@@ -284,19 +273,14 @@ describe('should be able to attach to non-standard path', () => {
284 273
         done()
285 274
     })
286 275
 
287
-    it('should be able to use all kinds of RPC definitions', (done) => {
288
-        client.connect().then(async () => {
289
-            const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
290
-            const r1 = await client['HelloWorldRPCGroup'].echof('World')
291
-            const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R', 'P', 'C!')
292
-
293
-
294
-            if (r0 === 'Hello' && r1 === 'World' && r2.join('') === 'RPC!') {
295
-                done()
296
-            } else {
297
-                done(new Error("Bad response"))
298
-            }
299
-        })
276
+    it('should be able to use all kinds of RPC definitions', async () => {
277
+        await client.connect()
278
+        const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
279
+        const r1 = await client['HelloWorldRPCGroup'].echof('World')
280
+        const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R', 'P', 'C!')
281
+        expect(r0).to.be.equal('Hello')
282
+        expect(r1).to.be.equal('World')
283
+        expect(r2.join('')).to.be.equal('RPC!')
300 284
     })
301 285
 })
302 286
 
@@ -366,22 +350,12 @@ describe('can attach multiple RPCServers to same http server', () => {
366 350
         done()
367 351
     })
368 352
 
369
-    it('both servers should answer', (done) => {
370
-        client['HelloWorldRPCGroup'].echo("test").then(res => {
371
-            if(res != "test"){
372
-                done(new Error("response was "+res))
373
-            }else{
374
-                client2['Grp2'].test().then(res => {
375
-                    if(res != "/test"){
376
-                        done(new Error("response2 was "+res))
377
-                    }else{
378
-                        done()
379
-                    }
380
-                })
381
-            }
382
-        })
353
+    it('both servers should answer', async () => {
354
+        const res = await client['HelloWorldRPCGroup'].echo("test")
355
+        expect(res).to.equal('test')
356
+        const res2 = await client2['Grp2'].test()
357
+        expect(res2).to.equal('/test')
383 358
     })
384
-
385 359
 })
386 360
 
387 361
 describe("can attach second RPCServer if first is already running", () => {
@@ -390,7 +364,7 @@ describe("can attach second RPCServer if first is already running", () => {
390 364
         {
391 365
             name: 'HelloWorldRPCGroup',
392 366
             RPCs: [
393
-                function echo (x) { return x}, //named function variable
367
+                function echo(x) { return x }, //named function variable
394 368
                 function echof(x) { return x }, //named function
395 369
                 {
396 370
                     name: 'echoExplicit', //describing object
@@ -409,7 +383,7 @@ describe("can attach second RPCServer if first is already running", () => {
409 383
         }
410 384
     ]
411 385
 
412
-    it("attaches correctly", done => {
386
+    it("attaches correctly", async () => {
413 387
         const expressServer = express()
414 388
         const httpServer = new http.Server(expressServer)
415 389
 
@@ -427,21 +401,15 @@ describe("can attach second RPCServer if first is already running", () => {
427 401
             path: "test"
428 402
         })
429 403
 
430
-        new RPCSocket(8080, 'localhost').connect().then(sock => {
431
-            new RPCSocket(8080, 'localhost', { path: "test" }).connect().then(sock2 => {
432
-                sock2.Grp2.test().then(resp => {
433
-                    if(resp === "/test") 
434
-                        done()
435
-                    else   
436
-                        done(new Error("response did not match"))
404
+        const sock = await new RPCSocket(8080, 'localhost').connect()
405
+        const sock2 = await new RPCSocket(8080, 'localhost', { path: "test" }).connect()
406
+        const resp = await sock2.Grp2.test()
407
+        expect(resp).to.be.equal("/test")
437 408
 
438
-                    server.close()
439
-                    server2.close()
440
-                    sock.close()
441
-                    sock2.close()
442
-                })
443
-            })
444
-        })
409
+        server.close()
410
+        server2.close()
411
+        sock.close()
412
+        sock2.close()
445 413
     })
446 414
 })
447 415
 
@@ -509,33 +477,19 @@ describe('RPCSocket', () => {
509 477
     })
510 478
 
511 479
 
512
-    it('should have rpc echo', (done) => {
513
-        client['test'].echo("x").then(x => {
514
-            if (x === 'x')
515
-                done()
516
-            else
517
-                done(new Error('echo RPC response did not match'))
518
-        })
480
+    it('should have rpc echo', async() => {
481
+        const x = await client['test'].echo("x")
482
+        expect(x).to.be.equal('x')
519 483
     })
520 484
 
521
-    it('should add up to 6', (done) => {
522
-        client['test'].add(1, 2, 3).then(x => {
523
-            if (x === 6)
524
-                done()
525
-            else
526
-                done(new Error('add RPC response did not match'))
527
-        })
485
+    it('should add up to 6', async() => {
486
+        const sum = await client['test'].add(1, 2, 3)
487
+        expect(sum).to.be.equal(6)
528 488
     })
529 489
 
530
-    it('should subscribe with success', (done) => {
531
-        client['test'].simpleSubscribe(console.log).then(res => {
532
-            if (res.topic === 'test') {
533
-                done()
534
-            } else {
535
-                console.error(res)
536
-                done(new Error('Subscribe did not return success'))
537
-            }
538
-        })
490
+    it('should subscribe with success', async () => {
491
+        const res = await client['test'].simpleSubscribe(noop)
492
+        expect(res.topic).to.be.equal('test')
539 493
     })
540 494
 
541 495
     it('subscribe should call back', (done) => {
@@ -616,38 +570,30 @@ describe('It should do unhook', () => {
616 570
         server.close()
617 571
     })
618 572
 
619
-    it('Subscribe with param', (done) => {
620
-        client['test'].subscribeWithParam("OK", noop).then(async (res) => {
621
-            if (res.uuid === candy) {
622
-                done()
623
-            } else
624
-                done(new Error("Results did not match " + res.uuid))
625
-        })
573
+    it('Subscribe with param', async () => {
574
+        const res = await client['test'].subscribeWithParam("OK", noop)
575
+        expect(res.uuid).to.be.equal(candy)
626 576
     })
627 577
 
628 578
     let run = 0
629 579
     const expected = [yesCandy, noCandy, noCandy, noCandy]
630 580
 
631
-    it('Unhook+unsubscribe should stop callbacks', (done) => {
632
-
633
-        client['test'].subscribe(function myCallback(c) {
581
+    it('Unhook+unsubscribe should stop callbacks', async() => {
582
+        await client['test'].subscribe(function myCallback(c) {
634 583
             if (run == 1)
635 584
                 (myCallback as any).destroy()
585
+            expect(c).to.equal(expected[run++])
586
+        })
636 587
 
637
-            if (c !== expected[run++]) {
638
-                done(new Error(`Wrong candy '${c}' in iteration '${run - 1}'`))
639
-            }
640
-        }).then(async function (res) {
641
-            const r1 = await client['test'].publish()
642
-            const r3 = await client['test'].unsubscribe()
643
-            const r2 = await client['test'].publish()
644
-            const r4 = await client['test'].publish()
588
+        const r1 = await client['test'].publish()
589
+        const r3 = await client['test'].unsubscribe()
590
+        const r2 = await client['test'].publish()
591
+        const r4 = await client['test'].publish()
645 592
 
646
-            if (r1 === yesCandy && r3 === noCandy && r2 === noCandy && r4 === noCandy)
647
-                done()
648
-            else
649
-                done(new Error("Results did not match: " + [r1, r2, r3, r4]))
650
-        })
593
+        expect(r1).to.be.equal(yesCandy)
594
+        expect(r2).to.be.equal(noCandy)
595
+        expect(r3).to.be.equal(noCandy)
596
+        expect(r4).to.be.equal(noCandy)
651 597
     })
652 598
 })
653 599
 
@@ -700,41 +646,33 @@ describe('Sesame should unlock the socket', () => {
700 646
         server.close()
701 647
     })
702 648
 
703
-    it('should work with sesame', (done) => {
704
-        client.test.checkCandy().then(c => done())
649
+    it('should work with sesame', async () => {
650
+        const c = client.test.checkCandy()
651
+        expect(c).to.exist
705 652
     })
706 653
 
707
-    it('should work with multiple params', (done) => {
708
-        client.test['manyParams']('a', 'b', 'c', 'd').then(c => {
709
-            if (c[0] == 'a' && c[1] === 'b' && c[2] === 'c' && c[3] === 'd')
710
-                done()
711
-        })
654
+    it('should work with multiple params', async () => {
655
+        const c = await client.test['manyParams']('a', 'b', 'c', 'd')
656
+        expect(c[0]).to.be.equal('a')
657
+        expect(c[1]).to.be.equal('b')
658
+        expect(c[2]).to.be.equal('c')
659
+        expect(c[3]).to.be.equal('d')
712 660
     })
713 661
 
714
-    it('should not work without sesame', (done) => {
662
+    it('should not work without sesame', async () => {
715 663
         const sock = new RPCSocket(21004, "localhost")
716
-        sock.connect().then(async (cli) => {
717
-            if (!cli.test)
718
-                done()
719
-            else {
720
-                done(new Error("Function supposed to be removed without sesame"))
721
-            }
722
-            cli.close()
723
-            sock.close()
724
-        })
664
+        const cli = await sock.connect()
665
+        expect(cli.test).to.not.exist
666
+        cli.close()
667
+        sock.close()
725 668
     })
726 669
 
727
-    it('should fail with wrong sesame', (done) => {
670
+    it('should fail with wrong sesame', async () => {
728 671
         const sock = new RPCSocket(21004, "localhost")
729
-        sock.connect('abasd').then(async (cli) => {
730
-            if (!cli.test)
731
-                done()
732
-            else {
733
-                done(new Error("Function supposed to be removed without sesame"))
734
-            }
735
-            cli.close()
736
-            sock.close()
737
-        })
672
+        const cli = await sock.connect('iamwrong')
673
+        expect(cli.test).to.not.exist
674
+        cli.close()
675
+        sock.close()
738 676
     })
739 677
 
740 678
     it('callback should work with sesame', (done) => {
@@ -775,21 +713,21 @@ describe('Error handling', () => {
775 713
                 a: 'a',
776 714
                 b: 'b'
777 715
             })
778
-                .then(r => {
779
-                    if (r != null)
780
-                        done(new Error("UNEXPECTED RESULT " + r))
781
-                })
782
-                .catch((e) => {
783
-                    if (e.message === errtxt)
784
-                        done()
785
-                    else
786
-                        done(e)
787
-                })
788
-                .finally(() => {
789
-                    cli.close()
790
-                    sock.close()
791
-                    server.close()
792
-                })
716
+            .then(r => {
717
+                if (r != null)
718
+                    done(new Error("UNEXPECTED RESULT " + r))
719
+            })
720
+            .catch((e) => {
721
+                if (e.message === errtxt)
722
+                    done()
723
+                else
724
+                    done(e)
725
+            })
726
+            .finally(() => {
727
+                cli.close()
728
+                sock.close()
729
+                server.close()
730
+            })
793 731
         })
794 732
     })
795 733
 
@@ -926,6 +864,7 @@ describe("Class binding", () => {
926 864
     let serv: RPCServer<myExporterIfc>
927 865
     let sock: RPCSocket & myExporterIfc
928 866
     let allowed = true
867
+    const SESAME = 'xyz'
929 868
 
930 869
     class MyExporter implements RPCExporter<myExporterIfc>{
931 870
         name = "MyExporter" as "MyExporter"
@@ -960,12 +899,12 @@ describe("Class binding", () => {
960 899
                     if (exporter.name === 'MyExporter') {
961 900
                         if (!allowed) return false
962 901
                         allowed = false
963
-                        return sesame === 'xxx';
902
+                        return sesame === SESAME;
964 903
                     } else {
965 904
                         return false
966 905
                     }
967 906
                 },
968
-                sesame: "xxx"
907
+                sesame: SESAME
969 908
             })
970 909
         serv.listen(21004)
971 910
         done()
@@ -973,7 +912,7 @@ describe("Class binding", () => {
973 912
 
974 913
     beforeEach((done) => {
975 914
         const s = new RPCSocket<myExporterIfc>(21004, 'localhost')
976
-        s.connect("xxx").then(conn => {
915
+        s.connect(SESAME).then(conn => {
977 916
             sock = conn
978 917
             done()
979 918
         })
@@ -1049,9 +988,9 @@ describe("attaching handlers before connecting", () => {
1049 988
         })
1050 989
 
1051 990
         sock.connect().then(_ => {
1052
-            sock.call("unknownRPC123", "AAAAA").catch(e => {  }).then(x => {
1053
-                console.log("X",x);
1054
-                
991
+            sock.call("unknownRPC123", "AAAAA").catch(e => { }).then(x => {
992
+                console.log("X", x);
993
+
1055 994
             })
1056 995
         }).catch(e => {
1057 996
             console.log("unexpected connect catch clause");

Ładowanie…
Anuluj
Zapisz