@wdio/mocha-framework
Version:
A WebdriverIO plugin. Adapter for Mocha testing framework.
142 lines (139 loc) • 5.07 kB
JavaScript
// src/common.ts
import logger from "@wdio/logger";
import { wrapGlobalTestMethod } from "@wdio/utils";
// src/constants.ts
var INTERFACES = {
bdd: ["it", "specify", "before", "beforeEach", "after", "afterEach"],
tdd: ["test", "suiteSetup", "setup", "suiteTeardown", "teardown"],
qunit: ["test", "before", "beforeEach", "after", "afterEach"]
};
var TEST_INTERFACES = {
bdd: ["it", "specify"],
tdd: ["test"],
qunit: ["test"]
};
var MOCHA_TIMEOUT_MESSAGE = 'For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.';
// src/common.ts
var log = logger("@wdio/mocha-framework");
var MOCHA_UI_TYPE_EXTRACTOR = /^(?:.*-)?([^-.]+)(?:.js)?$/;
var DEFAULT_INTERFACE_TYPE = "bdd";
var HOOK_LISTS = [["_beforeAll", "beforeAll"], ["_beforeEach", "beforeEach"], ["_afterEach", "afterEach"], ["_afterAll", "afterAll"]];
var TITLE_KINDS = { "before all": "beforeAll", "before each": "beforeEach", "after each": "afterEach", "after all": "afterAll" };
function checkHookKind(hook, kind) {
const titleKind = TITLE_KINDS[(hook.originalTitle ?? hook.title)?.match(/^"(before|after) (all|each)" hook/)?.slice(1).join(" ") ?? ""];
if (titleKind && titleKind !== kind) {
log.debug(`hook "${hook.title}" is a ${kind} hook, but its title says ${titleKind}`);
}
return kind;
}
function mochaHookKind(hook) {
const parent = hook.parent;
const kind = HOOK_LISTS.find(([list]) => parent?.[list]?.includes(hook))?.[1];
return kind && checkHookKind(hook, kind);
}
function dottedFullTitle(title, parent) {
let fullTitle = title;
while (parent && parent.title) {
fullTitle = parent.title + "." + fullTitle;
parent = parent.parent;
}
return fullTitle;
}
function formatError(err, testName) {
if (err.message && err.message.includes(MOCHA_TIMEOUT_MESSAGE)) {
const replacement = `The execution in the test "${testName}" took too long. Try to reduce the run time or increase your timeout for test specs (https://webdriver.io/docs/timeouts).`;
err.message = err.message.replace(MOCHA_TIMEOUT_MESSAGE, replacement);
err.stack = err.stack?.replace(MOCHA_TIMEOUT_MESSAGE, replacement);
}
return {
name: err.name,
message: err.message,
stack: err.stack,
type: err.type || err.name,
expected: err.expected,
actual: err.actual
};
}
function formatMessage(params) {
const message = {
type: params.type
};
const payload = params.payload;
const mochaAllHooksIfPresent = payload?.title?.match(/^"(before|after)( all| each)?" hook/);
if (params.err) {
const isSkipError = /^(sync|async) skip; aborting execution$/.test(params.err.message || "");
const isHookSkip = isSkipError && mochaAllHooksIfPresent;
if (isHookSkip) {
message.type = "hook:end";
} else {
const testName = (payload?.parent?.title ? `${payload.parent.title} ${payload.title}` : payload?.title) || "unknown test";
message.error = formatError(params.err, testName);
if (mochaAllHooksIfPresent) {
message.type = "hook:end";
}
}
}
if (payload) {
message.title = payload.title;
message.parent = payload.parent ? payload.parent.title : void 0;
message.fullTitle = dottedFullTitle(message.title, payload.parent);
message.pending = payload.pending || false;
message.file = payload.file;
message.duration = payload.duration;
message.body = payload.body;
if (payload.ctx && payload.ctx.currentTest) {
message.currentTest = payload.ctx.currentTest.title;
}
if (params.type.match(/Test/)) {
message.passed = payload.state === "passed";
}
if (payload.parent?.title && mochaAllHooksIfPresent) {
const hookName = mochaAllHooksIfPresent[0];
message.title = `${hookName} for ${payload.parent.title}`;
}
if (payload.context) {
message.context = payload.context;
}
const hookKind = message.type.startsWith("hook:") ? mochaHookKind(payload) : void 0;
if (hookKind) {
message.hookKind = hookKind;
}
}
return message;
}
function applyMochaDefaults(mochaOpts) {
if (mochaOpts.failHookAffectedTests === void 0) {
mochaOpts.failHookAffectedTests = true;
}
return mochaOpts;
}
function setupEnv(cid, options, beforeTest, beforeHook, afterTest, afterHook) {
const match = MOCHA_UI_TYPE_EXTRACTOR.exec(options.ui);
const type = match && INTERFACES[match[1]] && match[1] || DEFAULT_INTERFACE_TYPE;
const hookArgsFn = (context) => {
return [{ ...context.test, parent: context.test?.parent?.title }, context];
};
INTERFACES[type].forEach((fnName) => {
const isTest = TEST_INTERFACES[type].flatMap((testCommand) => [testCommand, testCommand + ".only"]).includes(fnName);
wrapGlobalTestMethod(
isTest,
isTest ? beforeTest : beforeHook,
// @ts-ignore
hookArgsFn,
isTest ? afterTest : afterHook,
hookArgsFn,
fnName,
cid
);
});
}
export {
TITLE_KINDS,
applyMochaDefaults,
checkHookKind,
dottedFullTitle,
formatError,
formatMessage,
mochaHookKind,
setupEnv
};