eslint-plugin-wdio
Version:
Eslint rules for WebdriverIO
214 lines (208 loc) • 5.19 kB
JavaScript
// src/constants.ts
var MATCHERS = [
"toHaveTitle",
"toHaveUrl",
"toBeClickable",
"toBeDisabled",
"toBeDisplayed",
"toBeDisplayedInViewport",
"toBeEnabled",
"toExist",
"toBeExisting",
"toBePresent",
"toBeFocused",
"toBeSelected",
"toHaveAttribute",
"toHaveChildren",
"toHaveElementClass",
"toHaveElementProperty",
"toHaveHref",
"toHaveId",
"toHaveStyle",
"toHaveText",
"toHaveValue",
"toBeElementsArrayOfSize",
"toBeRequested",
"toBeRequestedTimes",
"toBeRequestedWith",
"toBeRequestedWithResponse"
];
// src/rules/await-expect.ts
var rule = {
meta: {
type: "problem",
docs: {
description: "expect must be prefixed with await",
category: "Possible Errors",
url: "https://github.com/webdriverio/webdriverio/blob/main/packages/eslint-plugin-wdio/docs/rules/await-expect.md",
recommended: false
},
messages: {
missingAwait: "Missing await before an expect statement"
},
hasSuggestions: true
},
create: function(context) {
return {
CallExpression(node) {
if (node.callee.type !== "MemberExpression" || node.callee.object.type !== "CallExpression" || node.callee.object.callee.name !== "expect" || !MATCHERS.includes(node.callee.property.name)) {
return;
}
if (
/**
* expect is called without an `await` and as part of an
* expression
*/
node.parent.type === "ExpressionStatement"
) {
context.report({ node, messageId: "missingAwait" });
}
}
};
}
};
var await_expect_default = rule;
// src/utils/helpers.ts
var isCommand = function(expression, command, instances = ["browser"]) {
const callee = expression?.callee;
return callee && "object" in callee && "name" in callee.object && instances.includes(callee.object?.name) && "property" in callee && "name" in callee.property && callee.property?.name === command;
};
// src/rules/no-debug.ts
var rule2 = {
meta: {
type: "problem",
docs: {
description: "Disallow browser.debug() in tests",
category: "Possible Errors",
url: "https://github.com/webdriverio/webdriverio/blob/main/packages/eslint-plugin-wdio/docs/rules/no-debug.md",
recommended: false
},
messages: {
unexpectedDebug: "Unexpected browser.debug() not allowed"
},
hasSuggestions: true,
schema: [{
type: "object",
properties: {
instances: {
type: "array",
items: {
type: "string"
},
description: 'List of browser instances to check (default: ["browser"])',
default: ["browser"]
}
},
additionalProperties: false
}]
},
create: function(context) {
const options = context.options[0] || {};
const instances = options.instances || ["browser"];
return {
CallExpression(node) {
if (isCommand(node, "debug", instances)) {
context.report({
node,
messageId: "unexpectedDebug",
data: { instance: instances.join(", ") }
});
}
}
};
}
};
var no_debug_default = rule2;
// src/rules/no-pause.ts
var rule3 = {
meta: {
type: "problem",
docs: {
description: "Disallow browser.pause() in tests",
category: "Possible Errors",
url: "https://github.com/webdriverio/webdriverio/blob/main/packages/eslint-plugin-wdio/docs/rules/no-pause.md",
recommended: false
},
messages: {
unexpectedPause: "Unexpected browser.pause() not allowed"
},
hasSuggestions: true,
schema: [{
type: "object",
properties: {
instances: {
type: "array",
items: {
type: "string"
},
description: 'List of browser instances to check (default: ["browser"])',
default: ["browser"]
}
},
additionalProperties: false
}]
},
create: function(context) {
const options = context.options[0] || {};
const instances = options.instances || ["browser"];
return {
CallExpression(node) {
if (isCommand(node, "pause", instances)) {
context.report({
node,
messageId: "unexpectedPause",
data: { instance: instances.join(", ") }
});
}
}
};
}
};
var no_pause_default = rule3;
// src/index.ts
var sharedGlobals = {
$: false,
$$: false,
browser: false,
driver: false,
expect: false,
multiremotebrowser: false
};
var sharedConfig = {
rules: {
"wdio/await-expect": "error",
"wdio/no-debug": "error",
"wdio/no-pause": "error"
}
};
var index = {
configs: {},
rules: {
"await-expect": await_expect_default,
"no-debug": no_debug_default,
"no-pause": no_pause_default
}
};
var legacyConfig = {
...sharedConfig,
globals: sharedGlobals,
plugins: ["wdio"]
};
var flatConfig = {
...sharedConfig,
languageOptions: {
globals: sharedGlobals
},
plugins: {
wdio: index
}
};
var configs = {
"flat/recommended": flatConfig,
recommended: legacyConfig
};
var rules = index.rules;
export {
configs,
rules
};