Skip to content

Commit e295106

Browse files
authored
Tests: Make CLI test cases easier to debug with normalised stack
Remove use of regexes and instead strictly compare exact strings, but normalise two things: * collapse any Node internals into a single frame called "internal". * strip line/column numbers of our own build artefact.
1 parent eeaa482 commit e295106

File tree

4 files changed

+190
-186
lines changed

4 files changed

+190
-186
lines changed

test/cli/fixtures/expected/tap-outputs.js

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,13 @@ not ok 1 Throws match > bad
6060
severity: failed
6161
actual : "Error: Match me with a pattern"
6262
expected: "/incorrect pattern/"
63-
stack: .*
63+
stack: at Object.<anonymous> (/qunit/test/cli/fixtures/fail/throws-match.js:3:10)
6464
...
6565
1..1
6666
# pass 0
6767
# skip 0
6868
# todo 0
69-
# fail 1
70-
`,
69+
# fail 1`,
7170

7271
"qunit test single.js 'glob/**/*-test.js'":
7372
`TAP version 13
@@ -140,10 +139,9 @@ not ok 2 global failure
140139
# pass 0
141140
# skip 0
142141
# todo 0
143-
# fail 2
144-
`,
142+
# fail 2`,
145143

146-
// Ignore the last frame about Node processing ticks (differs between Node 10 ad 12+)
144+
// The last frame differs between Node 10 and 12+ (changes in processing of ticks)
147145
"qunit no-tests":
148146
`TAP version 13
149147
not ok 1 global failure
@@ -153,18 +151,17 @@ not ok 1 global failure
153151
actual : {}
154152
expected: undefined
155153
stack: Error: No tests were run.
156-
at done (.*)
157-
at advanceTestQueue (.*)
158-
at Object.advance (.*)
159-
at unblockAndAdvanceQueue (.*)(\n at <anonymous>)?
160-
at .*
154+
at done (/qunit/qunit/qunit.js)
155+
at advanceTestQueue (/qunit/qunit/qunit.js)
156+
at Object.advance (/qunit/qunit/qunit.js)
157+
at unblockAndAdvanceQueue (/qunit/qunit/qunit.js)
158+
at internal
161159
...
162160
1..1
163161
# pass 0
164162
# skip 0
165163
# todo 0
166-
# fail 1
167-
`,
164+
# fail 1`,
168165

169166
"qunit sourcemap/source.js":
170167
`TAP version 13
@@ -181,8 +178,7 @@ not ok 2 Example > bad
181178
# pass 1
182179
# skip 0
183180
# todo 0
184-
# fail 1
185-
`,
181+
# fail 1`,
186182

187183
"NODE_OPTIONS='--enable-source-maps' qunit sourcemap/source.min.js":
188184
`TAP version 13
@@ -193,19 +189,18 @@ not ok 2 Example > bad
193189
severity: failed
194190
actual : false
195191
expected: true
196-
stack: at .* \\(.*source.min.js:1:.*\\)
197-
-> .*source.js:7:10
192+
stack: at Object.<anonymous> (/qunit/test/cli/fixtures/sourcemap/source.min.js:1:133)
193+
-> /qunit/test/cli/fixtures/sourcemap/sourcemap/source.js:7:10
198194
...
199195
1..2
200196
# pass 1
201197
# skip 0
202198
# todo 0
203-
# fail 1
204-
`,
199+
# fail 1`,
205200

206201
"qunit ../../es2018/esm.mjs":
207202
`TAP version 13
208-
ok 1 ESM test suite > sum\\(\\)
203+
ok 1 ESM test suite > sum()
209204
1..1
210205
# pass 1
211206
# skip 0
@@ -220,16 +215,14 @@ not ok 1 timeout > first
220215
severity: failed
221216
actual : null
222217
expected: undefined
223-
stack: at .* (.*timers.js.*)
224-
at .*(\n at .*)?(\n at .*)?
218+
stack: at internal
225219
...
226220
ok 2 timeout > second
227221
1..2
228222
# pass 1
229223
# skip 0
230224
# todo 0
231-
# fail 1
232-
`,
225+
# fail 1`,
233226

