alm
Version:
The best IDE for TypeScript
95 lines (94 loc) • 3.38 kB
JavaScript
;
/**
* Instruments a spec file so we know what's happening
* Reasons:
* - console.log() : breaks mocha built in json reporter
* - figuring out `describe` / `it` lines is easiest with instrumentation
*/
Object.defineProperty(exports, "__esModule", { value: true });
/** Our only import */
var common = require("./instrumenterCommon");
var stringify = common.stringify, makeTestLogPosition = common.makeTestLogPosition, stackFromCaller = common.stackFromCaller;
/**
* Collects all our logs
*/
var logs = [];
var addToLogs = function () {
var stack = stackFromCaller();
var args = (Array.from(arguments));
var testLogPosition = makeTestLogPosition(filePath, stack);
logs.push({ testLogPosition: testLogPosition, args: args });
};
/**
* Mock out console
* NOTE: we mock out individual functions because
* the whole `console` is actually readonly in nodejs and cannot be set.
*/
var log = console.log.bind(console);
console.log = addToLogs;
console.warn = addToLogs;
console.error = addToLogs;
/**
* Get the filePath from the arguments ;)
*/
var filePath = process.argv[process.argv.length - 1];
/* Send logs to the data file */
process.on('exit', function () {
common.writeDataFile(filePath, { logs: logs, suites: suites, its: its });
});
/**
* Intercept all calls to describe and it
*/
/** The positions */
var suites = [];
var its = [];
/** The interceptor */
var Mocha = require('mocha');
var origBDD = Mocha.interfaces["bdd"];
Mocha.interfaces["bdd"] = function (suite) {
// Still do what the original one did to let its `pre-require` pass
origBDD(suite);
var addToDescribe = function (title) {
var stack = stackFromCaller().slice(1);
var testLogPosition = makeTestLogPosition(filePath, stack);
suites.push({ title: title, testLogPosition: testLogPosition });
};
var addToIt = function (title) {
var stack = stackFromCaller().slice(1);
var testLogPosition = makeTestLogPosition(filePath, stack);
its.push({ title: title, testLogPosition: testLogPosition });
};
// And attach our own custom pre-require to fixup context watchers
suite.on('pre-require', function (context, file, mocha) {
var origDescribe = context.describe;
var origDescribeOnly = context.describe.only;
var origDescribeSkip = context.describe.skip;
context.describe = function (title) {
addToDescribe(title);
return origDescribe.apply(context, arguments);
};
context.describe.only = function (title) {
addToDescribe(title);
return origDescribeOnly.apply(origDescribe, arguments);
};
context.describe.skip = function (title) {
addToDescribe(title);
return origDescribeSkip.apply(origDescribe, arguments);
};
var origIt = context.it;
var origItOnly = context.it.only;
var origItSkip = context.it.skip;
context.it = function (title) {
addToIt(title);
return origIt.apply(context, arguments);
};
context.it.only = function (title) {
addToIt(title);
return origItOnly.apply(origIt, arguments);
};
context.it.skip = function (title) {
addToIt(title);
return origItSkip.apply(origIt, arguments);
};
});
};