diff --git a/test/common/README.md b/test/common/README.md index 7fa47677ca8ce2..f48215836ecae5 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -133,6 +133,12 @@ a reason otherwise. Returns an instance of all possible `ArrayBufferView`s of the provided Buffer. +### getCallSite(func) +* `func` [<Function>] +* return [<String>] + +Returns the file name and line number for the provided Function. + ### globalCheck * [<Boolean>] diff --git a/test/common/index.js b/test/common/index.js index a78f90087793c1..2cf188abe5ffc9 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -566,9 +566,23 @@ exports.canCreateSymLink = function() { return true; }; +exports.getCallSite = function getCallSite(top) { + const originalStackFormatter = Error.prepareStackTrace; + Error.prepareStackTrace = (err, stack) => + `${stack[0].getFileName()}:${stack[0].getLineNumber()}`; + const err = new Error(); + Error.captureStackTrace(err, top); + // with the V8 Error API, the stack is not formatted until it is accessed + err.stack; + Error.prepareStackTrace = originalStackFormatter; + return err.stack; +}; + exports.mustNotCall = function(msg) { + const callSite = exports.getCallSite(exports.mustNotCall); return function mustNotCall() { - assert.fail(msg || 'function should not have been called'); + assert.fail( + `${msg || 'function should not have been called'} at ${callSite}`); }; }; diff --git a/test/parallel/test-common-must-not-call.js b/test/parallel/test-common-must-not-call.js new file mode 100644 index 00000000000000..d70daabf0a4bd0 --- /dev/null +++ b/test/parallel/test-common-must-not-call.js @@ -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); +}