vscode-extension-tester
Version:
ExTester is a package that is designed to help you run UI tests for your Visual Studio Code extensions using selenium-webdriver.
142 lines • 5.04 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.before = vscodeBefore;
exports.beforeEach = vscodeBeforeEach;
exports.after = vscodeAfter;
exports.afterEach = vscodeAfterEach;
const extester_1 = require("../extester");
const sanitize_filename_1 = __importDefault(require("sanitize-filename"));
function vscodeBefore(name, fn) {
callHook('before', name, fn);
}
function vscodeBeforeEach(name, fn) {
callHook('beforeEach', name, fn);
}
function vscodeAfter(name, fn) {
callHook('after', name, fn);
}
function vscodeAfterEach(name, fn) {
callHook('afterEach', name, fn);
}
/**
* Create new function which wraps original function. The wrapper function
* will be able to create screenshots in case of test crashes.
* @param hookType hook name
* @param fn callback function
* @returns wrapped function capable of doing screenshots on callback failure
*/
function createScreenshotCallbackFunction(name, hookType, fn) {
const alternativeFileName = createAlternativeFileName(hookType);
return async function () {
try {
await fn.call(this);
}
catch (e) {
if (this === undefined) {
throw e;
}
if (this.test === undefined) {
try {
await extester_1.VSBrowser.instance.takeScreenshot((0, sanitize_filename_1.default)(alternativeFileName));
}
catch (screenshotError) {
console.error(`Could not take screenshot. this.test is undefined. Reason:\n${screenshotError}\n\n`);
}
throw e;
}
try {
const titlePath = this.test.titlePath();
await extester_1.VSBrowser.instance.takeScreenshot((0, sanitize_filename_1.default)(titlePath.join('.')));
}
catch (screenshotError) {
console.error(`Could not take screenshot. Reason:\n${screenshotError}\n\n`);
}
throw e;
}
};
}
/**
* Call Mocha hook function with given arguments.
* @param hookType hook name
* @param firstArgument hook description or a callback
* @param secondArgument callback to be called or undefined
*/
function callHook(hookType, firstArgument, secondArgument) {
/* Disallowed combinations */
if (typeof firstArgument === 'function' && secondArgument !== undefined) {
throw new Error(`${hookType}(func1, func2) If the first argument is a function, then the second argument must be undefined.`);
}
if (typeof firstArgument === 'string' && secondArgument === undefined) {
throw new Error(`${hookType}(${firstArgument}) required callback function as seconds argument.`);
}
if (firstArgument === undefined && secondArgument === undefined) {
throw new Error(`${hookType}() requires at least callback function.`);
}
/* Remaining 2 combinations are valid. eg. before(callback) and before(name, callback). */
const name = typeof firstArgument === 'string' ? firstArgument : undefined;
const fn = typeof firstArgument === 'function' ? firstArgument : secondArgument;
const hook = getHookFunction(hookType);
const callback = fn ? fn : () => { };
if (name !== undefined) {
hook(name, createScreenshotCallbackFunction(name, hookType, callback));
}
else {
hook(createScreenshotCallbackFunction(name, hookType, callback));
}
}
/**
* Get hook function from hook name.
* @param hookType name of wanted hook function
* @returns hook function
*/
function getHookFunction(hookType) {
switch (hookType) {
case 'before':
return before;
case 'beforeEach':
return beforeEach;
case 'after':
return after;
case 'afterEach':
return afterEach;
default:
throw new Error(`Unknown hook type "${hookType}".`);
}
}
/**
* Create number generator [1..].
*/
function* createScreenshotNameGenerator() {
let counter = 1;
while (true) {
yield counter;
counter++;
}
}
/**
* Create alternative filename if given hook does not have name.
* @param hookType hook name
* @returns alternative file name without extension
*/
function createAlternativeFileName(hookType) {
const generator = screenshotNameGenerators.get(hookType);
if (generator) {
return `${hookType} ${generator.next().value}`;
}
else {
throw new Error(`Unknown mocha hook type "${hookType}".`);
}
}
/**
* Create number generator for each callback.
*/
const screenshotNameGenerators = new Map(Object.entries({
before: createScreenshotNameGenerator(),
beforeEach: createScreenshotNameGenerator(),
after: createScreenshotNameGenerator(),
afterEach: createScreenshotNameGenerator(),
}));
//# sourceMappingURL=mochaHooks.js.map