234227
"qunit zero-assertions.js":
235228
`TAP version 13
@@ -250,18 +243,17 @@ not ok 1 global failure
250243
actual : {}
251244
expected: undefined
252245
stack: Error: No tests matched the filter "no matches".
253-
at done (.*)
254-
at advanceTestQueue (.*)
255-
at Object.advance (.*)
256-
at unblockAndAdvanceQueue (.*)(\n at <anonymous>)?
257-
at .*
246+
at done (/qunit/qunit/qunit.js)
247+
at advanceTestQueue (/qunit/qunit/qunit.js)
248+
at Object.advance (/qunit/qunit/qunit.js)
249+
at unblockAndAdvanceQueue (/qunit/qunit/qunit.js)
250+
at internal
258251
...
259252
1..1
260253
# pass 0
261254
# skip 0
262255
# todo 0
263-
# fail 1
264-
`,
256+
# fail 1`,
265257

266258
"qunit single.js --require require-dep --require './node_modules/require-dep/module.js'":
267259
`required require-dep/index.js
@@ -302,7 +294,7 @@ not ok 1 # TODO module B > Only this module should run > a todo test
302294
severity: todo
303295
actual : false
304296
expected: true
305-
stack: .*
297+
stack: at Object.<anonymous> (/qunit/test/cli/fixtures/only/module.js:17:15)
306298
...
307299
ok 2 # SKIP module B > Only this module should run > implicitly skipped test
308300
ok 3 module B > Only this module should run > normal test
@@ -323,7 +315,7 @@ not ok 1 # TODO module B > test B
323315
severity: todo
324316
actual : false
325317
expected: true
326-
stack: .*
318+
stack: at Object.<anonymous> (/qunit/test/cli/fixtures/only/module-flat.js:9:13)
327319
...
328320
ok 2 # SKIP module B > test C
329321
ok 3 module B > test D

test/cli/helpers/execute.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,48 @@
22

33
const path = require( "path" );
44
const exec = require( "execa" ).shell;
5+
const reEscape = /([\\{}()|.?*+\-^$[\]])/g;
6+
7+
// Apply light normalization to CLI output to allow strict string
8+
// comparison across Node versions and OS platforms against the
9+
// expected output in fixtures/.
10+
function normalize( actual ) {
11+
const dir = path.join( __dirname, "..", "..", ".." );
12+
const reDir = new RegExp( dir.replace( reEscape, "\\$1" ), "g" );
13+
14+
return actual
15+
.replace( reDir, "/qunit" )
16+
.replace( /(\/qunit\/qunit\/qunit\.js):\d+:\d+\)/g, "$1)" )
17+
.replace( / at .+\([^/)][^)]*\)/g, " at internal" )
18+
19+
// merge successive lines after initial frame
20+
.replace( /(\n\s+at internal)+/g, "$1" )
21+
22+
// merge successive line with initial frame
23+
.replace( /(at internal)\n\s+at internal/g, "$1" );
24+
}
525

626
// Executes the provided command from within the fixtures directory
727
// The execaOptions parameter is used by test/cli/watch.js to
828
// control the stdio stream.
9-
module.exports = function execute( command, execaOptions ) {
29+
module.exports = async function execute( command, execaOptions, hook ) {
1030
const cwd = process.cwd();
1131
process.chdir( path.join( __dirname, "..", "fixtures" ) );
1232

1333
command = command.replace( /(^| )qunit\b/, "$1../../../bin/qunit.js" );
1434
const execution = exec( command, execaOptions );
35+
if ( hook ) {
36+
hook( execution );
37+
}
1538

1639
process.chdir( cwd );
1740

18-
return execution;
41+
try {
42+
const result = await execution;
43+
result.stdout = normalize( String( result.stdout ).trimEnd() );
44+
return result;
45+
} catch ( e ) {
46+
e.stdout = normalize( String( e.stdout ).trimEnd() );
47+
throw e;
48+
}
1949
};

test/cli/main.js

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ const expectedOutput = require( "./fixtures/expected/tap-outputs" );
44
const execute = require( "./helpers/execute" );
55
const semver = require( "semver" );
66

7-
QUnit.assert.matches = function( actual, expected, message ) {
8-
this.pushResult( {
9-
result: expected.test( actual ),
10-
actual,
11-
expected: expected.toString(),
12-
message
13-
} );
14-
};
15-
167
QUnit.module( "CLI Main", function() {
178
QUnit.test( "defaults to running tests in 'test' directory", async function( assert ) {
189
const command = "qunit";
@@ -93,8 +84,7 @@ QUnit.module( "CLI Main", function() {
9384
} catch ( e ) {
9485
assert.equal( e.code, 1 );
9586
assert.equal( e.stderr, "" );
96-
const re = new RegExp( expectedOutput[ command ] );
97-
assert.equal( re.test( e.stdout ), true );
87+
assert.equal( e.stdout, expectedOutput[ command ] );
9888
}
9989
} );
10090

@@ -113,8 +103,7 @@ QUnit.module( "CLI Main", function() {
113103
} catch ( e ) {
114104
assert.equal( e.code, 1 );
115105
assert.equal( e.stderr, "" );
116-
const re = new RegExp( expectedOutput[ command ] );
117-
assert.equal( re.test( e.stdout ), true );
106+
assert.equal( e.stdout, expectedOutput[ command ] );
118107
}
119108
} );
120109

@@ -158,11 +147,7 @@ QUnit.module( "CLI Main", function() {
158147
assert.equal( execution.stderr, "" );
159148
}
160149

161-
const re = new RegExp( expectedOutput[ command ] );
162-
assert.equal( re.test( execution.stdout ), true );
163-
if ( !re.test( execution.stdout ) ) {
164-
assert.equal( execution.stdout, expectedOutput[ command ] );
165-
}
150+
assert.equal( execution.stdout, expectedOutput[ command ] );
166151
} );
167152
}
168153

@@ -188,11 +173,7 @@ QUnit.module( "CLI Main", function() {
188173
} catch ( e ) {
189174
assert.equal( e.code, 1 );
190175
assert.equal( e.stderr, "" );
191-
const re = new RegExp( expectedOutput[ command ] );
192-
assert.equal( re.test( e.stdout ), true );
193-
if ( !re.test( e.stdout ) ) {
194-
assert.equal( e.stdout, expectedOutput[ command ] );
195-
}
176+
assert.equal( e.stdout, expectedOutput[ command ] );
196177
}
197178
} );
198179
}
@@ -204,8 +185,7 @@ QUnit.module( "CLI Main", function() {
204185
} catch ( e ) {
205186
assert.equal( e.code, 1 );
206187
assert.equal( e.stderr, "" );
207-
const re = new RegExp( expectedOutput[ command ] );
208-
assert.equal( re.test( e.stdout ), true );
188+
assert.equal( e.stdout, expectedOutput[ command ] );
209189
}
210190
} );
211191

@@ -249,8 +229,7 @@ QUnit.module( "CLI Main", function() {
249229
} catch ( e ) {
250230
assert.equal( e.code, 1 );
251231
assert.equal( e.stderr, "" );
252-
const re = new RegExp( expectedOutput[ command ] );
253-
assert.equal( re.test( e.stdout ), true );
232+
assert.equal( e.stdout, expectedOutput[ command ] );
254233
}
255234
} );
256235
} );
@@ -323,8 +302,7 @@ QUnit.module( "CLI Main", function() {
323302

324303
assert.equal( execution.code, 0 );
325304
assert.equal( execution.stderr, "" );
326-
const re = new RegExp( expectedOutput[ command ] );
327-
assert.matches( execution.stdout, re );
305+
assert.equal( execution.stdout, expectedOutput[ command ] );
328306
} );
329307

330308
QUnit.test( "flat modules", async function( assert ) {
@@ -334,8 +312,7 @@ QUnit.module( "CLI Main", function() {
334312

335313
assert.equal( execution.code, 0 );
336314
assert.equal( execution.stderr, "" );
337-
const re = new RegExp( expectedOutput[ command ] );
338-
assert.matches( execution.stdout, re );
315+
assert.equal( execution.stdout, expectedOutput[ command ] );
339316
} );
340317
} );
341318
} );

0 commit comments

Comments
 (0)