Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
7 changes: 6 additions & 1 deletion src/rules/no-test-id-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ const RULE_NAME = 'no-test-id-queries';
export type MessageIds = 'noTestIdQueries';
type Options = [];

const QUERIES_REGEX = `/^(${ALL_QUERIES_VARIANTS.join('|')})TestId$/`;
const QUERY_VARIANTS = [
...ALL_QUERIES_VARIANTS,
...ALL_QUERIES_VARIANTS.map((query) => `${query}Shadow` as const),
];

const QUERIES_REGEX = `/^(${QUERY_VARIANTS.join('|')})TestId$/`;

export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
Expand Down
36 changes: 23 additions & 13 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const LIBRARY_MODULES = [
'@testing-library/vue',
'@testing-library/svelte',
'@marko/testing-library',
'shadow-dom-testing-library',
] as const;

const USER_EVENT_MODULE = '@testing-library/user-event';
Expand Down Expand Up @@ -59,6 +60,15 @@ const ALL_QUERIES_METHODS = [
'ByDisplayValue',
'ByRole',
'ByTestId',
// queries coming from 'shadow-dom-testing-library'
'ByShadowLabelText',
'ByShadowPlaceholderText',
'ByShadowText',
'ByShadowAltText',
'ByShadowTitle',
'ByShadowDisplayValue',
'ByShadowRole',
'ByShadowTestId',
] as const;

