-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: make common.mustNotCall show file:linenumber
When a test fails via `common.mustNotCall` it is sometimes hard to determine exactly what was called. This modification stores the caller's file and line number by using the V8 Error API to capture a stack at the time `common.mustNotCall()` is called. In the event of failure, this information is printed. This change also exposes a new function in test/common, `getCallSite()` which accepts a `function` and returns a `String` with the file name and line number for the function. Backport-PR-URL: #19355 PR-URL: #17257 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Khaidi Chu <[email protected]>
- Loading branch information
1 parent
969398d
commit 46aed58
Showing
3 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
|
||
const message = 'message'; | ||
const testFunction = common.mustNotCall(message); | ||
|
||
const validateError = common.mustCall((e) => { | ||
const prefix = `${message} at `; | ||
assert.ok(e.message.startsWith(prefix)); | ||
if (process.platform === 'win32') { | ||
e.message = e.message.substring(2); // remove 'C:' | ||
} | ||
const [ fileName, lineNumber ] = e.message | ||
.substring(prefix.length).split(':'); | ||
assert.strictEqual(path.basename(fileName), 'test-common-must-not-call.js'); | ||
assert.strictEqual(lineNumber, '8'); | ||
}); | ||
|
||
try { | ||
testFunction(); | ||
} catch (e) { | ||
validateError(e); | ||
} |