1
1
/* eslint-disable no-console */
2
+ import type { Expand } from '@glimmer/interfaces' ;
2
3
import { debug } from '@glimmer/validator' ;
3
4
import { autoRegister } from 'js-reporters' ;
5
+ import { default as QUnit } from 'qunit' ;
4
6
5
7
export async function setupQunit ( ) {
6
- const qunit = await import ( 'qunit' ) ;
8
+ const qunitLib : QUnit = await import ( 'qunit' ) ;
7
9
await import ( 'qunit/qunit/qunit.css' ) ;
8
10
11
+ const testing = Testing . withConfig (
12
+ {
13
+ id : 'smoke_tests' ,
14
+ label : 'Smoke Tests' ,
15
+ tooltip : 'Enable Smoke Tests' ,
16
+ } ,
17
+ {
18
+ id : 'ci' ,
19
+ label : 'CI Mode' ,
20
+ tooltip :
21
+ 'CI mode emits tap output and makes tests run faster by sacrificing UI responsiveness' ,
22
+ } ,
23
+ {
24
+ id : 'enable_internals_logging' ,
25
+ label : 'Log Deep Internals' ,
26
+ tooltip : 'Logs internals that are used in the development of the trace logs' ,
27
+ } ,
28
+
29
+ {
30
+ id : 'enable_trace_logging' ,
31
+ label : 'Trace Logs' ,
32
+ tooltip : 'Trace logs emit information about the internal VM state' ,
33
+ } ,
34
+
35
+ {
36
+ id : 'enable_subtle_logging' ,
37
+ label : '+ Subtle' ,
38
+ tooltip :
39
+ 'Subtle logs include unchanged information and other details not necessary for normal debugging' ,
40
+ } ,
41
+
42
+ {
43
+ id : 'enable_trace_explanations' ,
44
+ label : '+ Explanations' ,
45
+ tooltip : 'Also explain the trace logs' ,
46
+ }
47
+ ) ;
48
+
9
49
const runner = autoRegister ( ) ;
10
- // @ts -expect-error qunit types don't expose "reporters"
11
- const tap = qunit . reporters . tap ;
12
- tap . init ( runner , { log : console . info } ) ;
13
-
14
- QUnit . config . urlConfig . push ( {
15
- id : 'smoke_tests' ,
16
- label : 'Enable Smoke Tests' ,
17
- tooltip : 'Enable Smoke Tests' ,
18
- } ) ;
19
50
20
- QUnit . config . urlConfig . push ( {
21
- id : 'ci' ,
22
- label : 'Enable CI Mode' ,
23
- tooltip : 'CI mode makes tests run faster by sacrificing UI responsiveness' ,
51
+ testing . begin ( ( ) => {
52
+ if ( testing . config . ci ) {
53
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
54
+ // @ts -ignore
55
+ const tap = qunitLib . reporters . tap ;
56
+ tap . init ( runner , { log : console . info } ) ;
57
+ }
24
58
} ) ;
25
59
26
60
await Promise . resolve ( ) ;
@@ -34,7 +68,7 @@ export async function setupQunit() {
34
68
35
69
console . log ( `[HARNESS] ci=${ hasFlag ( 'ci' ) } ` ) ;
36
70
37
- QUnit . testStart ( ( ) => {
71
+ testing . testStart ( ( ) => {
38
72
debug . resetTrackingTransaction ?.( ) ;
39
73
} ) ;
40
74
@@ -52,44 +86,94 @@ export async function setupQunit() {
52
86
} ) ;
53
87
54
88
let start = performance . now ( ) ;
55
- qunit . testDone ( async ( ) => {
89
+ qunitLib . testDone ( async ( ) => {
56
90
let gap = performance . now ( ) - start ;
57
91
if ( gap > 200 ) {
58
92
await pause ( ) ;
59
93
start = performance . now ( ) ;
60
94
}
61
95
} ) ;
62
96
63
- qunit . moduleDone ( pause ) ;
97
+ qunitLib . moduleDone ( pause ) ;
64
98
}
65
99
66
- // @ts -expect-error missing in types, does exist: https://api.qunitjs.com/callbacks/QUnit.on/#the-testend-event
67
- QUnit . on ( 'testEnd' , ( testEnd ) => {
68
- if ( testEnd . status === 'failed' ) {
69
- testEnd . errors . forEach ( ( assertion : any ) => {
70
- console . error ( assertion . stack ) ;
71
- // message: speedometer
72
- // actual: 75
73
- // expected: 88
74
- // stack: at dmc.test.js:12
75
- } ) ;
76
- }
77
- } ) ;
78
-
79
- qunit . done ( ( { failed } ) => {
80
- if ( failed > 0 ) {
81
- console . log ( '[HARNESS] fail' ) ;
82
- } else {
83
- console . log ( '[HARNESS] done' ) ;
84
- }
100
+ qunitLib . done ( ( ) => {
101
+ console . log ( '[HARNESS] done' ) ;
85
102
} ) ;
86
103
87
104
return {
88
105
smokeTest : hasFlag ( 'smoke_test' ) ,
89
106
} ;
90
107
}
91
108
109
+ class Testing < Q extends typeof QUnit > {
110
+ static withConfig < const C extends readonly UrlConfig [ ] > ( ...configs : C ) : Testing < WithConfig < C > > {
111
+ return new Testing ( withConfig ( ...configs ) ) ;
112
+ }
113
+
114
+ readonly #qunit: Q ;
115
+
116
+ constructor ( qunit : Q ) {
117
+ this . #qunit = qunit ;
118
+ }
119
+
120
+ get config ( ) : Q [ 'config' ] {
121
+ return this . #qunit. config ;
122
+ }
123
+
124
+ readonly begin = ( begin : ( details : QUnit . BeginDetails ) => void | Promise < void > ) : void => {
125
+ this . #qunit. begin ( begin ) ;
126
+ } ;
127
+
128
+ readonly testStart = (
129
+ callback : ( details : QUnit . TestStartDetails ) => void | Promise < void >
130
+ ) : void => {
131
+ this . #qunit. testStart ( callback ) ;
132
+ } ;
133
+ }
134
+
92
135
function hasFlag ( flag : string ) : boolean {
136
+ return hasSpecificFlag ( flag ) ;
137
+ }
138
+
139
+ function hasSpecificFlag ( flag : string ) : boolean {
93
140
let location = typeof window !== 'undefined' && window . location ;
94
141
return location && new RegExp ( `[?&]${ flag } ` ) . test ( location . search ) ;
95
142
}
143
+
144
+ // eslint-disable-next-line unused-imports/no-unused-vars
145
+ function getSpecificFlag ( flag : string ) : string | undefined {
146
+ let location = typeof window !== 'undefined' && window . location ;
147
+ if ( ! location ) {
148
+ return undefined ;
149
+ }
150
+
151
+ const matches = new RegExp ( `[?&]${ flag } =([^&]*)` ) . exec ( location . search ) ;
152
+ return matches ? matches [ 1 ] : undefined ;
153
+ }
154
+
155
+ interface UrlConfig {
156
+ id : string ;
157
+ label ?: string | undefined ;
158
+ tooltip ?: string | undefined ;
159
+ value ?: string | string [ ] | { [ key : string ] : string } | undefined ;
160
+ }
161
+
162
+ type WithConfig < C extends readonly UrlConfig [ ] > = typeof QUnit & {
163
+ config : QUnit [ 'config' ] & {
164
+ [ P in C [ number ] [ 'id' ] ] : string | undefined ;
165
+ } ;
166
+ } ;
167
+
168
+ function withConfig < const C extends readonly UrlConfig [ ] > ( ...configs : C ) : Expand < WithConfig < C > > {
169
+ for ( let config of configs ) {
170
+ QUnit . config . urlConfig . push ( config ) ;
171
+ }
172
+
173
+ const index = QUnit . config . urlConfig . findIndex ( ( c ) => c . id === 'noglobals' ) ;
174
+ if ( index !== - 1 ) {
175
+ QUnit . config . urlConfig . splice ( index , 1 ) ;
176
+ }
177
+
178
+ return QUnit as any ;
179
+ }
0 commit comments