const SYNC_QUERIES_COMBINATIONS = combineQueries(
Expand Down Expand Up @@ -157,27 +167,27 @@ const PRESENCE_MATCHERS = [
const ABSENCE_MATCHERS = ['toBeNull', 'toBeFalsy'] as const;

export {
combineQueries,
getDocsUrl,
SYNC_QUERIES_VARIANTS,
ASYNC_QUERIES_VARIANTS,
ALL_QUERIES_VARIANTS,
ABSENCE_MATCHERS,
ALL_QUERIES_COMBINATIONS,
ALL_QUERIES_METHODS,
SYNC_QUERIES_COMBINATIONS,
ALL_QUERIES_VARIANTS,
ALL_RETURNING_NODES,
ASYNC_QUERIES_COMBINATIONS,
ALL_QUERIES_COMBINATIONS,
ASYNC_QUERIES_VARIANTS,
ASYNC_UTILS,
combineQueries,
DEBUG_UTILS,
EVENT_HANDLER_METHODS,
EVENTS_SIMULATORS,
TESTING_FRAMEWORK_SETUP_HOOKS,
getDocsUrl,
LIBRARY_MODULES,
PROPERTIES_RETURNING_NODES,
METHODS_RETURNING_NODES,
ALL_RETURNING_NODES,
OLD_LIBRARY_MODULES,
PRESENCE_MATCHERS,
ABSENCE_MATCHERS,
EVENT_HANDLER_METHODS,
PROPERTIES_RETURNING_NODES,
SYNC_QUERIES_COMBINATIONS,
SYNC_QUERIES_VARIANTS,
TESTING_FRAMEWORK_SETUP_HOOKS,
USER_EVENT_METHODS,
USER_EVENT_MODULE,
OLD_LIBRARY_MODULES,
};
6 changes: 6 additions & 0 deletions tests/rules/await-async-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
'@testing-library/react',
'@testing-library/vue',
'@marko/testing-library',
'shadow-dom-testing-library',
];

interface TestCode {
Expand Down Expand Up @@ -121,6 +122,11 @@ ruleTester.run(rule.name, rule, {
testingFramework: '@marko/testing-library',
}),

// async shadow-dom-testing-library screen queries declaration are valid
...createTestCase((query) => `await screen.${query}('foo')`, {
testingFramework: 'shadow-dom-testing-library',
}),

// async queries not called are valid
...createTestCase((query) => `expect(screen.${query}).toBeDefined()`, {
isAsync: false,
Expand Down
1 change: 1 addition & 0 deletions tests/rules/await-async-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
'@testing-library/react',
'@testing-library/vue',
'@marko/testing-library',
// 'shadow-dom-testing-library', does not export waitFor and waitForElementToBeRemoved utils, so not relevant to this rule
];

ruleTester.run(rule.name, rule, {
Expand Down
1 change: 1 addition & 0 deletions tests/rules/no-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
'@testing-library/react',
'@testing-library/vue',
'@marko/testing-library',
'shadow-dom-testing-library',
];

ruleTester.run(rule.name, rule, {
Expand Down
1 change: 1 addition & 0 deletions tests/rules/no-debugging-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
'@testing-library/react',
'@testing-library/vue',
'@marko/testing-library',
'shadow-dom-testing-library',
];

ruleTester.run(rule.name, rule, {
Expand Down
1 change: 1 addition & 0 deletions tests/rules/no-node-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
'@testing-library/react',
'@testing-library/vue',
'@marko/testing-library',
'shadow-dom-testing-library',
];

ruleTester.run(rule.name, rule, {
Expand Down
1 change: 1 addition & 0 deletions tests/rules/no-render-in-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
'@testing-library/react',
'@testing-library/vue',
'@marko/testing-library',
'shadow-dom-testing-library',
];

ruleTester.run(rule.name, rule, {
Expand Down
98 changes: 58 additions & 40 deletions tests/rules/no-test-id-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
'@testing-library/react',
'@testing-library/vue',
'@marko/testing-library',
'shadow-dom-testing-library',
];

const QUERIES = [
Expand All @@ -20,6 +21,47 @@ const QUERIES = [
'findAllByTestId',
];

const createInvalidTestCase = (framework: string, query: string) => {
return [
{
code: `
import { render } from '${framework}';

test('test', async () => {
const { ${query} } = render(<MyComponent />);

expect(${query}('my-test-id')).toBeInTheDocument();
});
`,
errors: [
{
messageId: 'noTestIdQueries',
line: 7,
column: 14,
},
],
},
{
code: `
import { render, screen } from '${framework}';

test('test', async () => {
render(<MyComponent />);

expect(screen.${query}('my-test-id')).toBeInTheDocument();
});
`,
errors: [
{
messageId: 'noTestIdQueries',
line: 7,
column: 14,
},
],
},
] as const;
};

ruleTester.run(rule.name, rule, {
valid: [
`
Expand All @@ -43,44 +85,20 @@ ruleTester.run(rule.name, rule, {
`,
],

invalid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((framework) =>
QUERIES.flatMap((query) => [
{
code: `
import { render } from '${framework}';

test('test', async () => {
const { ${query} } = render(<MyComponent />);

expect(${query}('my-test-id')).toBeInTheDocument();
});
`,
errors: [
{
messageId: 'noTestIdQueries',
line: 7,
column: 14,
},
],
},
{
code: `
import { render, screen } from '${framework}';

test('test', async () => {
render(<MyComponent />);

expect(screen.${query}('my-test-id')).toBeInTheDocument();
});
`,
errors: [
{
messageId: 'noTestIdQueries',
line: 7,
column: 14,
},
],
},
])
),
invalid: [
...SUPPORTED_TESTING_FRAMEWORKS.flatMap((framework) =>
QUERIES.flatMap((query) => createInvalidTestCase(framework, query))
),
// special cases for shadow-dom-testing-library
...[
'getByShadowTestId',
'queryByShadowTestId',
'getAllByShadowTestId',
'queryAllByShadowTestId',
'findByShadowTestId',
'findAllByShadowTestId',
].flatMap((query) =>
createInvalidTestCase('shadow-dom-testing-library', query)
),
],
});
1 change: 1 addition & 0 deletions tests/rules/prefer-find-by.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
'@testing-library/react',
'@testing-library/vue',
'@marko/testing-library',
'shadow-dom-testing-library',
Comment thread
Sysix marked this conversation as resolved.
Outdated
];

function buildFindByMethod(queryMethod: string) {
Expand Down
1 change: 1 addition & 0 deletions tests/rules/prefer-query-by-disappearance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
'@testing-library/react',
'@testing-library/vue',
'@marko/testing-library',
'shadow-dom-testing-library',
];

ruleTester.run(rule.name, rule, {
Expand Down
1 change: 1 addition & 0 deletions tests/rules/prefer-screen-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const SUPPORTED_TESTING_FRAMEWORKS = [
'@testing-library/react',
'@testing-library/vue',
'@marko/testing-library',
'shadow-dom-testing-library',
];

const CUSTOM_QUERY_COMBINATIONS = combineQueries(ALL_QUERIES_VARIANTS, [
Expand Down
3 changes: 2 additions & 1 deletion tests/utils/is-testing-library-module.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { it, expect, describe } from 'vitest';
import { describe, expect, it } from 'vitest';

import {
isCustomTestingLibraryModule,
Expand All @@ -20,6 +20,7 @@ const LIBRARY_MODULES = [
'@testing-library/vue',
'@testing-library/svelte',
'@marko/testing-library',
'shadow-dom-testing-library',
] as const;

const USER_EVENT_MODULE = '@testing-library/user-event';
Expand Down
Loading