Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/rules/consistent-data-testid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type Options = [
];

const FILENAME_PLACEHOLDER = '{fileName}';
const PATH_SEPARATOR_PATTERN = /[/\\]/u;

export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
Expand Down Expand Up @@ -82,7 +83,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
const { testIdPattern, testIdAttribute: attr, customMessage } = options;

function getFileNameData() {
const splitPath = getFilename(context).split('/');
const splitPath = getFilename(context).split(PATH_SEPARATOR_PATTERN);
const fileNameWithExtension = splitPath.pop() ?? '';
if (
fileNameWithExtension.includes('[') ||
Expand Down
19 changes: 19 additions & 0 deletions tests/rules/consistent-data-testid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ const validTestCases: RuleValidTestCase[] = [
code: `
import React from 'react';

const TestComponent = props => {
return (
<div data-testid="Link__Button">
Hello
</div>
)
};
`,
options: [
{
testIdPattern: '^{fileName}__([A-Z]+[a-z]*?)+$',
},
],
filename: 'C:\\workspace\\projectX\\src\\components\\Link\\Link.tsx',
},
{
code: `
import React from 'react';

const TestComponent = props => {
return (
<div data-testid="Awesome">
Expand Down