chore(test): lint raw test id selectors

This commit is contained in:
Danila Poyarkov 2026-05-15 17:10:35 +03:00
parent 4a174e52ed
commit 4e942f77d7
2 changed files with 37 additions and 0 deletions

View file

@ -324,6 +324,41 @@ const noInvalidTestIdAttributes = {
}
}
const noRawTestIdSelectorsInTests = {
meta: {
docs: {
description: 'Disallow raw data-test-id CSS selectors in Playwright tests — use getByTestId()'
}
},
create(context) {
const file = normalizedFilename(context)
if (!file.includes('/tests/')) return {}
return {
CallExpression(node) {
const callee = node.callee
if (callee?.type !== 'MemberExpression') return
if (callee.property?.type !== 'Identifier' || callee.property.name !== 'locator') return
const firstArg = node.arguments?.[0]
if (!firstArg) return
const isRawTestIdSelector =
(firstArg.type === 'Literal' &&
typeof firstArg.value === 'string' &&
firstArg.value.includes('[data-test-id')) ||
(firstArg.type === 'TemplateLiteral' &&
firstArg.quasis?.some((part) => part.value.raw.includes('[data-test-id')))
if (!isRawTestIdSelector) return
context.report({
node,
message: 'Use getByTestId() instead of raw [data-test-id] CSS selectors in tests.'
})
}
}
}
}
const noGeneratedTestIdLiterals = {
meta: {
docs: {
@ -1442,6 +1477,7 @@ const plugin = {
'no-dynamic-data-test-id-in-vue': noDynamicDataTestIdInVue,
'no-test-id-helper-bind-in-vue': noTestIdHelperBindInVue,
'no-invalid-test-id-attributes': noInvalidTestIdAttributes,
'no-raw-test-id-selectors-in-tests': noRawTestIdSelectorsInTests,
'no-generated-test-id-literals': noGeneratedTestIdLiterals,
'no-document-query-selector-in-vue': noDocumentQuerySelectorInVue,
'no-direct-selection-tool-state-mutation': noDirectSelectionToolStateMutation,

View file

@ -116,6 +116,7 @@
"open-pencil/no-dynamic-data-test-id-in-vue": "error",
"open-pencil/no-test-id-helper-bind-in-vue": "error",
"open-pencil/no-invalid-test-id-attributes": "error",
"open-pencil/no-raw-test-id-selectors-in-tests": "error",
"open-pencil/no-generated-test-id-literals": "error",
"open-pencil/no-document-query-selector-in-vue": "error",
"open-pencil/no-direct-selection-tool-state-mutation": "error",