@camperaid/watest 2.4.6 → 2.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/core/base.js CHANGED
@@ -567,6 +567,25 @@ function is_out(got, expected, msg) {
567
567
  return false;
568
568
  }
569
569
 
570
+ function throws(func, exception, msg) {
571
+ try {
572
+ func();
573
+ fail(`${msg}: no '${exception}' exception`);
574
+ } catch (e) {
575
+ is(e?.message, exception, msg);
576
+ }
577
+ }
578
+
579
+ function no_throws(func, msg) {
580
+ try {
581
+ func();
582
+ success(msg);
583
+ } catch (e) {
584
+ log_error(e);
585
+ fail(`${msg}, got: ${e?.message ?? ''} exception`);
586
+ }
587
+ }
588
+
570
589
  module.exports = {
571
590
  success,
572
591
  fail,
@@ -581,6 +600,9 @@ module.exports = {
581
600
  is_primitive,
582
601
  is_string,
583
602
 
603
+ throws,
604
+ no_throws,
605
+
584
606
  test_is,
585
607
  test_contains,
586
608
 
package/core/core.js CHANGED
@@ -231,6 +231,7 @@ module.exports = {
231
231
  success(...args) {
232
232
  return currentCore.success(...args);
233
233
  },
234
+
234
235
  failed() {
235
236
  return currentCore.failed();
236
237
  },
package/index.js CHANGED
@@ -17,6 +17,8 @@ const {
17
17
  is,
18
18
  contains,
19
19
  is_output,
20
+ no_throws,
21
+ throws,
20
22
 
21
23
  test_is,
22
24
  test_contains,
@@ -44,6 +46,7 @@ module.exports = {
44
46
  info,
45
47
  inspect,
46
48
  not_reached,
49
+ no_throws,
47
50
  ok,
48
51
  scope,
49
52
  start_session,
@@ -51,6 +54,7 @@ module.exports = {
51
54
  todo,
52
55
  test_is,
53
56
  test_contains,
57
+ throws,
54
58
  tmp_storage_dir,
55
59
  warn,
56
60
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camperaid/watest",
3
- "version": "2.4.6",
3
+ "version": "2.4.7",
4
4
  "description": "Web Application Testsuite",
5
5
  "engines": {
6
6
  "node": ">=14.15.1"
@@ -0,0 +1,75 @@
1
+ 'use strict';
2
+
3
+ const { is_output, throws, no_throws } = require('./test.js');
4
+
5
+ module.exports.test = async () => {
6
+ // throws: success
7
+ await is_output(
8
+ () =>
9
+ throws(
10
+ () => {
11
+ throw new Error('Error#1');
12
+ },
13
+ `Error#1`,
14
+ `Throws error#1`
15
+ ),
16
+ [`Ok: Throws error#1, got: Error#1`],
17
+ [],
18
+ `throws sucess`
19
+ );
20
+
21
+ // throws: fail, unexpected exception
22
+ await is_output(
23
+ () =>
24
+ throws(
25
+ () => {
26
+ throw new Error('Error#2');
27
+ },
28
+ `Error#1`,
29
+ `Wanted error#1`
30
+ ),
31
+ [],
32
+ [
33
+ `Failed: Wanted error#1;
34
+ got:
35
+ Error#2
36
+ expected:
37
+ Error#1
38
+ unexpected character: '2' at 6 pos, expected: '1' at '' line
39
+ `,
40
+ ],
41
+ `throws fail, unexpected exception`
42
+ );
43
+
44
+ // throws: fail, no exception
45
+ await is_output(
46
+ () => throws(() => {}, `Error#1`, `Wanted error#1`),
47
+ [],
48
+ [`Failed: Wanted error#1: no 'Error#1' exception`],
49
+ `throws fail, no exception`
50
+ );
51
+
52
+ // no_throws(() => {}, `No exceptions`)
53
+
54
+ // no_throws: success
55
+ await is_output(
56
+ () => no_throws(() => {}, `No exceptions`),
57
+ [`Ok: No exceptions`],
58
+ [],
59
+ `no throws: sucess`
60
+ );
61
+
62
+ // no_throws: fail
63
+ await is_output(
64
+ () =>
65
+ no_throws(() => {
66
+ throw new Error('Error#1');
67
+ }, `No exceptions`),
68
+ [],
69
+ [
70
+ v => v.startsWith('Error: Error#1'),
71
+ `Failed: No exceptions, got: Error#1 exception`,
72
+ ],
73
+ `no_throws fail`
74
+ );
75
+ };