@@ -28,12 +28,19 @@ const FutureEvent = require('future-event');
28
28
const glob = require ( 'glob' ) ;
29
29
const path = require ( 'path' ) ;
30
30
31
+ /**
32
+ * All test files in the test.com.google.javascript.jscomp.runtime_tests.build
33
+ * directory.
34
+ */
31
35
const TEST_FILES = glob . sync ( path . resolve (
32
36
__dirname ,
33
37
'../../test/com/google/javascript/jscomp/'
34
38
+ 'runtime_tests/**/build/*_test.html' ,
35
39
) ) ;
36
40
41
+ /**
42
+ * Iterate over all found test files and execute them in JSDOM.
43
+ */
37
44
describe ( 'Runtime tests' , ( ) => {
38
45
for ( const testFile of TEST_FILES ) {
39
46
const logs = [ ] ;
@@ -42,31 +49,47 @@ describe('Runtime tests', () => {
42
49
43
50
const allLogs = ( ) => logs . join ( '\n' ) ;
44
51
const chalkMsg = ( msg ) => {
52
+ /**
53
+ * Check whether or not this message is a PASSED or FAILED message.
54
+ */
45
55
const isPass = passed . test ( msg ) ;
46
56
const isFail = failed . test ( msg ) ;
47
57
48
- if ( isPass || isFail ) {
49
- return msg . replace (
50
- passed , chalk . green ( 'PASSED' ) ,
51
- ) . replace (
52
- failed , chalk . red ( 'FAILED' ) ,
53
- ) ;
54
- } else return msg ;
58
+ /**
59
+ * Highlight PASSED and FAILED in messages to help with accessibility.
60
+ */
61
+ return ( isPass || isFail )
62
+ ? msg
63
+ . replace ( passed , chalk . green ( 'PASSED' ) )
64
+ . replace ( failed , chalk . red ( 'FAILED' ) )
65
+ : msg ;
55
66
} ;
56
67
57
- // file.ext -> file
68
+ /**
69
+ * Get filename, i.e. /path/to/file.ext -> file.ext
70
+ */
58
71
const testName = path . basename ( testFile ) ;
59
- // A promise that will resolve when JSDOM is done executing.
72
+
73
+ /**
74
+ * A promise that will resolve when JSDOM is done executing.
75
+ */
60
76
const testIsFinished = new FutureEvent ( ) ;
61
- // A virtual console which will receive messages from JSDOM's `console.log`.
77
+
78
+ /**
79
+ * A virtual console which will receive messages from JSDOM's
80
+ * `console.log`.
81
+ */
62
82
const virtualConsole = new VirtualConsole ( )
63
83
. on ( 'log' , ( msg ) => {
64
84
logs . push ( chalkMsg ( msg ) ) ;
65
85
if ( / T e s t s c o m p l e t e / i. test ( msg ) ) testIsFinished . ready ( allLogs ( ) ) ;
66
86
else if ( / T e s t s f a i l e d / i. test ( msg ) ) testIsFinished . cancel ( allLogs ( ) ) ;
67
87
} ) ;
68
88
69
- // Load the generated test file for consumption by the JSDOM environment.
89
+ /**
90
+ * Load the generated test file for consumption by the JSDOM environment.
91
+ * This will be a raw HTML document.
92
+ */
70
93
const testDocument = fs . readFileSync (
71
94
path . resolve (
72
95
__dirname ,
@@ -81,8 +104,9 @@ describe('Runtime tests', () => {
81
104
/**
82
105
* This does not actually run a server of any kind, it only informs the
83
106
* DOM what to put in `window.location.origin`. By default, this is
84
- * `null`, and any non-HTTPS URL field here will throw an error in the
85
- * test suite due to unsafe URL.
107
+ * `null`, which throws an "unsafe URL" error in the test suite. This is
108
+ * purely for accurately mocking a browser for `goog.testing.testsuite`
109
+ * tests, and any valid HTTPS URL will work here.
86
110
*/
87
111
url : 'https://localhost:42' ,
88
112
/**
@@ -98,13 +122,19 @@ describe('Runtime tests', () => {
98
122
} ) ;
99
123
100
124
try {
101
- // Wait for test to finish.
125
+ /**
126
+ * Wait for test to finish, resume if no errors thrown.
127
+ */
102
128
await testIsFinished ;
103
129
} catch ( e ) {
104
- // If there was an error, print it.
130
+ /**
131
+ * Print error and fail if any occurred.
132
+ */
105
133
fail ( `Failed test in suite ${ testName } : \n${ e } \n` ) ;
106
134
}
107
- // Otherwise, everything passed.
135
+ /**
136
+ * Otherwise, everything passed.
137
+ */
108
138
console . log ( `Passed all tests in suite ${ testName } ` ) ;
109
139
} ) ;
110
140
}
0 commit comments