Skip to content

refactor: strengthen type safety #1015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 6 additions & 4 deletions lib/create-testing-library-rule/detect-testing-library-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,10 @@ export function detectTestingLibraryUtils<
}

return isNegated
? ABSENCE_MATCHERS.includes(matcher)
: PRESENCE_MATCHERS.includes(matcher);
? ABSENCE_MATCHERS.some((absenceMather) => absenceMather === matcher)
: PRESENCE_MATCHERS.some(
(presenceMather) => presenceMather === matcher
);
};

/**
Expand All @@ -821,8 +823,8 @@ export function detectTestingLibraryUtils<
}

return isNegated
? PRESENCE_MATCHERS.includes(matcher)
: ABSENCE_MATCHERS.includes(matcher);
? PRESENCE_MATCHERS.some((presenceMather) => presenceMather === matcher)
: ABSENCE_MATCHERS.some((absenceMather) => absenceMather === matcher);
};

const isMatchingAssert: IsMatchingAssertFn = (node, matcherName) => {
Expand Down
19 changes: 16 additions & 3 deletions lib/create-testing-library-rule/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ESLintUtils } from '@typescript-eslint/utils';

import { getDocsUrl, TestingLibraryPluginDocs } from '../utils';
import {
getDocsUrl,
TestingLibraryPluginDocs,
TestingLibraryPluginRuleModule,
} from '../utils';

import {
DetectionOptions,
Expand All @@ -27,11 +31,20 @@
create: EnhancedRuleCreate<TMessageIds, TOptions>;
detectionOptions?: Partial<DetectionOptions>;
}
>) =>
ESLintUtils.RuleCreator<TestingLibraryPluginDocs<TOptions>>(getDocsUrl)({
>): TestingLibraryPluginRuleModule<TMessageIds, TOptions> => {
const rule = ESLintUtils.RuleCreator<TestingLibraryPluginDocs<TOptions>>(
getDocsUrl
)({
...remainingConfig,
create: detectTestingLibraryUtils<TMessageIds, TOptions>(
create,
detectionOptions
),
});
const { docs } = rule.meta;
if (docs === undefined) {
throw new Error('Rule metadata must contain `docs` property');

Check warning on line 46 in lib/create-testing-library-rule/index.ts

View check run for this annotation

Codecov / codecov/patch

lib/create-testing-library-rule/index.ts#L46

Added line #L46 was not covered by tests
}

return { ...rule, meta: { ...rule.meta, docs } };
};
11 changes: 8 additions & 3 deletions lib/rules/no-node-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ export default createTestingLibraryRule<Options, MessageIds>({
return;
}

const propertyName = ASTUtils.isIdentifier(node.property)
? node.property.name
: null;

if (
ASTUtils.isIdentifier(node.property) &&
ALL_RETURNING_NODES.includes(node.property.name)
ALL_RETURNING_NODES.some(
(allReturningNode) => allReturningNode === propertyName
)
) {
if (allowContainerFirstChild && node.property.name === 'firstChild') {
if (allowContainerFirstChild && propertyName === 'firstChild') {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-render-in-lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
type: 'object',
properties: {
allowTestingFrameworkSetupHook: {
enum: TESTING_FRAMEWORK_SETUP_HOOKS,
enum: [...TESTING_FRAMEWORK_SETUP_HOOKS],
type: 'string',
},
},
Expand Down
12 changes: 9 additions & 3 deletions lib/rules/prefer-explicit-assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
properties: {
assertion: {
type: 'string',
enum: PRESENCE_MATCHERS,
enum: [...PRESENCE_MATCHERS],
},
includeFindQueries: { type: 'boolean' },
},
Expand Down Expand Up @@ -182,8 +182,14 @@ export default createTestingLibraryRule<Options, MessageIds>({
}

const shouldEnforceAssertion =
(!isNegatedMatcher && PRESENCE_MATCHERS.includes(matcher)) ||
(isNegatedMatcher && ABSENCE_MATCHERS.includes(matcher));
(!isNegatedMatcher &&
PRESENCE_MATCHERS.some(
(presenceMather) => presenceMather === matcher
)) ||
(isNegatedMatcher &&
ABSENCE_MATCHERS.some(
(absenceMather) => absenceMather === matcher
));

if (shouldEnforceAssertion && matcher !== assertion) {
context.report({
Expand Down
34 changes: 21 additions & 13 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ export * from './compat';
export * from './file-import';
export * from './types';

const combineQueries = (variants: string[], methods: string[]): string[] => {
const combineQueries = (
variants: readonly string[],
methods: readonly string[]
): string[] => {
const combinedQueries: string[] = [];
variants.forEach((variant) => {
const variantPrefix = variant.replace('By', '');
Expand All @@ -25,14 +28,19 @@ const LIBRARY_MODULES = [
'@testing-library/vue',
'@testing-library/svelte',
'@marko/testing-library',
];
] as const;

const SYNC_QUERIES_VARIANTS = ['getBy', 'getAllBy', 'queryBy', 'queryAllBy'];
const ASYNC_QUERIES_VARIANTS = ['findBy', 'findAllBy'];
const SYNC_QUERIES_VARIANTS = [
'getBy',
'getAllBy',
'queryBy',
'queryAllBy',
] as const;
const ASYNC_QUERIES_VARIANTS = ['findBy', 'findAllBy'] as const;
const ALL_QUERIES_VARIANTS = [
...SYNC_QUERIES_VARIANTS,
...ASYNC_QUERIES_VARIANTS,
];
] as const;

const ALL_QUERIES_METHODS = [
'ByLabelText',
Expand All @@ -43,7 +51,7 @@ const ALL_QUERIES_METHODS = [
'ByDisplayValue',
'ByRole',
'ByTestId',
];
] as const;

const SYNC_QUERIES_COMBINATIONS = combineQueries(
SYNC_QUERIES_VARIANTS,
Expand All @@ -58,7 +66,7 @@ const ASYNC_QUERIES_COMBINATIONS = combineQueries(
const ALL_QUERIES_COMBINATIONS = [
...SYNC_QUERIES_COMBINATIONS,
...ASYNC_QUERIES_COMBINATIONS,
];
] as const;

const ASYNC_UTILS = ['waitFor', 'waitForElementToBeRemoved'] as const;

Expand All @@ -73,7 +81,7 @@ const DEBUG_UTILS = [

const EVENTS_SIMULATORS = ['fireEvent', 'userEvent'] as const;

const TESTING_FRAMEWORK_SETUP_HOOKS = ['beforeEach', 'beforeAll'];
const TESTING_FRAMEWORK_SETUP_HOOKS = ['beforeEach', 'beforeAll'] as const;

const PROPERTIES_RETURNING_NODES = [
'activeElement',
Expand All @@ -93,7 +101,7 @@ const PROPERTIES_RETURNING_NODES = [
'previousSibling',
'rootNode',
'scripts',
];
] as const;

const METHODS_RETURNING_NODES = [
'closest',
Expand All @@ -104,20 +112,20 @@ const METHODS_RETURNING_NODES = [
'getElementsByTagNameNS',
'querySelector',
'querySelectorAll',
];
] as const;

const ALL_RETURNING_NODES = [
...PROPERTIES_RETURNING_NODES,
...METHODS_RETURNING_NODES,
];
] as const;

const PRESENCE_MATCHERS = [
'toBeOnTheScreen',
'toBeInTheDocument',
'toBeTruthy',
'toBeDefined',
];
const ABSENCE_MATCHERS = ['toBeNull', 'toBeFalsy'];
] as const;
const ABSENCE_MATCHERS = ['toBeNull', 'toBeFalsy'] as const;

export {
combineQueries,
Expand Down
Loading