@locustjs/test 2.0.1 → 2.0.2

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/src/TestRunner.js CHANGED
@@ -4,6 +4,9 @@ import {
4
4
  isFunction,
5
5
  isArray,
6
6
  isIterable,
7
+ isString,
8
+ isEmpty,
9
+ typename,
7
10
  } from "@locustjs/base";
8
11
  import fs from "fs";
9
12
  import path from "path";
@@ -67,13 +70,26 @@ class TestRunner {
67
70
  });
68
71
  }
69
72
  }
70
- if (!(test instanceof Test)) {
73
+
74
+ if (test == null) {
71
75
  this._unknown++;
72
76
 
73
77
  this._errors.push({
74
78
  index: i,
75
79
  err: new TestException({
76
- message: `Given test is not an instance of '@locustjs/test:Test' class.`,
80
+ message: `No test was given.`,
81
+ code: 1505,
82
+ status: "no-test",
83
+ }),
84
+ });
85
+ } else if (!(test instanceof Test)) {
86
+ this._unknown++;
87
+ let testType = typename(test);
88
+
89
+ this._errors.push({
90
+ index: i,
91
+ err: new TestException({
92
+ message: `Given test is '${testType}'. Expected a function or an instance of '@locustjs/test:Test' class.`,
77
93
  code: 1504,
78
94
  status: "invalid-test",
79
95
  }),
@@ -83,12 +99,12 @@ class TestRunner {
83
99
  this._results.push(tr);
84
100
 
85
101
  if (isObject(tr.err)) {
86
- if (!tr.expected) {
102
+ if (!tr.satisfied) {
87
103
  this._faulted++;
88
104
  } else {
89
105
  this._failed++;
90
106
  }
91
- } else if (!tr.expected) {
107
+ } else if (!tr.satisfied) {
92
108
  this._unknown++;
93
109
  } else if (tr.success) {
94
110
  this._passed++;
@@ -116,13 +132,29 @@ class TestRunner {
116
132
  }
117
133
 
118
134
  if (isArray(tests)) {
119
- const _tests = tests
120
- .map((test) => {
135
+ const prs = tests
136
+ .map((test, i) => {
121
137
  let _test = test;
122
138
 
123
139
  if (isArray(test)) {
124
- if (test.length == 2) {
125
- _test = new Test(test[0], test[1]);
140
+ if (test.length == 2 && isString(test[0])) {
141
+ if (isFunction(test[1])) {
142
+ _test = new Test(test[0], test[1]);
143
+ } else if (test[1] instanceof Test) {
144
+ _test = test[1];
145
+
146
+ if (isEmpty(_test.name)) {
147
+ _test.name = test[0];
148
+ }
149
+ } else {
150
+ _test = test[1];
151
+ }
152
+ } else if (test.length == 1) {
153
+ if (test[0] instanceof Test) {
154
+ _test = test[0];
155
+ } else if (isFunction(test[0])) {
156
+ _test = new Test("nonamed test " + i, test[0]);
157
+ }
126
158
  }
127
159
  }
128
160
 
@@ -130,7 +162,7 @@ class TestRunner {
130
162
  })
131
163
  .map((test, i) => this._runSingle(test, onProgress, i));
132
164
 
133
- Promise.all(_tests)
165
+ Promise.all(prs)
134
166
  .then((_) => res(this.result))
135
167
  .catch((ex) => {
136
168
  this._errors.push({
@@ -193,12 +225,12 @@ class TestRunner {
193
225
  i == err.length - 1
194
226
  ? `${fgYellow}`
195
227
  : `${fgGray}error ${testResult.err.code}: `
196
- }${msg}${reset}`
228
+ }${msg}${reset}`,
197
229
  )
198
230
  .join("\n");
199
231
 
200
232
  if (isObject(testResult.err)) {
201
- if (!testResult.expected) {
233
+ if (!testResult.satisfied) {
202
234
  message += `${bright}${fgWhite}${testResult.test}: ${fgYellow}faulted${reset} ${t}`;
203
235
  message += "\n";
204
236
  message += `${fgGray}${err} ${reset}`;
@@ -207,7 +239,7 @@ class TestRunner {
207
239
  message += "\n";
208
240
  message += `${fgGray}${err} ${reset}`;
209
241
  }
210
- } else if (!testResult.expected) {
242
+ } else if (!testResult.satisfied) {
211
243
  message += `${bright}${fgWhite}${testResult.test}: ${fgMagenta}expect not used${reset} ${t}`;
212
244
 
213
245
  if (testResult.err) {
@@ -229,15 +261,21 @@ class TestRunner {
229
261
  }
230
262
 
231
263
  if (detailed && this._errors.length) {
232
- console.log("Errors:");
264
+ console.log("\nErrors:");
233
265
 
234
266
  for (let error of this._errors) {
235
267
  if (error.index !== undefined) {
236
- console.log(
237
- `${error.index}. ${
238
- error.test
239
- }: ${error.err.innerException.toString()}`
240
- );
268
+ if (error.err.innerException) {
269
+ console.log(
270
+ `${error.index}. ${
271
+ error.test
272
+ }: ${error.err.innerException.toString()}`,
273
+ );
274
+ } else {
275
+ console.log(
276
+ `${error.index}. ${error.err.status}: ${error.err.message}`,
277
+ );
278
+ }
241
279
  } else {
242
280
  console.log(`${error.err.toString()}`);
243
281
  }
@@ -274,7 +312,7 @@ class TestRunner {
274
312
  errors: this._errors,
275
313
  },
276
314
  null,
277
- "\t"
315
+ "\t",
278
316
  );
279
317
 
280
318
  if (filename == null) {
@@ -310,7 +348,18 @@ class TestRunner {
310
348
 
311
349
  if (i != tests.length - 1 || !isBool(t)) {
312
350
  if (isIterable(t)) {
313
- _tests = [..._tests, ...t];
351
+ if (
352
+ isArray(t) &&
353
+ t.length == 2 &&
354
+ isString(t[0]) &&
355
+ (isFunction(t[1]) || t[1] instanceof Test)
356
+ ) {
357
+ _tests.push(t);
358
+ } else {
359
+ _tests.push(...t);
360
+ }
361
+ } else {
362
+ _tests.push(["test " + i, t]);
314
363
  }
315
364
  }
316
365
  }
package/tests/index.js CHANGED
@@ -7,7 +7,7 @@ class Buz {}
7
7
  const tests = [
8
8
  [
9
9
  "Test 1: number",
10
- function (expect) {
10
+ function () {
11
11
  const n = 10;
12
12
 
13
13
  expect(n)
@@ -31,7 +31,7 @@ const tests = [
31
31
  ],
32
32
  [
33
33
  "Test 2: string",
34
- function (expect) {
34
+ function () {
35
35
  const n = "10";
36
36
 
37
37
  expect(n)
@@ -45,7 +45,7 @@ const tests = [
45
45
  ],
46
46
  [
47
47
  "Test 3: empty array",
48
- function (expect) {
48
+ function () {
49
49
  const n = [];
50
50
 
51
51
  expect(n).toBeDefined().toBeArray().toBeEmptyArray();
@@ -53,7 +53,7 @@ const tests = [
53
53
  ],
54
54
  [
55
55
  "Test 4: array",
56
- function (expect) {
56
+ function () {
57
57
  const n = [10];
58
58
 
59
59
  expect(n).toBeDefined().toBeArray().toBeSomeArray();
@@ -61,7 +61,7 @@ const tests = [
61
61
  ],
62
62
  [
63
63
  "Test 5: empty object",
64
- function (expect) {
64
+ function () {
65
65
  const n = {};
66
66
 
67
67
  expect(n)
@@ -75,7 +75,7 @@ const tests = [
75
75
  ],
76
76
  [
77
77
  "Test 6: object",
78
- function (expect) {
78
+ function () {
79
79
  const n = { a: 10 };
80
80
 
81
81
  expect(n)
@@ -89,22 +89,22 @@ const tests = [
89
89
  ],
90
90
  [
91
91
  "Test 7: error",
92
- function (expect) {
92
+ function () {
93
93
  const x = 30;
94
94
 
95
95
  expect(x).toBe(20);
96
96
  },
97
97
  ],
98
- ["Test 8: expect not used", function (expect) {}],
98
+ ["Test 8: expect not used", function () {}],
99
99
  [
100
100
  "Test 9: error without expect",
101
- function (expect) {
101
+ function () {
102
102
  throw "some err";
103
103
  },
104
104
  ],
105
105
  [
106
106
  "Test 10: class",
107
- function (expect) {
107
+ function () {
108
108
  const x = new Bar();
109
109
 
110
110
  expect(x)