-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunitest.js
More file actions
1126 lines (1008 loc) · 49.5 KB
/
unitest.js
File metadata and controls
1126 lines (1008 loc) · 49.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
if (typeof fcf == "undefined" && typeof module === "object" && typeof module.filename !== "undefined") {
require("fcf-framework-core");
}
fcf.module({
name: "fcf-framework-unitest:unitest.js",
dependencies: [],
lazy: [],
module: ()=>{
let Namespace = fcf.prepare(fcf, "NUnitest");
fcf.addException("UNITEST_EQUAL", "\"@{{left}}@\" and \"@{{right}}@\" are not equal");
fcf.addException("UNITEST_NOT_EQUAL", "\"@{{left}}@\" and \"@{{right}}@\" are equal");
fcf.addException("UNITEST_LESS", "\"@{{left}}@\" is not less than \"@{{right}}@\"");
fcf.addException("UNITEST_LESS_EQUAL", "\"@{{left}}@\" is not less than \"@{{right}}@\" or not equal");
fcf.addException("UNITEST_GREATER", "\"@{{left}}@\" is not greater than \"@{{right}}@\"");
fcf.addException("UNITEST_GREATER_EQUAL", "\"@{{left}}@\" is not greater than \"@{{right}}@\" or not equal");
fcf.addException("UNITEST_ERROR", "@{{error}}@");
fcf.addException("UNUTEST_WEB_COMMAND_UNSET", "Web unit test command not installed for unitest.run() (Options: webProcesses.command)");
fcf.addException("UNITEST_ERROR_EXEC_COMMAND", "Child process \"@{{process}}@\" exited with an error");
fcf.addException("UNITEST_FAILD_BROWSER_TEST", "Failed to run test in browser. Command: \"@{{command}}@\". @{{error}}@");
fcf.addException("UNITEST_FAILD_SEND_MESSAGE", "Failed to send message to test server \"@{{url}}@\"");
fcf.addException("UNITEST_TIMEOUT", "Test timed out");
function toString(a_value){
return typeof a_value == "number" && isNaN(a_value) ? "NaN" :
a_value === undefined ? "undefined" :
a_value === false ? "false" :
a_value === true ? "true" :
a_value === null ? "null" :
fcf.str(a_value);
}
/// @class fcf.NUnitest.Unitest
/// @brief The class that performs the testing is available through the singleton fcf.NUnitest.unitest
Namespace["Unitest"] = class {
constructor(_a_isNotRoot) {
this._isRoot = !_a_isNotRoot;
this._tests = _a_isNotRoot ? fcf.NUnitest.unitest._tests : {};
}
/// @method add(string a_part, string a_group, string a_name, function a_cbtest)
/// @method add(string a_group, string a_name, function a_cbtest)
/// @method add(string a_name, function a_cbtest)
/// @brief Adds a test, it is recommended to use the fcf.test() function,
/// which will load the fcf-framework-unitest:unitest.js executable JS module if necessary
/// @param string a_part = "default" - Test part name. The testing section
/// allows you to separate tests, for example,
/// into basic ones, which do not require the application to be launched,
/// and application tests, which require the entire application to be launched.
/// @param string a_group = "default" - Test group name.
/// @param string a_test - Test name.
/// @param function a_cbtest - test function
/// - Function signature:
/// <async> void a_cbtest(fcf.NUnitest.Unitest a_unitest)
/// - fcf.NUnitest.Unitest a_unitest - the test instance that is created by singleton
/// when the run() method is invoked
/// @example Example of adding a test
///
/// :tests/replaceAll.js file:
///
/// fcf.test("strings", "Function replaceAll", (a_unitest)=>{
/// a_unitest.equal(fcf.replaceAll("1234", "2", "_"), "1_34");
/// });
///
/// :index.js file:
///
/// let fcf = require("fcf-framework-core");
/// let unitest = require("fcf-framework-unitest");
///
/// unitest.run({
/// include: [":tests/replaceAll.js"],
/// })
/// .catch((a_error)=>{
/// console.error("System error: ", a_error);
/// });
add(a_part, a_group, a_name, a_cbtest) {
if (!this._isRoot){
fcf.NUnitest.unitest.add.apply(fcf.NUnitest.unitest, arguments);
return;
}
if (typeof a_name === "function"){
a_cbtest = a_name;
a_name = a_group;
a_group = a_part;
a_part = "default";
} else if (typeof a_group === "function"){
a_cbtest = a_group;
a_name = a_part;
a_group = "default";
a_part = "default";
} else if (typeof a_part === "function"){
a_cbtest = a_part;
a_name = fcf.uuid();
a_group = "default";
a_part = "default";
}
fcf.prepare(this._tests, [a_part, a_group]);
this._tests[a_part][a_group][a_name] = a_cbtest;
}
/// @method fcf.Actions->object run(object a_options)
/// @brief Performs both server-side and browser-side testing
/// @param object a_options - Test execution options
/// - Object items:
/// - [string]|string parts = undefined - Test part name. The testing section
/// allows you to separate tests, for example,
/// into basic ones, which do not require the application to be launched,
/// and application tests, which require the entire application to be launched.
/// - [string]|string groups = undefined - Test group name.
/// - [string]|string tests = undefined - Test name.
/// - boolean enableWebTests = true - Flag allowing execution of tests on the browser side
/// - boolean enableLocalTests = true - Flag to allow running tests on the nodejs server side.
/// Browser-side testing will only be performed if the webTestingPages
/// array is not empty and enableLocalTests is true.
/// - [string] include = [] - An array of included JS files during testing.
/// File paths are specified in FCF path notation. see fcf.getPath() function description
/// - boolean quiet = false - If it is true, then the output to the console about the testing is not performed
/// - integer timeout = 30000 - Single test execution timeout in milliseconds.
/// - [object] processes = [] - An array with a description of the processes that will be launched before testing.
/// When testing is complete, the processes are terminated.
/// - Object items:
/// - string|array command - start command, can be given as a single line or
/// an array of command line start arguments.
/// - integer startTimeout = 1000 - Time in milliseconds to wait for the application to start
/// - [object] webProcesses = [] - An array describing the processes that will be launched before testing on the
/// browser side. Used to launch WEB services. Runs only if enableWebTests is true and
/// the webTestingPages array is not empty. When testing is complete, the processes are terminated.
/// - Object items:
/// - string|array command - start command, can be given as a single line or
/// an array of command line start arguments.
/// - integer startTimeout = 1000 - Time in milliseconds to wait for the application to start
/// - [string|[string]] webBrowsers = [["google-chrome", "chrome", "chromium", "firefox", "opera", "safari"]] -
/// An array with a list of browsers for which testing will be performed.
/// If the array element is an array, then the test will run for the first browser
/// in the nested array that is successfully run.
/// - [object] webTestingPages = [] - An array with information about the pages for which testing will be performed on the browser side
/// - Object items:
/// - string url - URL of the page being tested
/// - [string] include = [] - Array of included URL JS files on the browser side where tests can be located
/// - integer webTestingPort = 4589 - TCP port for transmitting internal testing commands on the browser side
/// - function onMessage = undefined - Callback event of information output to the console
/// - Function signature:
/// onMessage(object a_info)
/// - object a_info - An object with information about output to the console
/// - string part - The name of the test part that is currently running
/// - string group - The name of the test group that is currently running
/// - string test - The name of the test currently running
/// - string level - Logging level (test|crash|error|warning|log|info|debug|trace)
/// - string source - The name of the module from which the logging was performed
/// - string executor - Test executor. If the test was run on the server,
/// then the value is equal to "server",
/// if the test was performed on the browser side,
/// then the value is equal to the name of the browser process
/// - Date time - log time
/// - string message - Displayed message
/// @result fcf.Actions->object - Returns an object with information about testing in a delayed action (similar to Promise)
/// - Object items:
/// - integer errorCount - Number of tests executed with errors
/// - integer testCount - Number of tests performed
/// - integer successfulCount - Number of tests completed successfully
/// - [object] tests - Array with information about executed tests
/// - Object items:
/// - string part - The name of the test part
/// - string group - The name of the test group
/// - string test - The name of the test
/// - string executor - Test executor. If the test was run on the server,
/// then the value is equal to "server",
/// if the test was performed on the browser side,
/// then the value is equal to the name of the browser process
/// - string status - Test state ("error"|"ok")
/// - Error error - Test error object
/// - [object] output - An array with information about the output to the
/// console that was executed during the test execution.
/// For a description of the object elements, see the description of
/// the onMessage parameter
/// - [object] output - An array with information about output to the console.
/// For a description of the object elements,
/// see the description of the onMessage parameter.
run(a_options) {
return this._run(a_options);
}
/// @method compare(string a_operation, a_left, a_right, boolean a_strict = false)
/// @brief Compares two values against the comparison operation a_operation
/// and if the comparison is not correct, it causes a test error
/// @param string a_operation - Comparison operation
/// - Accepted values:
/// - "=" | "==" - Comparison operation
/// - "!=" - Inequality operation
/// - "<" - Operation less
/// - "<=" - Operation less than or equal to
/// - ">" - Operation more
/// - ">=" - Operation greater than or equal to
/// @param mixed a_left - First comparison value
/// @param mixed a_right - Second compared value
/// @param boolean a_strict = false - If true, then strict comparison is used (===)
compare(a_operation, a_left, a_right, a_strict) {
let c = fcf.compare(a_left, a_right, a_strict);
if (a_operation == "!=") {
if (c == 0) {
this._task.currentTestInfo.error = new fcf.Exception("UNITEST_NOT_EQUAL", {left: toString(a_left), right: toString(a_right)});
throw this._task.currentTestInfo.error;
}
} else if (a_operation == "=" || a_operation == "==") {
if (c != 0) {
let error = new fcf.Exception("UNITEST_EQUAL", {left: toString(a_left), right: toString(a_right)});
if (this._task && this._task.currentTestInfo)
this._task.currentTestInfo.error = error
throw error;
}
} else if (a_operation == "<") {
if (c >= 0) {
let error = new fcf.Exception("UNITEST_LESS", {left: toString(a_left), right: toString(a_right)});
if (this._task && this._task.currentTestInfo)
this._task.currentTestInfo.error = error
throw error;
}
} else if (a_operation == "<=") {
if (c > 0) {
let error = new fcf.Exception("UNITEST_LESS_EQUAL", {left: toString(a_left), right: toString(a_right)});
if (this._task && this._task.currentTestInfo)
this._task.currentTestInfo.error = error
throw error;
}
} else if (a_operation == ">") {
if (c <= 0) {
let error = new fcf.Exception("UNITEST_GREATER", {left: toString(a_left), right: toString(a_right)});
if (this._task && this._task.currentTestInfo)
this._task.currentTestInfo.error = error
throw error;
}
} else if (a_operation == ">=") {
if (c < 0) {
let error = new fcf.Exception("UNITEST_GREATER_EQUAL", {left: toString(a_left), right: toString(a_right)});
if (this._task && this._task.currentTestInfo)
this._task.currentTestInfo.error = error
throw error;
}
}
}
/// @method error(Error|string a_error)
/// @brief Sets a test error and exit from test
/// Called on the fcf.NUnitest.unitest object passed through the fcf.test() test callback argument
/// @param Error|string a_error - Error information
error(a_error) {
let error = new fcf.Exception("UNITEST_ERROR", {error: a_error instanceof Error ? "Test execution error" : fcf.str(a_error)}, a_error instanceof Error ? a_error : undefined);
if (this._task && this._task.currentTestInfo)
this._task.currentTestInfo.error = error;
throw error;
}
/// @method equal(mixed a_left, mixed a_right, boolean a_strict = false)
/// @brief Compares two values and if they are not equal raises a test error
/// @param mixed a_left - First comparison value
/// @param mixed a_right - Second compared value
/// @param boolean a_strict = false - If true, then strict comparison is used (===)
equal(a_left, a_right, a_strict) {
return this.compare("==", a_left, a_right, a_strict);
}
/// @method notEqual(mixed a_left, mixed a_right, boolean a_strict = false)
/// @brief Compares two values and if they are equal raises a test error
/// @param mixed a_left - First comparison value
/// @param mixed a_right - Second compared value
/// @param boolean a_strict = false - If true, then strict comparison is used
notEqual(a_left, a_right, a_strict) {
return this.compare("!=", a_left, a_right, a_strict);
}
/// @method less(mixed a_left, mixed a_right)
/// @brief Compares two values and if the first value is greater or equal to the second than, throws a test error.
/// @param mixed a_left - First comparison value
/// @param mixed a_right - Second compared value
less(a_left, a_right) {
return this.compare("<", a_left, a_right);
}
/// @method lessEqual(mixed a_left, mixed a_right)
/// @brief Compares two values, and if the first value is not less or equal to the second than, throws a test error.
/// @param mixed a_left - First comparison value
/// @param mixed a_right - Second compared value
lessEqual(a_left, a_right) {
return this.compare("<=", a_left, a_right);
}
/// @method greater(mixed a_left, mixed a_right)
/// @brief Compares two values and if the first value is not greater than throws a test error.
/// @param mixed a_left - First comparison value
/// @param mixed a_right - Second compared value
greater(a_left, a_right) {
return this.compare(">", a_left, a_right);
}
/// @method greaterEqual(mixed a_left, mixed a_right)
/// @brief Compares two values and if the first value is not greater or not equal to the second then, throws a test error.
/// @param mixed a_left - First comparison value
/// @param mixed a_right - Second compared value
greaterEqual(a_left, a_right) {
return this.compare(">=", a_left, a_right);
}
runAutoTests(a_options) {
let route = new fcf.RouteInfo(window.location.href);
let host = route.args.___fcf_unitest_host || "localhost";
let port = route.args.___fcf_unitest_port;
let browserTestId = route.args.___fcf_unitest_id;
let tests = Array.isArray(route.args.___fcf_unitest_tests) ? route.args.___fcf_unitest_tests : undefined;
let groups = Array.isArray(route.args.___fcf_unitest_groups) ? route.args.___fcf_unitest_groups : undefined;
let parts = Array.isArray(route.args.___fcf_unitest_parts) ? route.args.___fcf_unitest_parts : undefined;
let step = !isNaN(route.args.___fcf_unitest_step) ? parseInt(route.args.___fcf_unitest_step) : 0;
let include = route.args.___fcf_unitest_include ? route.args.___fcf_unitest_include : [];
let wait = !isNaN(parseInt(route.args.___fcf_unitest_wait)) ? parseInt(route.args.___fcf_unitest_wait) :
route.args.___fcf_unitest_wait;
return fcf.actions()
.then(()=>{
return this._run(fcf.append(
{},
a_options,
{
id: browserTestId,
parts: parts,
groups: groups,
tests: tests,
include: include
}
),
{
host: host,
port: port,
},
wait
);
})
.then(async (a_result)=>{
if(!host || !port)
return;
let url = `http://${host}:${port}/getstep`;
try {
++step;
let data = {
step: step,
id: browserTestId,
};
let res = await fetch(url,
{
method: "POST",
mode: "cors",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data)
});
let resData = await res.json();
if (resData.url){
let route = new fcf.RouteInfo(resData.url, "root");
route.urlArgs.___fcf_unitest = undefined;
route.urlArgs.___fcf_unitest_host = host;
route.urlArgs.___fcf_unitest_port = port;
route.urlArgs.___fcf_unitest_step = step;
route.urlArgs.___fcf_unitest_groups = groups;
route.urlArgs.___fcf_unitest_parts = parts;
route.urlArgs.___fcf_unitest_tests = tests;
route.urlArgs.___fcf_unitest_id = browserTestId;
route.urlArgs.___fcf_unitest_include = Array.isArray(resData.include) ? resData.include : [];
window.location.href = fcf.buildUrl(route);
}
} catch(e) {
throw new fcf.Exception("UNITEST_FAILD_SEND_MESSAGE", {url: url}, e);
}
});
}
_run(a_options, a_autoTestOptions, a_wait) {
if (this._isRoot) {
return fcf.actions()
.then(()=>{
let ut = new Namespace["Unitest"](true);
return ut._run(a_options, a_autoTestOptions, a_wait);
});
}
let self = this;
a_options = fcf.append(
{
enableWebTests: true,
enableLocalTests: true,
processes: [],
timeout: 30000,
webTestingPort: 4589,
webBrowsers: [["google-chrome", "chrome", "chromium", "firefox", "opera", "safari"]],
webProcesses: [],
webTestingPages: []
},
a_options);
this._task = {
options: a_options,
result: {
errorCount: 0,
testCount: 0,
successfulCount: 0,
tests: [],
output: [],
},
currentTest: undefined,
currentPart: undefined,
currentGroup: undefined,
selfOutput: false,
originLog: undefined,
originError: undefined,
originWarn: undefined,
outputActions: fcf.actions(),
pushOutput: function (a_executor, a_level, a_source, a_exdata, a_arguments) {
let message = {
unitest: self,
part: this.currentPart,
group: this.currentGroup,
test: this.currentTest,
level: a_level,
source: a_source,
executor: a_executor,
time: new Date(),
message: fcf.trim(fcf.append([], a_arguments).map((val)=>{ return fcf.str(val) }).join(" "), ["\r", "\n"])
};
if (this.currentTestInfo) {
this.currentTestInfo.output.push(message);
}
if (this.options.onMessage){
this.options.onMessage(message);
}
fcf.getEventChannel().send("unitest_message", message)
this.result.output.push(message);
if (a_autoTestOptions && a_autoTestOptions.host && a_autoTestOptions.port) {
this.outputActions.then(async ()=>{
let url = `http://${a_autoTestOptions.host}:${a_autoTestOptions.port}/message`;
try {
let data = fcf.append({}, message, a_exdata);
delete data.unitest;
data.id = this.options.id;
let res = await fetch(url,
{
method: "PUT",
mode: "cors",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data)
});
} catch(e) {
throw new fcf.Exception("UNITEST_FAILD_SEND_MESSAGE", {url: url}, e);
}
})
}
}
};
this._task.outputActions
.catch((a_error)=>{
this._err(a_error);
});
let processes = [];
let partsNames;
let groupsNames;
let testsNames;
return fcf.actions()
.then(async ()=>{
let include = this._task.options.include;
if (include && typeof include == "string"){
include = [include];
}
if (Array.isArray(include)){
await fcf.require(include);
}
partsNames = this._quantityToOptionArray(this._task.options.parts);
groupsNames = this._quantityToOptionArray(this._task.options.groups);
testsNames = this._quantityToOptionArray(this._task.options.tests);
})
.then(()=>{
if (typeof a_wait == "string") {
let func = fcf.resolve(window, a_wait);
return func();
} else if (typeof a_wait == "number") {
return fcf.actions()
.then((a_res, a_act)=>{
setTimeout(()=>{
a_act.complete();
}, a_wait);
});
}
})
.then(async ()=>{
await this._startTests();
await this._logTestsStart(partsNames, groupsNames, testsNames);
})
.each(this._task.options.processes, (a_key, a_processInfo, a_res, a_act)=>{
if (!this._isSelected(a_processInfo.tests, testsNames)){
a_act.complete();
return;
}
if (!this._isSelected(a_processInfo.groups, groupsNames)){
a_act.complete();
return;
}
if (!this._isSelected(a_processInfo.parts, partsNames)){
a_act.complete();
return;
}
let libChildProcess = require("child_process");
let libFCFProcess = require("fcf-framework-core/NProcess/NProcess.js");
let endTimer;
let commandInfo = libFCFProcess.commandFromString(a_processInfo.command);
let process = libChildProcess.spawn(commandInfo.command, commandInfo.args);
processes.push(process);
this._log(`Child process "${a_processInfo.command}" started with pid ${process.pid}`);
process.stdout.on("data", (a_data) => {
this._log(`Sub process "${commandInfo.command}" stdout:`, fcf.Logger.offset(4, ` [${commandInfo.command} PID: ${process.pid}]> `), "\n" , a_data.toString());
});
process.stderr.on("data", (a_data) => {
this._err(`Sub process "${commandInfo.command}" stderr:`, fcf.Logger.offset(4, ` [PID: ${process.pid}]> `), "\n" , a_data.toString());
});
process.on("error", (a_error) => {
clearTimeout(endTimer);
a_act.error(new fcf.Exception("UNITEST_ERROR_EXEC_COMMAND", {process: a_processInfo.command}));
});
process.on("exit", (a_exitCode) => {
clearTimeout(endTimer);
if (a_exitCode) {
a_act.error(new fcf.Exception("UNITEST_ERROR_EXEC_COMMAND", {process: a_processInfo.command}));
} else {
a_act.complete();
}
});
processes.push(process);
endTimer = setTimeout(() => {
a_act.complete();
}, a_processInfo.startTimeout || 1000);
})
.then(()=>{
if (this._task.options.enableLocalTests)
return this._runLocalTests(partsNames, groupsNames, testsNames);
})
.then(()=>{
if (this._task.options.enableWebTests && !fcf.empty(this._task.options.webTestingPages))
return this._runWebTests(partsNames, groupsNames, testsNames);
})
.then(()=>{
return this._logTestsEnd();
})
.finally(()=> {
return fcf.actions()
.then((a_res, a_act)=>{
this._task.outputActions.finally(()=>{ a_act.complete(); });
})
.then(()=>{
return this._endTests();
})
})
.finally(()=>{
for(let process of processes) {
try {
process.kill();
} catch(e){
}
}
})
.then(()=>{
return this._task.result;
});
}
async _runLocalTests(a_partsNames, a_groupsNames, a_testsNames){
let tests = [];
for(let part in this._tests) {
if (a_partsNames && a_partsNames.indexOf(part) == -1)
continue;
for(let group in this._tests[part]) {
if (a_groupsNames && a_groupsNames.indexOf(group) == -1)
continue;
for(let name in this._tests[part][group]) {
if (a_testsNames && a_testsNames.indexOf(name) == -1)
continue;
tests.push({
part: part,
group: group,
name: name,
test: this._tests[part][group][name]
});
}
}
}
if (tests.length) {
this._log("");
this._log("Start local tests...");
this._log("--------------------");
}
for(let test of tests) {
this._task.currentPart = test.part;
this._task.currentGroup = test.group;
this._task.currentTest = test.name;
this._task.currentTestInfo = {
part: test.part,
group: test.group,
test: test.name,
executor: fcf.isServer() ? "server" : "browser",
status: undefined,
error: undefined,
output: [],
};
this._logEx({command: "start_test" },
`Test [${this._task.currentPart}][${this._task.currentGroup}][${this._task.currentTest}] running ...`);
let timer;
try {
await fcf.actions()
.then(async (a_res, a_act)=>{
timer = setTimeout(() => {
a_act.error(new fcf.Exception("UNITEST_TIMEOUT"));
}, this._task.options.timeout);
await test.test(this);
a_act.complete();
});
if (this._task.currentTestInfo.error) {
throw this._task.currentTestInfo.error;
}
this._task.currentTestInfo.status = "ok";
this._logEx({command: "end_test", error: undefined } ,
`Test [${this._task.currentPart}][${this._task.currentGroup}][${this._task.currentTest}] is completed.`);
} catch(e) {
this._task.currentTestInfo.status = "error";
this._task.currentTestInfo.error = this._getErrorInfo(e);
this._errEx(
{command: "end_test", error: e },
`Test [${this._task.currentPart}][${this._task.currentGroup}][${this._task.currentTest}] is failed. \n`+
`Position: ${this._task.currentTestInfo.error.file}:${this._task.currentTestInfo.error.line}\n` +
`Error: `, fcf.str(this._task.currentTestInfo.error.error, true));
}
clearTimeout(timer);
this._task.result.tests.push(this._task.currentTestInfo);
if (this._task.currentTestInfo.status == "error") {
++this._task.result.errorCount;
} else {
++this._task.result.successfulCount;
}
++this._task.result.testCount;
this._task.currentTestInfo = undefined;
}
this._task.currentPart = undefined;
this._task.currentGroup = undefined;
this._task.currentTest = undefined;
this._task.currentTestInfo = undefined;
}
async _runWebTests(a_partsNames, a_groupsNames, a_testsNames){
if (fcf.empty(this._task.options.webTestingPages) || !fcf.isServer())
return;
let libChildProcess = require("child_process");
let libFCFProcess = require("fcf-framework-core/NProcess/NProcess.js");
let BackServer = require("fcf-framework-unitest/NDetails/BackServer.js");
let backServer;
let processes = [];
return fcf.actions()
.then(()=>{
this._log("");
this._log("Start web tests...");
this._log("--------------------");
})
// running sub processes
.each(this._task.options.webProcesses, async (k, cmdInfo, a_res, a_act)=>{
if (!this._isSelected(cmdInfo.tests, a_testsNames)){
a_act.complete();
return;
}
if (!this._isSelected(cmdInfo.groups, a_groupsNames)){
a_act.complete();
return;
}
if (!this._isSelected(cmdInfo.parts, a_partsNames)){
a_act.complete();
return;
}
let endTimer;
let process;
if (fcf.empty(cmdInfo.command) || (!Array.isArray(cmdInfo.command) && typeof cmdInfo.command !== "string")){
throw new fcf.Exception("UNUTEST_WEB_COMMAND_UNSET", {});
}
let cmdStr = Array.isArray(cmdInfo.command) ? libFCFProcess.commandToString(cmdInfo.command)
: cmdInfo.command;
let cmdProc;
let cmdArgs;
if (Array.isArray(cmdInfo.command)) {
cmdArgs = fcf.clone(cmdInfo.command);
cmdProc = cmdArgs.shift();
} else {
let info = libFCFProcess.commandFromString(cmdInfo.command);
cmdProc = info.command;
cmdArgs = info.args;
}
this._log(`Start process: ${cmdStr}`);
process = libChildProcess.spawn(cmdProc, cmdArgs);
processes.push(process);
this._log(`Child process "${cmdProc}" started with pid ${process.pid}`);
process.stdout.on("data", (a_data) => {
this._log(`Sub process "${cmdProc}" stdout:`, fcf.Logger.offset(4, ` [${cmdProc} PID: ${process.pid}]> `), "\n" , a_data.toString());
});
process.stderr.on("data", (a_data) => {
this._err(`Sub process "${cmdProc}" stderr:`, fcf.Logger.offset(4, ` [PID: ${process.pid}]> `), "\n" , a_data.toString());
});
process.on("error", (a_error) => {
clearTimeout(endTimer);
a_act.error(new fcf.Exception("UNITEST_ERROR_EXEC_COMMAND", {process: cmdStr}));
});
process.on("exit", (a_exitCode) => {
clearTimeout(endTimer);
if (a_exitCode) {
a_act.error(new fcf.Exception("UNITEST_ERROR_EXEC_COMMAND", {process: cmdStr}));
}
});
endTimer = setTimeout(()=>{
a_act.complete();
}, !isNaN(cmdInfo.startTimeout) ? cmdInfo.startTimeout : 1000)
})
.then(()=>{
this._log(`Start back server on port ${this._task.options.webTestingPort}`);
backServer = new BackServer(this._task.options.webTestingPort, this._task.options.webTestingPages);
return backServer.run();
})
// running tests
.each(this._task.options.webBrowsers, async (a_key, a_browsers)=>{
a_browsers = Array.isArray(a_browsers) ? a_browsers : [a_browsers];
let lastError;
let lastCommand;
let complete;
for(let i = 0; i < a_browsers.length; ++i) {
let browserTestId = fcf.id();
lastError = undefined;
try {
let process;
let timer;
await fcf.actions()
.then((a_res, a_act)=>{
lastCommand = a_browsers[i];
if (lastCommand.indexOf("{{") == -1){
lastCommand += " \"@{{url}}@\"";
}
let include = Array.isArray(this._task.options.webTestingPages[0].include) ? this._task.options.webTestingPages[0].include :
typeof this._task.options.webTestingPages[0].include == "string" ? [this._task.options.webTestingPages[0].include] :
[];
let url = this._task.options.webTestingPages[0].url;
url += url.indexOf("?") == -1 ? "?" : "&";
url += `___fcf_unitest&___fcf_unitest_host=localhost&`+
`___fcf_unitest_port=${this._task.options.webTestingPort}&`+
`___fcf_unitest_tests=${encodeURIComponent(JSON.stringify(this._task.options.tests))}&`+
`___fcf_unitest_groups=${encodeURIComponent(JSON.stringify(this._task.options.groups))}&`+
`___fcf_unitest_parts=${encodeURIComponent(JSON.stringify(this._task.options.parts))}&`+
`___fcf_unitest_id=${browserTestId}&`+
(this._task.options.webTestingPages[0].wait ? `___fcf_unitest_wait=${this._task.options.webTestingPages[0].wait}&`: "") +
`___fcf_unitest_include=${encodeURIComponent(JSON.stringify(include))}`;
lastCommand = fcf.tokenize(lastCommand, {url: url}, {quiet: true});
let commandInfo = libFCFProcess.commandFromString(lastCommand);
backServer.setId(browserTestId);
backServer.onStep((a_stepIndex) => {
if (Array.isArray(this._task.options.webTestingPages) &&
this._task.options.webTestingPages[a_stepIndex] &&
this._task.options.webTestingPages[a_stepIndex].url) {
let step = this._task.options.webTestingPages[a_stepIndex];
let route = new fcf.RouteInfo(this._task.options.webTestingPages[a_stepIndex].url, "root");
route.urlArgs.___fcf_unitest = undefined;
route.urlArgs.___fcf_unitest_host = "localhost";
route.urlArgs.___fcf_unitest_port = this._task.options.webTestingPort;
route.urlArgs.___fcf_unitest_include = Array.isArray(step.include) ? step.include : [];
route.urlArgs.___fcf_unitest_step = a_stepIndex;
route.urlArgs.___fcf_unitest_id = browserTestId;
route.urlArgs.___fcf_unitest_wait = this._task.options.webTestingPages[a_stepIndex].wait;
let url = fcf.buildUrl(route);
this._msg(
commandInfo.command,
{
unitest: this,
part: undefined,
group: undefined,
test: undefined,
level: "log",
source: "UniTest",
executor: commandInfo.command,
time: new Date(),
message: `Go to page ${url}`,
}
);
} else {
complete = true;
if (timer) {
clearTimeout(timer);
}
a_act.complete();
}
});
backServer.onMessage((a_message) => {
if (!a_message.test)
return;
if (a_message.command == "start_test") {
clearTimeout(timer);
timer = setTimeout(()=>{
complete = true;
a_act.error(new Error(`[${a_message.part}][${a_message.group}][${a_message.test}] test timed out`));
}, this._task.options.timeout);
this._task.currentTestInfo = {
part: a_message.part,
group: a_message.group,
test: a_message.test,
executor: commandInfo.command,
status: undefined,
error: undefined,
output: [],
};
} else if (a_message.command == "end_test" && this._task.currentTestInfo) {
clearTimeout(timer);
timer = setTimeout(()=>{
complete = true;
a_act.error(new Error("Browser command timed out"));
}, this._task.options.timeout);
this._task.currentTestInfo.status = a_message.error ? "error" : "ok";
this._task.currentTestInfo.error = a_message.error;
this._task.result.tests.push(this._task.currentTestInfo);
if (this._task.currentTestInfo.status == "error") {
++this._task.result.errorCount;
} else {
++this._task.result.successfulCount;
}
++this._task.result.testCount;
this._task.currentTestInfo = undefined;
}
this._msg(commandInfo.command, a_message);
});
this._log(`Start browser '${lastCommand}' ...`);
process = libChildProcess.spawn(commandInfo.command, commandInfo.args);
process.on("error", (a_error)=>{
clearTimeout(timer);
this._err(`Failed start browser 2'${lastCommand}'`);
a_act.error(new Error(`browser '${lastCommand}' exited with an error.`));
});
clearTimeout(timer);
timer = setTimeout(()=>{
complete = true;
a_act.error(new Error(`Page "${url}" test timed out`));
}, this._task.options.timeout);
})
.finally(()=>{
if (process){
process.kill();
}
if (timer) {
clearTimeout(timer);
timer = undefined;
}
})
.then(()=>{
complete = true;
});
} catch(e) {
lastError = e;
}
if (complete)
break;
}
if (lastError) {
throw new fcf.Exception("UNITEST_FAILD_BROWSER_TEST", {command: lastCommand, error: lastError});
}
})
.finally(()=>{
backServer.stop();
for(let process of processes) {
try {
process.kill();
} catch(e) {
}
}
});
}
async _startTests() {
var self = this;
this._task.originLog = console.log;
console.log = function(){
if (!self._task.selfOutput){
self._task.pushOutput("server", "log", "", {}, arguments);
}
self._task.originLog.apply(console, arguments);
};
this._task.originWarn = console.warn;
console.warn = function(){
if (!self._task.selfOutput){
self._task.pushOutput("server", "warning", "", {}, arguments);
}
self._task.originWarn.apply(console, arguments);
};
this._task.originError = console.error;
console.error = function(){
if (!self._task.selfOutput){
self._task.pushOutput("server", "error", "", {}, arguments);
}
self._task.originError.apply(console, arguments);
};
this._task.logEventHandlerBefore = function(a_info){
if (!self._task.selfOutput){
self._task.selfLoggerOutput = true;
self._task.selfOutput = true;
}
};
fcf.log.on("message_before", this._task.logEventHandlerBefore);
this._task.logEventHandlerAfter = function(a_info){
if (!self._task.selfOutput || self._task.selfLoggerOutput){
self._task.pushOutput("server", a_info.level, a_info.module, {}, a_info.args);
}
if (self._task.selfOutput && self._task.selfLoggerOutput){
self._task.selfLoggerOutput = false;
self._task.selfOutput = false;
}
};
fcf.log.on("message_after", this._task.logEventHandlerAfter);
}
_endTests() {
if (this._task.originLog)
console.log = this._task.originLog;
if (this._task.originWarn)
console.warn = this._task.originWarn;
if (this._task.originError)
console.error = this._task.originError;
fcf.log.detach(this._task.logEventHandlerBefore);
fcf.log.detach(this._task.logEventHandlerAfter);
}
_logTestsStart(a_parts, a_groups, a_tests) {
this._log("====================================================")
this._log(`Start testing${fcf.isServer() ? " on the server side..." : ""}`)
this._log(`Parts: ${a_parts ? a_parts.join("; ") : "*"}`);
this._log(`Groups: ${a_groups ? a_groups.join("; ") : "*"}`);