0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-common-must-not-call.js
Lance Ball a322b8e316
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.

PR-URL: https://github.com/nodejs/node/pull/17257
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
2017-12-04 11:43:43 -05:00

27 lines
719 B
JavaScript

'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);
}