@@ -297,36 +364,37 @@ const invalidTestCases: InvalidTestCase[] = [
)
};
`,
- options: [
- {
- testIdPattern: '^right$',
- testIdAttribute: ['custom-attr', 'another-custom-attr'],
- },
- ],
- filename: '/my/cool/__tests__/Parent/index.js',
- errors: [
- {
- messageId: 'consistentDataTestId',
- data: {
- attr: 'custom-attr',
- value: 'wrong',
- regex: '/^right$/',
- },
- },
- {
- messageId: 'consistentDataTestId',
- data: {
- attr: 'another-custom-attr',
- value: 'wrong',
- regex: '/^right$/',
- },
- },
- ],
- },
- {
- code: `
+ options: [
+ {
+ testIdPattern: '^right$',
+ testIdAttribute: ['custom-attr', 'another-custom-attr'],
+ },
+ ],
+ filename: '/my/cool/__tests__/Parent/index.js',
+ errors: [
+ {
+ messageId: 'consistentDataTestId',
+ data: {
+ attr: 'custom-attr',
+ value: 'wrong',
+ regex: '/^right$/',
+ },
+ },
+ {
+ messageId: 'consistentDataTestId',
+ data: {
+ attr: 'another-custom-attr',
+ value: 'wrong',
+ regex: '/^right$/',
+ message: '',
+ },
+ },
+ ],
+ },
+ {
+ code: `
import React from 'react';
-
+
const TestComponent = props => {
return (
@@ -335,29 +403,61 @@ const invalidTestCases: InvalidTestCase[] = [
)
};
`,
- options: [
- {
- testIdPattern: '^{fileName}(__([A-Z]+[a-z]*?)+)*$',
- },
- ],
- filename: '/my/cool/__tests__/Parent/index.js',
- errors: [
- {
- messageId: 'consistentDataTestId',
- data: {
- attr: 'data-testid',
- value: 'WrongComponent__cool',
- regex: '/^Parent(__([A-Z]+[a-z]*?)+)*$/',
- },
- },
- ],
- },
+ options: [
+ {
+ testIdPattern: '^{fileName}(__([A-Z]+[a-z]*?)+)*$',
+ },
+ ],
+ filename: '/my/cool/__tests__/Parent/index.js',
+ errors: [
+ {
+ messageId: 'consistentDataTestId',
+ data: {
+ attr: 'data-testid',
+ value: 'WrongComponent__cool',
+ regex: '/^Parent(__([A-Z]+[a-z]*?)+)*$/',
+ message: '',
+ },
+ },
+ ],
+ },
+ {
+ code: ` // test for custom message
+ import React from 'react';
+
+ const TestComponent = props => {
+ return (
+
+ Hello
+
+ )
+ };
+ `,
+ options: [
+ {
+ testIdPattern: '^([a-z][a-z0-9]*)(-[a-z0-9]+)*$', //kebab-case
+ customMessage: 'Please use kebab-cased data-testid values.',
+ },
+ ],
+ filename: '/my/cool/__tests__/Parent/index.js',
+ errors: [
+ {
+ messageId: 'consistentDataTestIdCustomMessage',
+ data: {
+ attr: 'data-testid',
+ value: 'snake_case_value',
+ regex: '^([a-z][a-z0-9]*)(-[a-z0-9]+)*$',
+ message: 'Please use kebab-cased data-testid values.',
+ },
+ },
+ ],
+ },
];
ruleTester.run(RULE_NAME, rule, {
- valid: [...validTestCases, ...disableAggressiveReporting(validTestCases)],
- invalid: [
- ...invalidTestCases,
- ...disableAggressiveReporting(invalidTestCases),
- ],
+ valid: [...validTestCases, ...disableAggressiveReporting(validTestCases)],
+ invalid: [
+ ...invalidTestCases,
+ ...disableAggressiveReporting(invalidTestCases),
+ ],
});
diff --git a/tests/lib/rules/no-await-sync-events.test.ts b/tests/lib/rules/no-await-sync-events.test.ts
index e63c130b..da871acc 100644
--- a/tests/lib/rules/no-await-sync-events.test.ts
+++ b/tests/lib/rules/no-await-sync-events.test.ts
@@ -4,157 +4,199 @@ import { createRuleTester } from '../test-utils';
const ruleTester = createRuleTester();
const FIRE_EVENT_FUNCTIONS = [
- 'copy',
- 'cut',
- 'paste',
- 'compositionEnd',
- 'compositionStart',
- 'compositionUpdate',
- 'keyDown',
- 'keyPress',
- 'keyUp',
- 'focus',
- 'blur',
- 'focusIn',
- 'focusOut',
- 'change',
- 'input',
- 'invalid',
- 'submit',
- 'reset',
- 'click',
- 'contextMenu',
- 'dblClick',
- 'drag',
- 'dragEnd',
- 'dragEnter',
- 'dragExit',
- 'dragLeave',
- 'dragOver',
- 'dragStart',
- 'drop',
- 'mouseDown',
- 'mouseEnter',
- 'mouseLeave',
- 'mouseMove',
- 'mouseOut',
- 'mouseOver',
- 'mouseUp',
- 'popState',
- 'select',
- 'touchCancel',
- 'touchEnd',
- 'touchMove',
- 'touchStart',
- 'scroll',
- 'wheel',
- 'abort',
- 'canPlay',
- 'canPlayThrough',
- 'durationChange',
- 'emptied',
- 'encrypted',
- 'ended',
- 'loadedData',
- 'loadedMetadata',
- 'loadStart',
- 'pause',
- 'play',
- 'playing',
- 'progress',
- 'rateChange',
- 'seeked',
- 'seeking',
- 'stalled',
- 'suspend',
- 'timeUpdate',
- 'volumeChange',
- 'waiting',
- 'load',
- 'error',
- 'animationStart',
- 'animationEnd',
- 'animationIteration',
- 'transitionEnd',
- 'doubleClick',
- 'pointerOver',
- 'pointerEnter',
- 'pointerDown',
- 'pointerMove',
- 'pointerUp',
- 'pointerCancel',
- 'pointerOut',
- 'pointerLeave',
- 'gotPointerCapture',
- 'lostPointerCapture',
+ 'copy',
+ 'cut',
+ 'paste',
+ 'compositionEnd',
+ 'compositionStart',
+ 'compositionUpdate',
+ 'keyDown',
+ 'keyPress',
+ 'keyUp',
+ 'focus',
+ 'blur',
+ 'focusIn',
+ 'focusOut',
+ 'change',
+ 'input',
+ 'invalid',
+ 'submit',
+ 'reset',
+ 'click',
+ 'contextMenu',
+ 'dblClick',
+ 'drag',
+ 'dragEnd',
+ 'dragEnter',
+ 'dragExit',
+ 'dragLeave',
+ 'dragOver',
+ 'dragStart',
+ 'drop',
+ 'mouseDown',
+ 'mouseEnter',
+ 'mouseLeave',
+ 'mouseMove',
+ 'mouseOut',
+ 'mouseOver',
+ 'mouseUp',
+ 'popState',
+ 'select',
+ 'touchCancel',
+ 'touchEnd',
+ 'touchMove',
+ 'touchStart',
+ 'scroll',
+ 'wheel',
+ 'abort',
+ 'canPlay',
+ 'canPlayThrough',
+ 'durationChange',
+ 'emptied',
+ 'encrypted',
+ 'ended',
+ 'loadedData',
+ 'loadedMetadata',
+ 'loadStart',
+ 'pause',
+ 'play',
+ 'playing',
+ 'progress',
+ 'rateChange',
+ 'seeked',
+ 'seeking',
+ 'stalled',
+ 'suspend',
+ 'timeUpdate',
+ 'volumeChange',
+ 'waiting',
+ 'load',
+ 'error',
+ 'animationStart',
+ 'animationEnd',
+ 'animationIteration',
+ 'transitionEnd',
+ 'doubleClick',
+ 'pointerOver',
+ 'pointerEnter',
+ 'pointerDown',
+ 'pointerMove',
+ 'pointerUp',
+ 'pointerCancel',
+ 'pointerOut',
+ 'pointerLeave',
+ 'gotPointerCapture',
+ 'lostPointerCapture',
+];
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ '@testing-library/dom',
+ '@testing-library/angular',
+ '@testing-library/react',
+ '@testing-library/vue',
+ '@marko/testing-library',
];
const USER_EVENT_SYNC_FUNCTIONS = [
- 'clear',
- 'click',
- 'dblClick',
- 'selectOptions',
- 'deselectOptions',
- 'upload',
- // 'type',
- // 'keyboard',
- 'tab',
- 'paste',
- 'hover',
- 'unhover',
+ 'clear',
+ 'click',
+ 'dblClick',
+ 'selectOptions',
+ 'deselectOptions',
+ 'upload',
+ // 'type',
+ // 'keyboard',
+ 'tab',
+ 'paste',
+ 'hover',
+ 'unhover',
];
ruleTester.run(RULE_NAME, rule, {
- valid: [
- // sync fireEvents methods without await are valid
- ...FIRE_EVENT_FUNCTIONS.map((func) => ({
- code: `() => {
+ valid: [
+ // sync fireEvents methods without await are valid
+ ...FIRE_EVENT_FUNCTIONS.map((func) => ({
+ code: `() => {
fireEvent.${func}('foo')
}
`,
- })),
- // sync userEvent methods without await are valid
- ...USER_EVENT_SYNC_FUNCTIONS.map((func) => ({
- code: `() => {
+ })),
+ // sync userEvent methods without await are valid
+ ...USER_EVENT_SYNC_FUNCTIONS.map((func) => ({
+ code: `() => {
userEvent.${func}('foo')
}
`,
- })),
- {
- code: `() => {
+ })),
+ {
+ code: `() => {
userEvent.type(element, 'foo')
}
`,
- },
- {
- code: `() => {
+ },
+ {
+ code: `() => {
userEvent.keyboard('foo')
}
`,
- },
- {
- code: `() => {
+ },
+ {
+ code: `() => {
await userEvent.type(element, 'bar', {delay: 1234})
}
`,
- },
- {
- code: `() => {
+ },
+ {
+ code: `() => {
+ await userEvent.type(element, 'bar', {delay: null})
+ }
+ `,
+ },
+ {
+ code: `() => {
await userEvent.keyboard('foo', {delay: 1234})
}
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ {
+ code: `async() => {
+ const delay = 10
+ await userEvent.keyboard('foo', {delay})
+ }
+ `,
+ },
+ {
+ code: `async() => {
+ const delay = null
+ await userEvent.keyboard('foo', {delay})
+ }
+ `,
+ },
+ {
+ code: `async() => {
+ const delay = 10
+ await userEvent.type(element, text, {delay})
+ }
+ `,
+ },
+ {
+ code: `async() => {
+ let delay = 0
+ delay = 10
+ await userEvent.type(element, text, {delay})
+ }
+ `,
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { fireEvent } from 'somewhere-else';
test('should not report fireEvent.click() not related to Testing Library', async() => {
await fireEvent.click('foo');
});
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { fireEvent as renamedFireEvent } from 'somewhere-else';
import renamedUserEvent from '@testing-library/user-event';
import { fireEvent, userEvent } from 'somewhere-else'
@@ -165,100 +207,139 @@ ruleTester.run(RULE_NAME, rule, {
await userEvent.keyboard('foo', { delay: 5 });
});
`,
- },
- ],
+ },
+
+ // valid tests for fire-event when only user-event set in eventModules
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) =>
+ FIRE_EVENT_FUNCTIONS.map((func) => ({
+ code: `
+ import { fireEvent } from '${testingFramework}';
+ test('should not report fireEvent.${func} sync event awaited', async() => {
+ await fireEvent.${func}('foo');
+ });
+ `,
+ options: [{ eventModules: ['user-event'] }],
+ }))
+ ),
- invalid: [
- // sync fireEvent methods with await operator are not valid
- ...FIRE_EVENT_FUNCTIONS.map(
- (func) =>
- ({
- code: `
- import { fireEvent } from '@testing-library/framework';
+ // valid tests for user-event when only fire-event set in eventModules
+ ...USER_EVENT_SYNC_FUNCTIONS.map((func) => ({
+ code: `
+ import userEvent from '@testing-library/user-event';
+ test('should not report userEvent.${func} sync event awaited', async() => {
+ await userEvent.${func}('foo');
+ });
+ `,
+ options: [{ eventModules: ['fire-event'] }],
+ })),
+
+ // valid tests for user-event with default options (user-event disabled)
+ ...USER_EVENT_SYNC_FUNCTIONS.map((func) => ({
+ code: `
+ import userEvent from '@testing-library/user-event';
+ test('should not report userEvent.${func} by default', async() => {
+ await userEvent.${func}('foo');
+ });
+ `,
+ })),
+ ],
+
+ invalid: [
+ // sync fireEvent methods with await operator are not valid
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) =>
+ FIRE_EVENT_FUNCTIONS.map(
+ (func) =>
+ ({
+ code: `
+ import { fireEvent } from '${testingFramework}';
test('should report fireEvent.${func} sync event awaited', async() => {
await fireEvent.${func}('foo');
});
`,
- errors: [
- {
- line: 4,
- column: 17,
- messageId: 'noAwaitSyncEvents',
- data: { name: `fireEvent.${func}` },
- },
- ],
- } as const)
- ),
- // sync userEvent sync methods with await operator are not valid
- ...USER_EVENT_SYNC_FUNCTIONS.map(
- (func) =>
- ({
- code: `
+ errors: [
+ {
+ line: 4,
+ column: 17,
+ messageId: 'noAwaitSyncEvents',
+ data: { name: `fireEvent.${func}` },
+ },
+ ],
+ }) as const
+ )
+ ),
+ // sync userEvent sync methods with await operator are not valid
+ ...USER_EVENT_SYNC_FUNCTIONS.map(
+ (func) =>
+ ({
+ code: `
import userEvent from '@testing-library/user-event';
test('should report userEvent.${func} sync event awaited', async() => {
await userEvent.${func}('foo');
});
`,
- errors: [
- {
- line: 4,
- column: 17,
- messageId: 'noAwaitSyncEvents',
- data: { name: `userEvent.${func}` },
- },
- ],
- } as const)
- ),
+ options: [{ eventModules: ['user-event'] }],
+ errors: [
+ {
+ line: 4,
+ column: 17,
+ messageId: 'noAwaitSyncEvents',
+ data: { name: `userEvent.${func}` },
+ },
+ ],
+ }) as const
+ ),
- {
- code: `
+ {
+ code: `
import userEvent from '@testing-library/user-event';
test('should report async events without delay awaited', async() => {
await userEvent.type('foo', 'bar');
await userEvent.keyboard('foo');
});
`,
- errors: [
- {
- line: 4,
- column: 17,
- messageId: 'noAwaitSyncEvents',
- data: { name: 'userEvent.type' },
- },
- {
- line: 5,
- column: 17,
- messageId: 'noAwaitSyncEvents',
- data: { name: 'userEvent.keyboard' },
- },
- ],
- },
- {
- code: `
+ options: [{ eventModules: ['user-event'] }],
+ errors: [
+ {
+ line: 4,
+ column: 17,
+ messageId: 'noAwaitSyncEvents',
+ data: { name: 'userEvent.type' },
+ },
+ {
+ line: 5,
+ column: 17,
+ messageId: 'noAwaitSyncEvents',
+ data: { name: 'userEvent.keyboard' },
+ },
+ ],
+ },
+ {
+ code: `
import userEvent from '@testing-library/user-event';
test('should report async events with 0 delay awaited', async() => {
await userEvent.type('foo', 'bar', { delay: 0 });
await userEvent.keyboard('foo', { delay: 0 });
});
`,
- errors: [
- {
- line: 4,
- column: 17,
- messageId: 'noAwaitSyncEvents',
- data: { name: 'userEvent.type' },
- },
- {
- line: 5,
- column: 17,
- messageId: 'noAwaitSyncEvents',
- data: { name: 'userEvent.keyboard' },
- },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ options: [{ eventModules: ['user-event'] }],
+ errors: [
+ {
+ line: 4,
+ column: 17,
+ messageId: 'noAwaitSyncEvents',
+ data: { name: 'userEvent.type' },
+ },
+ {
+ line: 5,
+ column: 17,
+ messageId: 'noAwaitSyncEvents',
+ data: { name: 'userEvent.keyboard' },
+ },
+ ],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { fireEvent as renamedFireEvent } from 'test-utils';
import renamedUserEvent from '@testing-library/user-event';
@@ -268,26 +349,81 @@ ruleTester.run(RULE_NAME, rule, {
await renamedUserEvent.keyboard('foo', { delay: 0 });
});
`,
- errors: [
- {
- line: 6,
- column: 17,
- messageId: 'noAwaitSyncEvents',
- data: { name: 'renamedFireEvent.click' },
- },
- {
- line: 7,
- column: 17,
- messageId: 'noAwaitSyncEvents',
- data: { name: 'renamedUserEvent.type' },
- },
- {
- line: 8,
- column: 17,
- messageId: 'noAwaitSyncEvents',
- data: { name: 'renamedUserEvent.keyboard' },
- },
- ],
- },
- ],
+ options: [{ eventModules: ['user-event', 'fire-event'] }],
+ errors: [
+ {
+ line: 6,
+ column: 17,
+ messageId: 'noAwaitSyncEvents',
+ data: { name: 'renamedFireEvent.click' },
+ },
+ {
+ line: 7,
+ column: 17,
+ messageId: 'noAwaitSyncEvents',
+ data: { name: 'renamedUserEvent.type' },
+ },
+ {
+ line: 8,
+ column: 17,
+ messageId: 'noAwaitSyncEvents',
+ data: { name: 'renamedUserEvent.keyboard' },
+ },
+ ],
+ },
+ {
+ code: `async() => {
+ const delay = 0
+ await userEvent.type('foo', { delay });
+ }
+ `,
+ options: [{ eventModules: ['user-event'] }],
+ errors: [
+ {
+ line: 3,
+ column: 17,
+ messageId: 'noAwaitSyncEvents',
+ data: { name: 'userEvent.type' },
+ },
+ ],
+ },
+ {
+ code: `async() => {
+ const delay = 0
+ const somethingElse = true
+ const skipHover = true
+ await userEvent.type('foo', { delay, skipHover });
+ }
+ `,
+ options: [{ eventModules: ['user-event'] }],
+ errors: [
+ {
+ line: 5,
+ column: 17,
+ messageId: 'noAwaitSyncEvents',
+ data: { name: 'userEvent.type' },
+ },
+ ],
+ },
+ {
+ code: `async() => {
+ let delay = 0
+ const somethingElse = true
+ const skipHover = true
+ delay = 15
+ delay = 0
+ await userEvent.type('foo', { delay, skipHover });
+ }
+ `,
+ options: [{ eventModules: ['user-event'] }],
+ errors: [
+ {
+ line: 7,
+ column: 17,
+ messageId: 'noAwaitSyncEvents',
+ data: { name: 'userEvent.type' },
+ },
+ ],
+ },
+ ],
});
diff --git a/tests/lib/rules/no-await-sync-queries.test.ts b/tests/lib/rules/no-await-sync-queries.test.ts
new file mode 100644
index 00000000..0896f817
--- /dev/null
+++ b/tests/lib/rules/no-await-sync-queries.test.ts
@@ -0,0 +1,274 @@
+import rule, { RULE_NAME } from '../../../lib/rules/no-await-sync-queries';
+import {
+ SYNC_QUERIES_COMBINATIONS,
+ ASYNC_QUERIES_COMBINATIONS,
+} from '../../../lib/utils';
+import { createRuleTester } from '../test-utils';
+
+const ruleTester = createRuleTester();
+
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ '@testing-library/dom',
+ '@testing-library/angular',
+ '@testing-library/react',
+ '@testing-library/vue',
+ '@marko/testing-library',
+];
+
+ruleTester.run(RULE_NAME, rule, {
+ valid: [
+ // sync queries without await are valid
+ ...SYNC_QUERIES_COMBINATIONS.map((query) => ({
+ code: `() => {
+ const element = ${query}('foo')
+ }
+ `,
+ })),
+ // custom sync queries without await are valid
+ `() => {
+ const element = getByIcon('search')
+ }
+ `,
+ `() => {
+ const element = queryByIcon('search')
+ }
+ `,
+ `() => {
+ const element = getAllByIcon('search')
+ }
+ `,
+ `() => {
+ const element = queryAllByIcon('search')
+ }
+ `,
+ `async () => {
+ await waitFor(() => {
+ getByText('search');
+ });
+ }
+ `,
+
+ // awaited custom sync query not matching custom-queries setting is valid
+ {
+ settings: {
+ 'testing-library/custom-queries': ['queryByIcon', 'ByComplexText'],
+ },
+ code: `
+ test('A valid example test', async () => {
+ const element = await getByIcon('search')
+ })
+ `,
+ },
+
+ // sync queries without await inside assert are valid
+ ...SYNC_QUERIES_COMBINATIONS.map((query) => ({
+ code: `() => {
+ expect(${query}('foo')).toBeEnabled()
+ }
+ `,
+ })),
+
+ // async queries with await operator are valid
+ ...ASYNC_QUERIES_COMBINATIONS.map((query) => ({
+ code: `async () => {
+ const element = await ${query}('foo')
+ }
+ `,
+ })),
+
+ // async queries with then method are valid
+ ...ASYNC_QUERIES_COMBINATIONS.map((query) => ({
+ code: `() => {
+ ${query}('foo').then(() => {});
+ }
+ `,
+ })),
+
+ // sync query awaited but not related to custom module is invalid but not reported
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { screen } from 'somewhere-else'
+ () => {
+ const element = await screen.getByRole('button')
+ }
+ `,
+ },
+
+ // https://github.com/testing-library/eslint-plugin-testing-library/issues/276
+ `
+ // sync query within call expression but not part of the callee
+ const chooseElementFromSomewhere = async (text, getAllByLabelText) => {
+ const someElement = getAllByLabelText(text)[0].parentElement;
+ // ...
+ await someOtherAsyncFunction();
+ };
+
+ await chooseElementFromSomewhere('someTextToUseInAQuery', getAllByLabelText);
+ `,
+
+ `// edge case for coverage:
+ // valid use case without call expression
+ // so there is no innermost function scope found
+ await test('edge case for no innermost function scope', () => {
+ const foo = getAllByLabelText
+ })
+ `,
+
+ `// edge case for coverage: CallExpression without deepest Identifier
+ await someList[0]();
+ `,
+
+ `// element is removed
+ test('movie title no longer present in DOM', async () => {
+ await waitForElementToBeRemoved(() => queryByText('the mummy'))
+ })
+ `,
+ ],
+
+ invalid: [
+ // sync queries with await operator are not valid
+ ...SYNC_QUERIES_COMBINATIONS.map(
+ (query) =>
+ ({
+ code: `async () => {
+ const element = await ${query}('foo')
+ }
+ `,
+ errors: [
+ {
+ messageId: 'noAwaitSyncQuery',
+ line: 2,
+ column: 31,
+ },
+ ],
+ }) as const
+ ),
+ // custom sync queries with await operator are not valid
+ {
+ code: `
+ async () => {
+ const element = await getByIcon('search')
+ }
+ `,
+ errors: [{ messageId: 'noAwaitSyncQuery', line: 3, column: 31 }],
+ },
+ {
+ code: `
+ async () => {
+ const element = await queryByIcon('search')
+ }
+ `,
+ errors: [{ messageId: 'noAwaitSyncQuery', line: 3, column: 31 }],
+ },
+ {
+ code: `
+ async () => {
+ const element = await screen.getAllByIcon('search')
+ }
+ `,
+ errors: [{ messageId: 'noAwaitSyncQuery', line: 3, column: 38 }],
+ },
+ {
+ code: `
+ async () => {
+ const element = await screen.queryAllByIcon('search')
+ }
+ `,
+ errors: [{ messageId: 'noAwaitSyncQuery', line: 3, column: 38 }],
+ },
+ // sync queries with await operator inside assert are not valid
+ ...SYNC_QUERIES_COMBINATIONS.map(
+ (query) =>
+ ({
+ code: `async () => {
+ expect(await ${query}('foo')).toBeEnabled()
+ }
+ `,
+ errors: [
+ {
+ messageId: 'noAwaitSyncQuery',
+ line: 2,
+ column: 22,
+ },
+ ],
+ }) as const
+ ),
+
+ // sync queries in screen with await operator are not valid
+ ...SYNC_QUERIES_COMBINATIONS.map(
+ (query) =>
+ ({
+ code: `async () => {
+ const element = await screen.${query}('foo')
+ }
+ `,
+ errors: [
+ {
+ messageId: 'noAwaitSyncQuery',
+ line: 2,
+ column: 38,
+ },
+ ],
+ }) as const
+ ),
+
+ // sync queries in screen with await operator inside assert are not valid
+ ...SYNC_QUERIES_COMBINATIONS.map(
+ (query) =>
+ ({
+ code: `async () => {
+ expect(await screen.${query}('foo')).toBeEnabled()
+ }
+ `,
+ errors: [
+ {
+ messageId: 'noAwaitSyncQuery',
+ line: 2,
+ column: 29,
+ },
+ ],
+ }) as const
+ ),
+
+ // sync query awaited and related to testing library module
+ // with custom module setting is not valid
+ ...SUPPORTED_TESTING_FRAMEWORKS.map(
+ (testingFramework) =>
+ ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { screen } from '${testingFramework}'
+ () => {
+ const element = await screen.getByRole('button')
+ }
+ `,
+ errors: [{ messageId: 'noAwaitSyncQuery', line: 4, column: 38 }],
+ }) as const
+ ),
+ // sync query awaited and related to custom module is not valid
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { screen } from 'test-utils'
+ () => {
+ const element = await screen.getByRole('button')
+ }
+ `,
+ errors: [{ messageId: 'noAwaitSyncQuery', line: 4, column: 38 }],
+ },
+
+ // awaited custom sync query matching custom-queries setting is invalid
+ {
+ settings: {
+ 'testing-library/custom-queries': ['queryByIcon', 'ByComplexText'],
+ },
+ code: `
+ test('A valid example test', async () => {
+ const element = await queryByIcon('search')
+ })
+ `,
+ errors: [{ messageId: 'noAwaitSyncQuery', line: 3, column: 31 }],
+ },
+ ],
+});
diff --git a/tests/lib/rules/no-await-sync-query.test.ts b/tests/lib/rules/no-await-sync-query.test.ts
deleted file mode 100644
index fb01165d..00000000
--- a/tests/lib/rules/no-await-sync-query.test.ts
+++ /dev/null
@@ -1,263 +0,0 @@
-import rule, { RULE_NAME } from '../../../lib/rules/no-await-sync-query';
-import {
- SYNC_QUERIES_COMBINATIONS,
- ASYNC_QUERIES_COMBINATIONS,
-} from '../../../lib/utils';
-import { createRuleTester } from '../test-utils';
-
-const ruleTester = createRuleTester();
-
-ruleTester.run(RULE_NAME, rule, {
- valid: [
- // sync queries without await are valid
- ...SYNC_QUERIES_COMBINATIONS.map((query) => ({
- code: `() => {
- const element = ${query}('foo')
- }
- `,
- })),
- // custom sync queries without await are valid
- `() => {
- const element = getByIcon('search')
- }
- `,
- `() => {
- const element = queryByIcon('search')
- }
- `,
- `() => {
- const element = getAllByIcon('search')
- }
- `,
- `() => {
- const element = queryAllByIcon('search')
- }
- `,
- `async () => {
- await waitFor(() => {
- getByText('search');
- });
- }
- `,
-
- // awaited custom sync query not matching custom-queries setting is valid
- {
- settings: {
- 'testing-library/custom-queries': ['queryByIcon', 'ByComplexText'],
- },
- code: `
- test('A valid example test', async () => {
- const element = await getByIcon('search')
- })
- `,
- },
-
- // sync queries without await inside assert are valid
- ...SYNC_QUERIES_COMBINATIONS.map((query) => ({
- code: `() => {
- expect(${query}('foo')).toBeEnabled()
- }
- `,
- })),
-
- // async queries with await operator are valid
- ...ASYNC_QUERIES_COMBINATIONS.map((query) => ({
- code: `async () => {
- const element = await ${query}('foo')
- }
- `,
- })),
-
- // async queries with then method are valid
- ...ASYNC_QUERIES_COMBINATIONS.map((query) => ({
- code: `() => {
- ${query}('foo').then(() => {});
- }
- `,
- })),
-
- // sync query awaited but not related to custom module is invalid but not reported
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { screen } from 'somewhere-else'
- () => {
- const element = await screen.getByRole('button')
- }
- `,
- },
-
- // https://github.com/testing-library/eslint-plugin-testing-library/issues/276
- `
- // sync query within call expression but not part of the callee
- const chooseElementFromSomewhere = async (text, getAllByLabelText) => {
- const someElement = getAllByLabelText(text)[0].parentElement;
- // ...
- await someOtherAsyncFunction();
- };
-
- await chooseElementFromSomewhere('someTextToUseInAQuery', getAllByLabelText);
- `,
-
- `// edge case for coverage:
- // valid use case without call expression
- // so there is no innermost function scope found
- await test('edge case for no innermost function scope', () => {
- const foo = getAllByLabelText
- })
- `,
-
- `// edge case for coverage: CallExpression without deepest Identifier
- await someList[0]();
- `,
-
- `// element is removed
- test('movie title no longer present in DOM', async () => {
- await waitForElementToBeRemoved(() => queryByText('the mummy'))
- })
- `,
- ],
-
- invalid: [
- // sync queries with await operator are not valid
- ...SYNC_QUERIES_COMBINATIONS.map(
- (query) =>
- ({
- code: `async () => {
- const element = await ${query}('foo')
- }
- `,
- errors: [
- {
- messageId: 'noAwaitSyncQuery',
- line: 2,
- column: 31,
- },
- ],
- } as const)
- ),
- // custom sync queries with await operator are not valid
- {
- code: `
- async () => {
- const element = await getByIcon('search')
- }
- `,
- errors: [{ messageId: 'noAwaitSyncQuery', line: 3, column: 31 }],
- },
- {
- code: `
- async () => {
- const element = await queryByIcon('search')
- }
- `,
- errors: [{ messageId: 'noAwaitSyncQuery', line: 3, column: 31 }],
- },
- {
- code: `
- async () => {
- const element = await screen.getAllByIcon('search')
- }
- `,
- errors: [{ messageId: 'noAwaitSyncQuery', line: 3, column: 38 }],
- },
- {
- code: `
- async () => {
- const element = await screen.queryAllByIcon('search')
- }
- `,
- errors: [{ messageId: 'noAwaitSyncQuery', line: 3, column: 38 }],
- },
- // sync queries with await operator inside assert are not valid
- ...SYNC_QUERIES_COMBINATIONS.map(
- (query) =>
- ({
- code: `async () => {
- expect(await ${query}('foo')).toBeEnabled()
- }
- `,
- errors: [
- {
- messageId: 'noAwaitSyncQuery',
- line: 2,
- column: 22,
- },
- ],
- } as const)
- ),
-
- // sync queries in screen with await operator are not valid
- ...SYNC_QUERIES_COMBINATIONS.map(
- (query) =>
- ({
- code: `async () => {
- const element = await screen.${query}('foo')
- }
- `,
- errors: [
- {
- messageId: 'noAwaitSyncQuery',
- line: 2,
- column: 38,
- },
- ],
- } as const)
- ),
-
- // sync queries in screen with await operator inside assert are not valid
- ...SYNC_QUERIES_COMBINATIONS.map(
- (query) =>
- ({
- code: `async () => {
- expect(await screen.${query}('foo')).toBeEnabled()
- }
- `,
- errors: [
- {
- messageId: 'noAwaitSyncQuery',
- line: 2,
- column: 29,
- },
- ],
- } as const)
- ),
-
- // sync query awaited and related to testing library module
- // with custom module setting is not valid
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { screen } from '@testing-library/react'
- () => {
- const element = await screen.getByRole('button')
- }
- `,
- errors: [{ messageId: 'noAwaitSyncQuery', line: 4, column: 38 }],
- },
- // sync query awaited and related to custom module is not valid
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { screen } from 'test-utils'
- () => {
- const element = await screen.getByRole('button')
- }
- `,
- errors: [{ messageId: 'noAwaitSyncQuery', line: 4, column: 38 }],
- },
-
- // awaited custom sync query matching custom-queries setting is invalid
- {
- settings: {
- 'testing-library/custom-queries': ['queryByIcon', 'ByComplexText'],
- },
- code: `
- test('A valid example test', async () => {
- const element = await queryByIcon('search')
- })
- `,
- errors: [{ messageId: 'noAwaitSyncQuery', line: 3, column: 31 }],
- },
- ],
-});
diff --git a/tests/lib/rules/no-container.test.ts b/tests/lib/rules/no-container.test.ts
index c28d3071..5f3596a6 100644
--- a/tests/lib/rules/no-container.test.ts
+++ b/tests/lib/rules/no-container.test.ts
@@ -3,28 +3,35 @@ import { createRuleTester } from '../test-utils';
const ruleTester = createRuleTester();
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ '@testing-library/angular',
+ '@testing-library/react',
+ '@testing-library/vue',
+ '@marko/testing-library',
+];
+
ruleTester.run(RULE_NAME, rule, {
- valid: [
- {
- code: `
+ valid: [
+ {
+ code: `
render(
);
screen.getByRole('button', {name: /click me/i});
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const { container } = render(
);
expect(container.firstChild).toBeDefined();
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const { container: alias } = render(
);
expect(alias.firstChild).toBeDefined();
`,
- },
- {
- code: `
+ },
+ {
+ code: `
function getExampleDOM() {
const container = document.createElement('div');
container.innerHTML = \`
@@ -41,177 +48,189 @@ ruleTester.run(RULE_NAME, rule, {
const exampleDOM = getExampleDOM();
screen.getByText(exampleDOM, 'Print Username').click();
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const { container: { firstChild } } = render(
);
expect(firstChild).toBeDefined();
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { render as renamed } from '@testing-library/react'
- import { render } from 'somewhere-else'
- const { container } = render(
);
- const button = container.querySelector('.btn-primary');
- `,
- },
- {
- settings: {
- 'testing-library/custom-renders': ['customRender', 'renderWithRedux'],
- },
- code: `
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.map(
+ (testingFramework) =>
+ ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { render as renamed } from '${testingFramework}'
+ import { render } from 'somewhere-else'
+ const { container } = render(
);
+ const button = container.querySelector('.btn-primary');
+ `,
+ }) as const
+ ),
+ {
+ settings: {
+ 'testing-library/custom-renders': ['customRender', 'renderWithRedux'],
+ },
+ code: `
import { otherRender } from 'somewhere-else'
const { container } = otherRender(
);
const button = container.querySelector('.btn-primary');
`,
- },
- ],
- invalid: [
- {
- code: `
+ },
+ ],
+ invalid: [
+ {
+ code: `
const { container } = render(
);
const button = container.querySelector('.btn-primary');
`,
- errors: [
- {
- line: 3,
- column: 24,
- messageId: 'noContainer',
- },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 24,
+ messageId: 'noContainer',
+ },
+ ],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { render } from 'test-utils'
const { container } = render(
);
const button = container.querySelector('.btn-primary');
`,
- errors: [
- {
- line: 4,
- column: 24,
- messageId: 'noContainer',
- },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { render as testingRender } from '@testing-library/react'
+ errors: [
+ {
+ line: 4,
+ column: 24,
+ messageId: 'noContainer',
+ },
+ ],
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.map(
+ (testingFramework) =>
+ ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { render as testingRender } from '${testingFramework}'
const { container: renamed } = testingRender(
);
const button = renamed.querySelector('.btn-primary');
`,
- errors: [
- {
- line: 4,
- column: 24,
- messageId: 'noContainer',
- },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { render } from '@testing-library/react'
-
+ errors: [
+ {
+ line: 4,
+ column: 24,
+ messageId: 'noContainer',
+ },
+ ],
+ }) as const
+ ),
+ ...SUPPORTED_TESTING_FRAMEWORKS.map(
+ (testingFramework) =>
+ ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { render } from '${testingFramework}'
+
const setup = () => render(
)
const { container } = setup()
const button = container.querySelector('.btn-primary');
`,
- errors: [
- {
- line: 7,
- column: 24,
- messageId: 'noContainer',
- },
- ],
- },
- {
- code: `
+ errors: [
+ {
+ line: 7,
+ column: 24,
+ messageId: 'noContainer',
+ },
+ ],
+ }) as const
+ ),
+ {
+ code: `
const { container } = render(
);
container.querySelector();
`,
- errors: [
- {
- line: 3,
- column: 9,
- messageId: 'noContainer',
- },
- ],
- },
- {
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 9,
+ messageId: 'noContainer',
+ },
+ ],
+ },
+ {
+ code: `
const { container: alias } = render(
);
alias.querySelector();
`,
- errors: [
- {
- line: 3,
- column: 9,
- messageId: 'noContainer',
- },
- ],
- },
- {
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 9,
+ messageId: 'noContainer',
+ },
+ ],
+ },
+ {
+ code: `
const view = render(
);
const button = view.container.querySelector('.btn-primary');
`,
- errors: [
- {
- line: 3,
- column: 29,
- messageId: 'noContainer',
- },
- ],
- },
- {
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 29,
+ messageId: 'noContainer',
+ },
+ ],
+ },
+ {
+ code: `
const { container: { querySelector } } = render(
);
querySelector('foo');
`,
- errors: [
- {
- line: 3,
- column: 9,
- messageId: 'noContainer',
- },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { render } from '@testing-library/react'
+ errors: [
+ {
+ line: 3,
+ column: 9,
+ messageId: 'noContainer',
+ },
+ ],
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.map(
+ (testingFramework) =>
+ ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { render } from '${testingFramework}'
const { container: { querySelector } } = render(
);
querySelector('foo');
`,
- errors: [
- {
- line: 4,
- column: 9,
- messageId: 'noContainer',
- },
- ],
- },
- {
- settings: {
- 'testing-library/custom-renders': ['customRender', 'renderWithRedux'],
- },
- code: `
+ errors: [
+ {
+ line: 4,
+ column: 9,
+ messageId: 'noContainer',
+ },
+ ],
+ }) as const
+ ),
+ {
+ settings: {
+ 'testing-library/custom-renders': ['customRender', 'renderWithRedux'],
+ },
+ code: `
const { container } = renderWithRedux(
);
container.querySelector();
`,
- errors: [
- {
- line: 3,
- column: 9,
- messageId: 'noContainer',
- },
- ],
- },
- ],
+ errors: [
+ {
+ line: 3,
+ column: 9,
+ messageId: 'noContainer',
+ },
+ ],
+ },
+ ],
});
diff --git a/tests/lib/rules/no-debugging-utils.test.ts b/tests/lib/rules/no-debugging-utils.test.ts
index 56814597..7d6f1045 100644
--- a/tests/lib/rules/no-debugging-utils.test.ts
+++ b/tests/lib/rules/no-debugging-utils.test.ts
@@ -3,256 +3,266 @@ import { createRuleTester } from '../test-utils';
const ruleTester = createRuleTester();
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ '@testing-library/angular',
+ '@testing-library/react',
+ '@testing-library/vue',
+ '@marko/testing-library',
+];
+
ruleTester.run(RULE_NAME, rule, {
- valid: [
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `debug()`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ valid: [
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `debug()`,
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { screen } from 'somewhere-else'
screen.debug()
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `() => {
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `() => {
const somethingElse = {}
const { debug } = foo()
debug()
}`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
let foo
const debug = require('debug')
debug()
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const { test } = render(
)
test()
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const utils = render(
)
utils.debug
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const utils = render(
)
utils.foo()
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `screen.debug()`,
- },
- {
- code: `console.debug()`,
- },
- {
- code: `
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `screen.debug()`,
+ },
+ {
+ code: `console.debug()`,
+ },
+ {
+ code: `
const consoleDebug = console.debug
consoleDebug()
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const { debug } = console
debug()
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const { debug: consoleDebug } = console
consoleDebug()
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const { screen } = require('@testing-library/dom')
screen.debug
`,
- },
- {
- code: `
+ },
+ {
+ code: `
import { screen } from '@testing-library/dom'
screen.debug
`,
- },
- {
- code: `
+ },
+ {
+ code: `
import { screen } from '@testing-library/dom'
screen.logTestingPlaygroundURL()
`,
- options: [{ utilsToCheckFor: { logTestingPlaygroundURL: false } }],
- },
- {
- code: `
+ options: [{ utilsToCheckFor: { logTestingPlaygroundURL: false } }],
+ },
+ {
+ code: `
import { screen } from '@testing-library/dom'
screen.logTestingPlaygroundURL()
`,
- options: [{ utilsToCheckFor: undefined }],
- },
- {
- code: `const { queries } = require('@testing-library/dom')`,
- },
- {
- code: `import * as dtl from '@testing-library/dom';
+ options: [{ utilsToCheckFor: undefined }],
+ },
+ {
+ code: `const { queries } = require('@testing-library/dom')`,
+ },
+ {
+ code: `import * as dtl from '@testing-library/dom';
const foo = dtl.debug;
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import * as foo from '@somewhere/else';
foo.debug();
`,
- },
- {
- code: `import { queries } from '@testing-library/dom'`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ {
+ code: `import { queries } from '@testing-library/dom'`,
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
const { screen } = require('something-else')
screen.debug()
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { screen } from 'something-else'
screen.debug()
`,
- },
- {
- code: `
+ },
+ {
+ code: `
async function foo() {
const foo = await bar;
}
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { debug as testingDebug } from 'test-utils'
import { debug } from 'somewhere-else'
debug()
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { render as testingRender } from '@testing-library/react'
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.map((testingFramework) => ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { render as testingRender } from '${testingFramework}'
import { render } from 'somewhere-else'
-
+
const { debug } = render(element)
-
+
somethingElse()
debug()
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { render as testingRender } from '@testing-library/react'
+ })),
+ ...SUPPORTED_TESTING_FRAMEWORKS.map((testingFramework) => ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { render as testingRender } from '${testingFramework}'
import { render } from 'somewhere-else'
-
+
const { debug } = render(element)
const { debug: testingDebug } = testingRender(element)
-
+
somethingElse()
debug()
`,
- },
+ })),
- `// cover edge case for https://github.com/testing-library/eslint-plugin-testing-library/issues/306
- thing.method.lastCall.args[0]();
- `,
- ],
+ {
+ code: `
+ // cover edge case for https://github.com/testing-library/eslint-plugin-testing-library/issues/306
+ thing.method.lastCall.args[0]();
+ `,
+ },
+ ],
- invalid: [
- {
- code: `debug()`,
- errors: [{ line: 1, column: 1, messageId: 'noDebug' }],
- },
- {
- code: `
+ invalid: [
+ {
+ code: `debug()`,
+ errors: [{ line: 1, column: 1, messageId: 'noDebug' }],
+ },
+ {
+ code: `
import { screen } from 'aggressive-reporting'
screen.debug()
`,
- errors: [{ line: 3, column: 14, messageId: 'noDebug' }],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ errors: [{ line: 3, column: 14, messageId: 'noDebug' }],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { screen } from 'test-utils'
screen.debug()
`,
- errors: [{ line: 3, column: 14, messageId: 'noDebug' }],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ errors: [{ line: 3, column: 14, messageId: 'noDebug' }],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { debug as testingDebug } from 'test-utils'
testingDebug()
`,
- errors: [{ line: 3, column: 7, messageId: 'noDebug' }],
- },
- {
- code: `
+ errors: [{ line: 3, column: 7, messageId: 'noDebug' }],
+ },
+ {
+ code: `
const { debug } = render(
)
debug()
`,
- errors: [
- {
- line: 3,
- column: 9,
- messageId: 'noDebug',
- },
- ],
- },
- {
- settings: {
- 'testing-library/custom-renders': ['customRender', 'renderWithRedux'],
- },
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 9,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ settings: {
+ 'testing-library/custom-renders': ['customRender', 'renderWithRedux'],
+ },
+ code: `
const { debug } = renderWithRedux(
)
debug()
`,
- errors: [
- {
- line: 3,
- column: 9,
- messageId: 'noDebug',
- },
- ],
- },
- {
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 9,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ code: `
const utils = render(
)
utils.debug()
`,
- errors: [
- {
- line: 3,
- column: 15,
- messageId: 'noDebug',
- },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 15,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { render } from 'test-utils'
const setup = () => render(
)
@@ -260,90 +270,90 @@ ruleTester.run(RULE_NAME, rule, {
const utils = setup()
utils.debug()
`,
- errors: [
- {
- line: 7,
- column: 15,
- messageId: 'noDebug',
- },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `// aggressive reporting disabled
+ errors: [
+ {
+ line: 7,
+ column: 15,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `// aggressive reporting disabled
import { render } from 'test-utils'
const utils = render(
)
utils.debug()
`,
- errors: [
- {
- line: 4,
- column: 15,
- messageId: 'noDebug',
- },
- ],
- },
- {
- code: `
+ errors: [
+ {
+ line: 4,
+ column: 15,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ code: `
const utils = render(
)
utils.debug()
utils.foo()
utils.debug()
`,
- errors: [
- {
- line: 3,
- column: 15,
- messageId: 'noDebug',
- },
- {
- line: 5,
- column: 15,
- messageId: 'noDebug',
- },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `// aggressive reporting disabled
+ errors: [
+ {
+ line: 3,
+ column: 15,
+ messageId: 'noDebug',
+ },
+ {
+ line: 5,
+ column: 15,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `// aggressive reporting disabled
import { render } from 'test-utils'
const utils = render(
)
utils.debug()
utils.foo()
utils.debug()
`,
- errors: [
- {
- line: 4,
- column: 15,
- messageId: 'noDebug',
- },
- {
- line: 6,
- column: 15,
- messageId: 'noDebug',
- },
- ],
- },
- {
- code: `
+ errors: [
+ {
+ line: 4,
+ column: 15,
+ messageId: 'noDebug',
+ },
+ {
+ line: 6,
+ column: 15,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ code: `
describe(() => {
test(async () => {
const { debug } = await render("foo")
debug()
})
})`,
- errors: [
- {
- line: 5,
- column: 11,
- messageId: 'noDebug',
- },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `// aggressive reporting disabled
+ errors: [
+ {
+ line: 5,
+ column: 11,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `// aggressive reporting disabled
import { render } from 'test-utils'
describe(() => {
test(async () => {
@@ -351,33 +361,33 @@ ruleTester.run(RULE_NAME, rule, {
debug()
})
})`,
- errors: [
- {
- line: 6,
- column: 11,
- messageId: 'noDebug',
- },
- ],
- },
- {
- code: `
+ errors: [
+ {
+ line: 6,
+ column: 11,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ code: `
describe(() => {
test(async () => {
const utils = await render("foo")
utils.debug()
})
})`,
- errors: [
- {
- line: 5,
- column: 17,
- messageId: 'noDebug',
- },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `// aggressive reporting disabled
+ errors: [
+ {
+ line: 5,
+ column: 17,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `// aggressive reporting disabled
import { render } from 'test-utils'
describe(() => {
test(async () => {
@@ -385,260 +395,225 @@ ruleTester.run(RULE_NAME, rule, {
utils.debug()
})
})`,
- errors: [
- {
- line: 6,
- column: 17,
- messageId: 'noDebug',
- },
- ],
- },
- {
- code: `
+ errors: [
+ {
+ line: 6,
+ column: 17,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ code: `
const { screen } = require('@testing-library/dom')
screen.debug()
`,
- errors: [
- {
- line: 3,
- column: 16,
- messageId: 'noDebug',
- },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `// aggressive reporting disabled
+ errors: [
+ {
+ line: 3,
+ column: 16,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `// aggressive reporting disabled
const { screen } = require('@testing-library/dom')
screen.debug()
`,
- errors: [
- {
- line: 3,
- column: 16,
- messageId: 'noDebug',
- },
- ],
- },
- {
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 16,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ code: `
import { screen } from '@testing-library/dom'
screen.debug()
`,
- errors: [
- {
- line: 3,
- column: 16,
- messageId: 'noDebug',
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/dom'
- screen.logTestingPlaygroundURL()
- `,
- options: [{ utilsToCheckFor: { logTestingPlaygroundURL: true } }],
- errors: [
- {
- line: 3,
- column: 16,
- messageId: 'noDebug',
- },
- ],
- },
- {
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 16,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ code: `
import { logRoles } from '@testing-library/dom'
logRoles(document.createElement('nav'))
`,
- options: [{ utilsToCheckFor: { logRoles: true } }],
- errors: [
- {
- line: 3,
- column: 9,
- messageId: 'noDebug',
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/dom'
- screen.logTestingPlaygroundURL()
- `,
- options: [{ utilsToCheckFor: { logRoles: true } }],
- errors: [
- {
- line: 3,
- column: 16,
- messageId: 'noDebug',
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/dom'
- screen.logTestingPlaygroundURL()
- `,
- options: [{ utilsToCheckFor: { debug: false } }],
- errors: [
- {
- line: 3,
- column: 16,
- messageId: 'noDebug',
- },
- ],
- },
- {
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 9,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ code: `
import { screen } from '@testing-library/dom'
screen.logTestingPlaygroundURL()
`,
- options: [{ utilsToCheckFor: {} }],
- errors: [
- {
- line: 3,
- column: 16,
- messageId: 'noDebug',
- },
- ],
- },
- {
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 16,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ code: `
import { screen } from '@testing-library/dom'
screen.logTestingPlaygroundURL()
`,
- errors: [
- {
- line: 3,
- column: 16,
- messageId: 'noDebug',
- },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `// aggressive reporting disabled
+ options: [{ utilsToCheckFor: {} }],
+ errors: [
+ {
+ line: 3,
+ column: 16,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `// aggressive reporting disabled
import { screen } from '@testing-library/dom'
screen.debug()
`,
- errors: [
- {
- line: 3,
- column: 16,
- messageId: 'noDebug',
- },
- ],
- },
- {
- // https://github.com/testing-library/eslint-plugin-testing-library/issues/174
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 16,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ // https://github.com/testing-library/eslint-plugin-testing-library/issues/174
+ code: `
import { screen, render } from '@testing-library/dom'
screen.debug()
`,
- errors: [
- {
- line: 3,
- column: 16,
- messageId: 'noDebug',
- },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `// aggressive reporting disabled
+ errors: [
+ {
+ line: 3,
+ column: 16,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `// aggressive reporting disabled
import { screen, render } from '@testing-library/dom'
screen.debug()
`,
- errors: [
- {
- line: 3,
- column: 16,
- messageId: 'noDebug',
- },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 16,
+ messageId: 'noDebug',
+ },
+ ],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import * as dtl from '@testing-library/dom';
dtl.debug();
`,
- errors: [
- {
- messageId: 'noDebug',
- line: 3,
- column: 13,
- },
- ],
- },
- {
- code: `
+ errors: [
+ {
+ messageId: 'noDebug',
+ line: 3,
+ column: 13,
+ },
+ ],
+ },
+ {
+ code: `
import { render } from 'aggressive-reporting'
-
+
const { debug } = render(element)
-
+
somethingElse()
debug()
`,
- errors: [{ line: 7, column: 7, messageId: 'noDebug' }],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { render } from '@testing-library/react'
-
+ errors: [{ line: 7, column: 7, messageId: 'noDebug' }],
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.map(
+ (testingFramework) =>
+ ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { render } from '${testingFramework}'
+
const { debug } = render(element)
-
+
somethingElse()
debug()
`,
- errors: [{ line: 7, column: 7, messageId: 'noDebug' }],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ errors: [{ line: 7, column: 7, messageId: 'noDebug' }],
+ }) as const
+ ),
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { render } from 'test-utils'
-
+
const { debug: renamed } = render(element)
-
+
somethingElse()
renamed()
`,
- errors: [{ line: 7, column: 7, messageId: 'noDebug' }],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { render } from '@testing-library/react'
-
+ errors: [{ line: 7, column: 7, messageId: 'noDebug' }],
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.map(
+ (testingFramework) =>
+ ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { render } from '${testingFramework}'
+
const utils = render(element)
-
+
somethingElse()
utils.debug()
`,
- errors: [{ line: 7, column: 13, messageId: 'noDebug' }],
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- 'testing-library/custom-renders': ['testingRender'],
- },
- code: `// aggressive reporting disabled, custom render set
+ errors: [{ line: 7, column: 13, messageId: 'noDebug' }],
+ }) as const
+ ),
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ 'testing-library/custom-renders': ['testingRender'],
+ },
+ code: `// aggressive reporting disabled, custom render set
import { testingRender } from 'test-utils'
-
+
const { debug: renamedDebug } = testingRender(element)
-
+
somethingElse()
renamedDebug()
`,
- errors: [{ line: 7, column: 7, messageId: 'noDebug' }],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { render } from '@testing-library/react'
-
+ errors: [{ line: 7, column: 7, messageId: 'noDebug' }],
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.map(
+ (testingFramework) =>
+ ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { render } from '${testingFramework}'
+
const utils = render(element)
const { debug: renamedDestructuredDebug } = console
const { debug } = console
@@ -650,7 +625,8 @@ ruleTester.run(RULE_NAME, rule, {
utils.debug()
renamedDestructuredDebug('foo')
`,
- errors: [{ line: 12, column: 13, messageId: 'noDebug' }],
- },
- ],
+ errors: [{ line: 12, column: 13, messageId: 'noDebug' }],
+ }) as const
+ ),
+ ],
});
diff --git a/tests/lib/rules/no-dom-import.test.ts b/tests/lib/rules/no-dom-import.test.ts
index 4dfc0623..e5fbce0c 100644
--- a/tests/lib/rules/no-dom-import.test.ts
+++ b/tests/lib/rules/no-dom-import.test.ts
@@ -3,229 +3,231 @@ import { createRuleTester } from '../test-utils';
const ruleTester = createRuleTester();
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ {
+ configOption: 'angular',
+ oldName: '@testing-library/angular',
+ newName: '@testing-library/angular',
+ },
+ {
+ configOption: 'react',
+ oldName: 'react-testing-library',
+ newName: '@testing-library/react',
+ },
+ {
+ configOption: 'vue',
+ oldName: 'vue-testing-library',
+ newName: '@testing-library/vue',
+ },
+ {
+ configOption: 'marko',
+ oldName: '@marko/testing-library',
+ newName: '@marko/testing-library',
+ },
+];
+
ruleTester.run(RULE_NAME, rule, {
- valid: [
- 'import { foo } from "foo"',
- 'import "foo"',
- 'import { fireEvent } from "react-testing-library"',
- 'import * as testing from "react-testing-library"',
- 'import { fireEvent } from "@testing-library/react"',
- 'import * as testing from "@testing-library/react"',
- 'import "react-testing-library"',
- 'import "@testing-library/react"',
- 'const { foo } = require("foo")',
- 'require("foo")',
- 'require("")',
- 'require()',
- 'const { fireEvent } = require("react-testing-library")',
- 'const { fireEvent } = require("@testing-library/react")',
- 'require("react-testing-library")',
- 'require("@testing-library/react")',
- {
- code: 'import { fireEvent } from "test-utils"',
- settings: { 'testing-library/utils-module': 'test-utils' },
- },
- ],
- invalid: [
- {
- code: 'import { fireEvent } from "dom-testing-library"',
- errors: [
- {
- messageId: 'noDomImport',
- },
- ],
- output: 'import { fireEvent } from "dom-testing-library"',
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
- // case: dom-testing-library imported with custom module setting
- import { fireEvent } from "dom-testing-library"`,
- errors: [
- {
- line: 3,
- messageId: 'noDomImport',
- },
- ],
- output: `
+ valid: [
+ 'import { foo } from "foo"',
+ 'import "foo"',
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap(({ oldName, newName }) =>
+ [oldName, newName !== oldName ? newName : null].flatMap(
+ (testingFramework) => {
+ if (!testingFramework) {
+ return [];
+ }
+
+ return [
+ `import { fireEvent } from "${testingFramework}"`,
+ `import * as testing from "${testingFramework}"`,
+ `import "${testingFramework}"`,
+ ];
+ }
+ )
+ ),
+ 'const { foo } = require("foo")',
+ 'require("foo")',
+ 'require("")',
+ 'require()',
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap(({ oldName, newName }) =>
+ [oldName, newName !== oldName ? newName : null].flatMap(
+ (testingFramework) => {
+ if (!testingFramework) {
+ return [];
+ }
+
+ return [
+ `const { fireEvent } = require("${testingFramework}")`,
+ `const { fireEvent: testing } = require("${testingFramework}")`,
+ `require("${testingFramework}")`,
+ ];
+ }
+ )
+ ),
+ {
+ code: 'import { fireEvent } from "test-utils"',
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ },
+ ],
+ invalid: [
+ {
+ code: 'import { fireEvent } from "dom-testing-library"',
+ errors: [
+ {
+ messageId: 'noDomImport',
+ },
+ ],
+ output: null,
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
// case: dom-testing-library imported with custom module setting
import { fireEvent } from "dom-testing-library"`,
- },
- {
- code: 'import { fireEvent } from "dom-testing-library"',
- options: ['react'],
- errors: [
- {
- messageId: 'noDomImportFramework',
- data: {
- module: 'react-testing-library',
- },
- },
- ],
- output: `import { fireEvent } from "react-testing-library"`,
- },
- // Single quote or double quotes should not be replaced
- {
- code: `import { fireEvent } from 'dom-testing-library'`,
- options: ['react'],
- errors: [
- {
- messageId: 'noDomImportFramework',
- data: {
- module: 'react-testing-library',
- },
- },
- ],
- output: `import { fireEvent } from 'react-testing-library'`,
- },
- {
- code: 'import * as testing from "dom-testing-library"',
- errors: [
- {
- messageId: 'noDomImport',
- },
- ],
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [
+ {
+ line: 3,
+ messageId: 'noDomImport',
+ },
+ ],
+ output: null,
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap(
+ ({ configOption, oldName, newName }) =>
+ [true, false].flatMap((isOldImport) =>
+ // Single quote or double quotes should not be replaced
+ [`'`, `"`].flatMap((quote) => [
+ {
+ code: `const { fireEvent } = require(${quote}${
+ isOldImport ? 'dom-testing-library' : '@testing-library/dom'
+ }${quote})`,
+ options: [configOption],
+ errors: [
+ {
+ data: { module: isOldImport ? oldName : newName },
+ messageId: 'noDomImportFramework',
+ },
+ ],
+ output: `const { fireEvent } = require(${quote}${
+ isOldImport ? oldName : newName
+ }${quote})`,
+ } as const,
+ {
+ code: `import { fireEvent } from ${quote}${
+ isOldImport ? 'dom-testing-library' : '@testing-library/dom'
+ }${quote}`,
+ options: [configOption],
+ errors: [
+ {
+ data: { module: isOldImport ? oldName : newName },
+ messageId: 'noDomImportFramework',
+ },
+ ],
+ output: `import { fireEvent } from ${quote}${
+ isOldImport ? oldName : newName
+ }${quote}`,
+ } as const,
+ ])
+ )
+ ),
+ {
+ code: 'import * as testing from "dom-testing-library"',
+ errors: [{ messageId: 'noDomImport' }],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
// case: dom-testing-library wildcard imported with custom module setting
import * as testing from "dom-testing-library"`,
- errors: [
- {
- line: 3,
- messageId: 'noDomImport',
- },
- ],
- },
- {
- code: 'import { fireEvent } from "@testing-library/dom"',
- errors: [
- {
- messageId: 'noDomImport',
- },
- ],
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [{ line: 3, messageId: 'noDomImport' }],
+ },
+ {
+ code: 'import { fireEvent } from "@testing-library/dom"',
+ errors: [{ messageId: 'noDomImport' }],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
// case: @testing-library/dom imported with custom module setting
import { fireEvent } from "@testing-library/dom"`,
- errors: [
- {
- line: 3,
- messageId: 'noDomImport',
- },
- ],
- },
- {
- code: 'import * as testing from "@testing-library/dom"',
- errors: [
- {
- messageId: 'noDomImport',
- },
- ],
- },
- {
- code: 'import "dom-testing-library"',
- errors: [
- {
- messageId: 'noDomImport',
- },
- ],
- },
- {
- code: 'import "@testing-library/dom"',
- errors: [
- {
- messageId: 'noDomImport',
- },
- ],
- },
- {
- code: 'const { fireEvent } = require("dom-testing-library")',
- errors: [
- {
- messageId: 'noDomImport',
- },
- ],
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [{ line: 3, messageId: 'noDomImport' }],
+ },
+ {
+ code: 'import * as testing from "@testing-library/dom"',
+ errors: [{ messageId: 'noDomImport' }],
+ },
+ {
+ code: 'import "dom-testing-library"',
+ errors: [{ messageId: 'noDomImport' }],
+ },
+ {
+ code: 'import "@testing-library/dom"',
+ errors: [{ messageId: 'noDomImport' }],
+ },
+ {
+ code: 'const { fireEvent } = require("dom-testing-library")',
+ errors: [{ messageId: 'noDomImport' }],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
// case: dom-testing-library required with custom module setting
const { fireEvent } = require("dom-testing-library")`,
- errors: [
- {
- line: 3,
- messageId: 'noDomImport',
- },
- ],
- },
- {
- code: 'const { fireEvent } = require("@testing-library/dom")',
- errors: [
- {
- messageId: 'noDomImport',
- },
- ],
- },
- {
- code: 'const { fireEvent } = require("@testing-library/dom")',
- options: ['vue'],
- errors: [
- {
- messageId: 'noDomImportFramework',
- data: {
- module: '@testing-library/vue',
- },
- },
- ],
- output: 'const { fireEvent } = require("@testing-library/vue")',
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
- // case: @testing-library/dom required with custom module setting
- const { fireEvent } = require("@testing-library/dom")`,
- options: ['vue'],
- errors: [
- {
- messageId: 'noDomImportFramework',
- data: {
- module: '@testing-library/vue',
- },
- },
- ],
- output: `
- // case: @testing-library/dom required with custom module setting
- const { fireEvent } = require("@testing-library/vue")`,
- },
- {
- code: 'require("dom-testing-library")',
- errors: [
- {
- messageId: 'noDomImport',
- },
- ],
- },
- {
- code: 'require("@testing-library/dom")',
- errors: [
- {
- messageId: 'noDomImport',
- },
- ],
- },
- ],
+ errors: [{ line: 3, messageId: 'noDomImport' }],
+ },
+ {
+ code: 'const { fireEvent } = require("@testing-library/dom")',
+ errors: [{ messageId: 'noDomImport' }],
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap(
+ ({ configOption, oldName, newName }) =>
+ [true, false].map(
+ (isOldImport) =>
+ ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ // case: @testing-library/dom required with custom module setting
+ const { fireEvent } = require("${
+ isOldImport ? 'dom-testing-library' : '@testing-library/dom'
+ }")
+ `,
+ options: [configOption],
+ errors: [
+ {
+ data: { module: isOldImport ? oldName : newName },
+ messageId: 'noDomImportFramework',
+ },
+ ],
+ output: `
+ // case: @testing-library/dom required with custom module setting
+ const { fireEvent } = require("${
+ isOldImport ? oldName : newName
+ }")
+ `,
+ }) as const
+ )
+ ),
+ {
+ code: 'require("dom-testing-library")',
+ errors: [{ messageId: 'noDomImport' }],
+ },
+ {
+ code: 'require("@testing-library/dom")',
+ errors: [{ messageId: 'noDomImport' }],
+ },
+ {
+ code: `
+ require("@testing-library/dom");
+ require("@testing-library/react");`,
+ errors: [{ line: 2, messageId: 'noDomImport' }],
+ },
+ {
+ code: `
+ import { render } from '@testing-library/react';
+ import { screen } from '@testing-library/dom';`,
+ errors: [{ line: 3, messageId: 'noDomImport' }],
+ },
+ ],
});
diff --git a/tests/lib/rules/no-global-regexp-flag-in-query.test.ts b/tests/lib/rules/no-global-regexp-flag-in-query.test.ts
new file mode 100644
index 00000000..f885771d
--- /dev/null
+++ b/tests/lib/rules/no-global-regexp-flag-in-query.test.ts
@@ -0,0 +1,224 @@
+import rule, {
+ RULE_NAME,
+} from '../../../lib/rules/no-global-regexp-flag-in-query';
+import { createRuleTester } from '../test-utils';
+
+const ruleTester = createRuleTester();
+
+ruleTester.run(RULE_NAME, rule, {
+ valid: [
+ `
+ import { screen } from '@testing-library/dom'
+ screen.getByText(/hello/)
+ `,
+ `
+ import { screen } from '@testing-library/dom'
+ screen.getByText(/hello/i)
+ `,
+ `
+ import { screen } from '@testing-library/dom'
+ screen.getByText('hello')
+ `,
+
+ `
+ import { screen } from '@testing-library/dom'
+ screen.findByRole('button', {name: /hello/})
+ `,
+ `
+ import { screen } from '@testing-library/dom'
+ screen.findByRole('button', {name: /hello/im})
+ `,
+ `
+ import { screen } from '@testing-library/dom'
+ screen.findByRole('button', {name: 'hello'})
+ `,
+ `
+ const utils = render(
)
+ utils.findByRole('button', {name: /hello/m})
+ `,
+ `
+ const {queryAllByPlaceholderText} = render(
)
+ queryAllByPlaceholderText(/hello/i)
+ `,
+ `
+ import { within } from '@testing-library/dom'
+ within(element).findByRole('button', {name: /hello/i})
+ `,
+ `
+ import { within } from '@testing-library/dom'
+ within(element).queryByText('Hello')
+ `,
+ `
+ const text = 'hello';
+ /hello/g.test(text)
+ text.match(/hello/g)
+ `,
+ `
+ const text = somethingElse()
+ /hello/g.test(text)
+ text.match(/hello/g)
+ `,
+ `
+ import somethingElse from 'somethingElse'
+ somethingElse.lookup(/hello/g)
+ `,
+ `
+ import { screen } from '@testing-library/dom'
+ screen.notAQuery(/hello/g)
+ `,
+ `
+ import { screen } from '@testing-library/dom'
+ screen.notAQuery('button', {name: /hello/g})
+ `,
+ `
+ const utils = render(
)
+ utils.notAQuery('button', {name: /hello/i})
+ `,
+ `
+ const utils = render(
)
+ utils.notAQuery(/hello/i)
+ `,
+
+ // issue #565
+ `
+ import { screen } from "@testing-library/react"
+
+ describe("App", () => {
+ test("is rendered", async () => {
+ await screen.findByText("Hello World", { exact: false });
+ })
+ })
+ `,
+ ],
+ invalid: [
+ {
+ code: `
+ import { screen } from '@testing-library/dom'
+ screen.getByText(/hello/g)`,
+ errors: [
+ {
+ messageId: 'noGlobalRegExpFlagInQuery',
+ line: 3,
+ column: 26,
+ },
+ ],
+ output: `
+ import { screen } from '@testing-library/dom'
+ screen.getByText(/hello/)`,
+ },
+ {
+ code: `
+ import { screen } from '@testing-library/dom'
+ screen.findByRole('button', {name: /hellogg/g})`,
+ errors: [
+ {
+ messageId: 'noGlobalRegExpFlagInQuery',
+ line: 3,
+ column: 44,
+ },
+ ],
+ output: `
+ import { screen } from '@testing-library/dom'
+ screen.findByRole('button', {name: /hellogg/})`,
+ },
+ {
+ code: `
+ import { screen } from '@testing-library/dom'
+ screen.findByRole('button', {otherProp: true, name: /hello/g})`,
+ errors: [
+ {
+ messageId: 'noGlobalRegExpFlagInQuery',
+ line: 3,
+ column: 61,
+ },
+ ],
+ output: `
+ import { screen } from '@testing-library/dom'
+ screen.findByRole('button', {otherProp: true, name: /hello/})`,
+ },
+ {
+ code: `
+ const utils = render(
)
+ utils.findByRole('button', {name: /hello/ig})`,
+ errors: [
+ {
+ messageId: 'noGlobalRegExpFlagInQuery',
+ line: 3,
+ column: 43,
+ },
+ ],
+ output: `
+ const utils = render(
)
+ utils.findByRole('button', {name: /hello/i})`,
+ },
+ {
+ code: `
+ const {queryAllByLabelText} = render(
)
+ queryAllByLabelText(/hello/gi)`,
+ errors: [
+ {
+ messageId: 'noGlobalRegExpFlagInQuery',
+ line: 3,
+ column: 29,
+ },
+ ],
+ output: `
+ const {queryAllByLabelText} = render(
)
+ queryAllByLabelText(/hello/i)`,
+ },
+ {
+ code: `
+ import { within } from '@testing-library/dom'
+ within(element).findByRole('button', {name: /hello/igm})`,
+ errors: [
+ {
+ messageId: 'noGlobalRegExpFlagInQuery',
+ line: 3,
+ column: 53,
+ },
+ ],
+ output: `
+ import { within } from '@testing-library/dom'
+ within(element).findByRole('button', {name: /hello/im})`,
+ },
+ {
+ code: `
+ import { within } from '@testing-library/dom'
+ within(element).queryAllByText(/hello/ig)`,
+ errors: [
+ {
+ messageId: 'noGlobalRegExpFlagInQuery',
+ line: 3,
+ column: 40,
+ },
+ ],
+ output: `
+ import { within } from '@testing-library/dom'
+ within(element).queryAllByText(/hello/i)`,
+ },
+ {
+ code: `
+ const countRegExp = /count/gm
+ const anotherRegExp = /something/mgi
+ expect(screen.getByText(countRegExp)).toBeInTheDocument()
+ expect(screen.getByAllText(anotherRegExp)).toBeInTheDocument()`,
+ errors: [
+ {
+ messageId: 'noGlobalRegExpFlagInQuery',
+ line: 4,
+ column: 28,
+ },
+ {
+ messageId: 'noGlobalRegExpFlagInQuery',
+ line: 5,
+ column: 31,
+ },
+ ],
+ output: `
+ const countRegExp = /count/m
+ const anotherRegExp = /something/mi
+ expect(screen.getByText(countRegExp)).toBeInTheDocument()
+ expect(screen.getByAllText(anotherRegExp)).toBeInTheDocument()`,
+ },
+ ],
+});
diff --git a/tests/lib/rules/no-manual-cleanup.test.ts b/tests/lib/rules/no-manual-cleanup.test.ts
index 73732736..88fc87f6 100644
--- a/tests/lib/rules/no-manual-cleanup.test.ts
+++ b/tests/lib/rules/no-manual-cleanup.test.ts
@@ -4,236 +4,304 @@ import { createRuleTester } from '../test-utils';
const ruleTester = createRuleTester();
const ALL_TESTING_LIBRARIES_WITH_CLEANUP = [
- '@testing-library/preact',
- '@testing-library/react',
- '@testing-library/svelte',
- '@testing-library/vue',
- '@marko/testing-library',
+ '@testing-library/preact',
+ '@testing-library/react',
+ '@testing-library/svelte',
+ '@testing-library/vue',
+ '@marko/testing-library',
];
ruleTester.run(RULE_NAME, rule, {
- valid: [
- {
- code: `import "@testing-library/react"`,
- },
- {
- code: `import { cleanup } from "test-utils"`,
- },
- {
- // Angular Testing Library doesn't have `cleanup` util
- code: `import { cleanup } from "@testing-library/angular"`,
- },
- ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map((lib) => ({
- code: `import { render } from "${lib}"`,
- })),
- ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map((lib) => ({
- code: `import utils from "${lib}"`,
- })),
- ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map((lib) => ({
- code: `
+ valid: [
+ {
+ code: `import "@testing-library/react"`,
+ },
+ {
+ code: `import { cleanup } from "test-utils"`,
+ },
+ {
+ // Angular Testing Library doesn't have `cleanup` util
+ code: `import { cleanup } from "@testing-library/angular"`,
+ },
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map((lib) => ({
+ code: `import { render } from "${lib}"`,
+ })),
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map((lib) => ({
+ code: `import utils from "${lib}"`,
+ })),
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map((lib) => ({
+ code: `
import utils from "${lib}"
utils.render()
`,
- })),
- ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map((lib) => ({
- code: `const { render, within } = require("${lib}")`,
- })),
- {
- code: `const { cleanup } = require("any-other-library")`,
- },
- {
- code: `
+ })),
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map((lib) => ({
+ code: `const { render, within } = require("${lib}")`,
+ })),
+ {
+ code: `const { cleanup } = require("any-other-library")`,
+ },
+ {
+ code: `
const utils = require("any-other-library")
utils.cleanup()
`,
- },
- {
- // For test coverage
- code: `const utils = render("something")`,
- },
- {
- code: `const utils = require(moduleName)`,
- },
- ],
- invalid: [
- ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
- (lib) =>
- ({
- code: `import { render, cleanup } from "${lib}"`,
- errors: [
- {
- line: 1,
- column: 18, // error points to `cleanup`
- messageId: 'noManualCleanup',
- },
- ],
- } as const)
- ),
- ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
- (lib) =>
- ({
- // official testing-library packages should be reported with custom module setting
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import { cleanup, render } from "${lib}"`,
- errors: [
- {
- line: 1,
- column: 10, // error points to `cleanup`
- messageId: 'noManualCleanup',
- },
- ],
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ },
+ {
+ // For test coverage
+ code: `const utils = render("something")`,
+ },
+ {
+ code: `const utils = require(moduleName)`,
+ },
+ ],
+ invalid: [
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
+ (lib) =>
+ ({
+ code: `import { render, cleanup } from "${lib}"`,
+ errors: [
+ {
+ line: 1,
+ column: 18, // error points to `cleanup`
+ messageId: 'noManualCleanup',
+ },
+ ],
+ }) as const
+ ),
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
+ (lib) =>
+ ({
+ code: `
+ import { render, cleanup } from "${lib}"
+ import userEvent from "@testing-library/user-event"
+ `,
+ errors: [
+ {
+ line: 2,
+ column: 24, // error points to `cleanup`
+ messageId: 'noManualCleanup',
+ },
+ ],
+ }) as const
+ ),
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
+ (lib) =>
+ ({
+ code: `
+ import userEvent from "@testing-library/user-event"
+ import { render, cleanup } from "${lib}"
+ `,
+ errors: [
+ {
+ line: 3,
+ column: 24, // error points to `cleanup`
+ messageId: 'noManualCleanup',
+ },
+ ],
+ }) as const
+ ),
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
+ (lib) =>
+ ({
+ // official testing-library packages should be reported with custom module setting
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `import { cleanup, render } from "${lib}"`,
+ errors: [
+ {
+ line: 1,
+ column: 10, // error points to `cleanup`
+ messageId: 'noManualCleanup',
+ },
+ ],
+ }) as const
+ ),
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { render, cleanup } from 'test-utils'
`,
- errors: [{ line: 2, column: 26, messageId: 'noManualCleanup' }],
- },
- ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
- (lib) =>
- ({
- code: `import { cleanup as myCustomCleanup } from "${lib}"`,
- errors: [
- {
- line: 1,
- column: 10, // error points to `cleanup`
- messageId: 'noManualCleanup',
- },
- ],
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [{ line: 2, column: 26, messageId: 'noManualCleanup' }],
+ },
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
+ (lib) =>
+ ({
+ code: `import { cleanup as myCustomCleanup } from "${lib}"`,
+ errors: [
+ {
+ line: 1,
+ column: 10, // error points to `cleanup`
+ messageId: 'noManualCleanup',
+ },
+ ],
+ }) as const
+ ),
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { cleanup as myCustomCleanup } from 'test-utils'
`,
- errors: [{ line: 2, column: 18, messageId: 'noManualCleanup' }],
- },
- ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
- (lib) =>
- ({
- code: `import utils, { cleanup } from "${lib}"`,
- errors: [
- {
- line: 1,
- column: 17, // error points to `cleanup`
- messageId: 'noManualCleanup',
- },
- ],
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [{ line: 2, column: 18, messageId: 'noManualCleanup' }],
+ },
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
+ (lib) =>
+ ({
+ code: `import utils, { cleanup } from "${lib}"`,
+ errors: [
+ {
+ line: 1,
+ column: 17, // error points to `cleanup`
+ messageId: 'noManualCleanup',
+ },
+ ],
+ }) as const
+ ),
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import utils, { cleanup } from 'test-utils'
`,
- errors: [{ line: 2, column: 25, messageId: 'noManualCleanup' }],
- },
- ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
- (lib) =>
- ({
- code: `
+ errors: [{ line: 2, column: 25, messageId: 'noManualCleanup' }],
+ },
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
+ (lib) =>
+ ({
+ code: `
import utils from "${lib}"
afterEach(() => utils.cleanup())
`,
- errors: [
- {
- line: 3,
- column: 31,
- messageId: 'noManualCleanup',
- },
- ],
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 31,
+ messageId: 'noManualCleanup',
+ },
+ ],
+ }) as const
+ ),
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import utils from 'test-utils'
afterEach(() => utils.cleanup())
`,
- errors: [{ line: 3, column: 31, messageId: 'noManualCleanup' }],
- },
- ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
- (lib) =>
- ({
- code: `
+ errors: [{ line: 3, column: 31, messageId: 'noManualCleanup' }],
+ },
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
+ (lib) =>
+ ({
+ code: `
import utils from "${lib}"
afterEach(utils.cleanup)
`,
- errors: [
- {
- line: 3,
- column: 25,
- messageId: 'noManualCleanup',
- },
- ],
- } as const)
- ),
- ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
- (lib) =>
- ({
- code: `const { cleanup } = require("${lib}")`,
- errors: [
- {
- line: 1,
- column: 9, // error points to `cleanup`
- messageId: 'noManualCleanup',
- },
- ],
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 25,
+ messageId: 'noManualCleanup',
+ },
+ ],
+ }) as const
+ ),
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
+ (lib) =>
+ ({
+ code: `const { cleanup } = require("${lib}")`,
+ errors: [
+ {
+ line: 1,
+ column: 9, // error points to `cleanup`
+ messageId: 'noManualCleanup',
+ },
+ ],
+ }) as const
+ ),
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
const { render, cleanup } = require('test-utils')
`,
- errors: [{ line: 2, column: 25, messageId: 'noManualCleanup' }],
- },
- ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
- (lib) =>
- ({
- code: `
+ errors: [{ line: 2, column: 25, messageId: 'noManualCleanup' }],
+ },
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
+ (lib) =>
+ ({
+ code: `
const utils = require("${lib}")
afterEach(() => utils.cleanup())
`,
- errors: [
- {
- line: 3,
- column: 31,
- messageId: 'noManualCleanup',
- },
- ],
- } as const)
- ),
- ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
- (lib) =>
- ({
- code: `
+ errors: [
+ {
+ line: 3,
+ column: 31,
+ messageId: 'noManualCleanup',
+ },
+ ],
+ }) as const
+ ),
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
+ (lib) =>
+ ({
+ code: `
const utils = require("${lib}")
afterEach(utils.cleanup)
`,
- errors: [
- {
- line: 3,
- column: 25,
- messageId: 'noManualCleanup',
- },
- ],
- } as const)
- ),
- ],
+ errors: [
+ {
+ line: 3,
+ column: 25,
+ messageId: 'noManualCleanup',
+ },
+ ],
+ }) as const
+ ),
+ ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(
+ (lib) =>
+ ({
+ code: `
+ import { render } from "${lib}";
+ import { cleanup } from "${lib}";
+ afterEach(cleanup);
+ `,
+ errors: [
+ {
+ line: 3,
+ column: 18,
+ messageId: 'noManualCleanup',
+ },
+ ],
+ }) as const
+ ),
+ {
+ code: `
+ import { cleanup as cleanupVue } from "@testing-library/vue";
+ import { cleanup as cleanupReact } from "@testing-library/react";
+ afterEach(() => { cleanupVue(); cleanupReact(); });
+ `,
+ errors: [
+ {
+ line: 2,
+ column: 14,
+ messageId: 'noManualCleanup',
+ },
+ {
+ line: 3,
+ column: 14,
+ messageId: 'noManualCleanup',
+ },
+ ],
+ },
+ ],
});
diff --git a/tests/lib/rules/no-node-access.test.ts b/tests/lib/rules/no-node-access.test.ts
index 39d172a4..41c29b16 100644
--- a/tests/lib/rules/no-node-access.test.ts
+++ b/tests/lib/rules/no-node-access.test.ts
@@ -1,55 +1,73 @@
-import rule, { RULE_NAME } from '../../../lib/rules/no-node-access';
+import { InvalidTestCase, ValidTestCase } from '@typescript-eslint/rule-tester';
+
+import rule, {
+ RULE_NAME,
+ Options,
+ MessageIds,
+} from '../../../lib/rules/no-node-access';
+import { EVENT_HANDLER_METHODS } from '../../../lib/utils';
import { createRuleTester } from '../test-utils';
const ruleTester = createRuleTester();
+type RuleValidTestCase = ValidTestCase
;
+type RuleInvalidTestCase = InvalidTestCase;
+
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ '@testing-library/angular',
+ '@testing-library/react',
+ '@testing-library/vue',
+ '@marko/testing-library',
+];
+
ruleTester.run(RULE_NAME, rule, {
- valid: [
- {
- code: `
- import { screen } from '@testing-library/react';
-
+ valid: SUPPORTED_TESTING_FRAMEWORKS.flatMap(
+ (testingFramework) => [
+ {
+ code: `
+ import { screen } from '${testingFramework}';
+
const buttonText = screen.getByText('submit');
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { getByText } = screen
const firstChild = getByText('submit');
expect(firstChild).toBeInTheDocument()
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const firstChild = screen.getByText('submit');
expect(firstChild).toBeInTheDocument()
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
-
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
+
const { getByText } = screen;
const button = getByRole('button');
expect(button).toHaveTextContent('submit');
`,
- },
- {
- code: `
- import { render, within } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { render, within } from '${testingFramework}';
const { getByLabelText } = render();
const signInModal = getByLabelText('Sign In');
within(signInModal).getByPlaceholderText('Username');
`,
- },
- {
- code: `
- // case: code not related to testing library at all
+ },
+ {
+ code: `
+ // case: code not related to ${testingFramework} at all
ReactDOM.render(
@@ -63,171 +81,414 @@ ruleTester.run(RULE_NAME, rule, {
document.getElementById('root')
);
`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
- // case: custom module set but not imported (aggressive reporting limited)
+ },
+ {
+ code: `// issue #386 examples, props.children should not be reported
+ import { screen } from '${testingFramework}';
+ jest.mock('@/some/path', () => ({
+ someProperty: jest.fn((props) => props.children),
+ }));
+ `,
+ },
+ {
+ code: `// issue #386 examples
+ import { screen } from '${testingFramework}';
+ function ComponentA(props) {
+ if (props.children) {
+ // ...
+ }
+
+ return {props.children}
+ }
+ `,
+ },
+ {
+ code: `/* related to issue #386 fix
+ * now all node accessing properties (listed in lib/utils/index.ts, in PROPERTIES_RETURNING_NODES)
+ * will not be reported by this rule because anything props.something won't be reported.
+ */
+ import { screen } from '${testingFramework}';
+ function ComponentA(props) {
+ if (props.firstChild) {
+ // ...
+ }
+
+ return {props.nextSibling}
+ }
+ `,
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
+ // case: custom module set but not imported using ${testingFramework} (aggressive reporting limited)
const closestButton = document.getElementById('submit-btn').closest('button');
expect(closestButton).toBeInTheDocument();
`,
- },
- {
- code: `
- // case: without importing TL (aggressive reporting skipped)
+ },
+ {
+ code: `
+ // case: without importing ${testingFramework} (aggressive reporting skipped)
const closestButton = document.getElementById('submit-btn')
expect(closestButton).toBeInTheDocument();
`,
- },
- ],
- invalid: [
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
- // case: importing from custom module (aggressive reporting limited)
+ },
+ {
+ options: [{ allowContainerFirstChild: true }],
+ code: `
+ import { render } from '${testingFramework}';
+
+ const { container } = render()
+
+ expect(container.firstChild).toMatchSnapshot()
+ `,
+ },
+ {
+ // Example from discussions in issue #386
+ code: `
+ import { render } from '${testingFramework}';
+
+ function Wrapper({ children }) {
+ // this should NOT be reported
+ if (children) {
+ // ...
+ }
+
+ // this should NOT be reported
+ return {children}
+ };
+
+ render();
+ expect(screen.getByText('SomeComponent')).toBeInTheDocument();
+ `,
+ },
+ {
+ code: `
+ import userEvent from '@testing-library/user-event';
+ import { screen } from '${testingFramework}';
+
+ const buttonText = screen.getByText('submit');
+ const user = userEvent.setup();
+ user.click(buttonText);
+ `,
+ },
+ {
+ code: `
+ import userEvent from '@testing-library/user-event';
+ import { screen } from '${testingFramework}';
+
+ const buttonText = screen.getByText('submit');
+ const userAlias = userEvent.setup();
+ userAlias.click(buttonText);
+ `,
+ },
+ {
+ code: `
+ import userEvent from '@testing-library/user-event';
+ import { screen } from '${testingFramework}';
+ test('...', () => {
+ const buttonText = screen.getByText('submit');
+ (() => { click: userEvent.click(buttonText); })();
+ });
+ `,
+ },
+ {
+ code: `
+ import userEvent from '@testing-library/user-event';
+ import { screen } from '${testingFramework}';
+
+ const buttonText = screen.getByText('submit');
+ userEvent.setup().click(buttonText);
+ `,
+ },
+ {
+ code: `
+ import userEvt from '@testing-library/user-event';
+ import { screen } from '${testingFramework}';
+
+ const buttonText = screen.getByText('submit');
+ const userAlias = userEvt.setup();
+ userAlias.click(buttonText);
+ `,
+ },
+ {
+ code: `
+ import userEvt from '@testing-library/user-event';
+ import { screen } from '${testingFramework}';
+
+ const buttonText = screen.getByText('submit');
+ userEvt.click(buttonText);
+ `,
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
+ import userEvent from '@testing-library/user-event';
+
+ describe('Testing', () => {
+ let user;
+
+ beforeEach(() => {
+ user = userEvent.setup();
+ });
+
+ it('test 1', async () => {
+ await user.click(screen.getByRole('button'));
+ });
+ });
+ `,
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ // case: custom module set but not imported using ${testingFramework} (aggressive reporting limited)
+ import { screen, userEvent } from 'test-utils';
+
+ describe('Testing', () => {
+ let user;
+
+ beforeEach(() => {
+ user = userEvent.setup();
+ });
+
+ it('test 1', async () => {
+ await user.click(screen.getByRole('button'));
+ });
+ });
+ `,
+ },
+ {
+ code: `
+ import { screen, fireEvent as fe } from '${testingFramework}';
+
+ const buttonText = screen.getByText('submit');
+ fe.click(buttonText);
+ `,
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ // case: custom module set but not imported using ${testingFramework} (aggressive reporting limited)
+ import { screen, fireEvent as fe } from 'test-utils';
+
+ const buttonText = screen.getByText('submit');
+ fe.click(buttonText);
+ `,
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ // case: custom module set but not imported using ${testingFramework} (aggressive reporting limited)
+ import { screen, fireEvent } from '../test-utils';
+
+ const buttonText = screen.getByText('submit');
+ fireEvent.click(buttonText);
+ `,
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
+
+ const ui = {
+ select: screen.getByRole('combobox', {name: 'Test label'}),
+ };
+ test('...', () => {
+ const select = ui.select.get();
+ expect(select).toHaveClass(selectClasses.select);
+ });
+ `,
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ // case: custom module set but not imported using ${testingFramework} (aggressive reporting limited)
+ import { screen, render } from 'test-utils';
+ import MyComponent from './MyComponent'
+
+ test('...', async () => {
+ const { user } = render()
+ await user.click(screen.getByRole("button"))
+ });
+ `,
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ // case: custom module set but not imported using ${testingFramework} (aggressive reporting limited)
+ import { screen, render } from 'test-utils';
+ import MyComponent from './MyComponent'
+
+ test('...', async () => {
+ const result = render()
+ await result.user.click(screen.getByRole("button"))
+ });
+ `,
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'TestUtils',
+ 'testing-library/custom-renders': ['renderComponent'],
+ },
+ code: `
+ // case: custom module set but not imported using ${testingFramework} (aggressive reporting limited)
+ import { screen, renderComponent } from './TestUtils';
+ import MyComponent from './MyComponent'
+
+ test('...', async () => {
+ const result = renderComponent()
+ await result.user.click(screen.getByRole("button"))
+ });
+ `,
+ },
+ ]
+ ),
+ invalid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
+ // case: importing from custom module for ${testingFramework} (aggressive reporting limited)
import 'test-utils';
const closestButton = document.getElementById('submit-btn')
expect(closestButton).toBeInTheDocument();
`,
- errors: [{ line: 4, column: 38, messageId: 'noNodeAccess' }],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
-
+ errors: [{ line: 4, column: 38, messageId: 'noNodeAccess' }],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
+
const button = document.getElementById('submit-btn').closest('button');
`,
- errors: [
- {
- line: 4,
- column: 33,
- messageId: 'noNodeAccess',
- },
- {
- line: 4,
- column: 62,
- messageId: 'noNodeAccess',
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ line: 4,
+ column: 33,
+ messageId: 'noNodeAccess',
+ },
+ {
+ line: 4,
+ column: 62,
+ messageId: 'noNodeAccess',
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
document.getElementById('submit-btn');
`,
- errors: [
- {
- line: 4,
- column: 18,
- messageId: 'noNodeAccess',
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
-
+ errors: [
+ {
+ line: 4,
+ column: 18,
+ messageId: 'noNodeAccess',
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
+
screen.getByText('submit').closest('button');
`,
- errors: [
- {
- // error points to `closest`
- line: 4,
- column: 36,
- messageId: 'noNodeAccess',
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
-
+ errors: [
+ {
+ // error points to `closest`
+ line: 4,
+ column: 36,
+ messageId: 'noNodeAccess',
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
+
expect(screen.getByText('submit').closest('button').textContent).toBe('Submit');
`,
- errors: [
- {
- line: 4,
- column: 43,
- messageId: 'noNodeAccess',
- },
- ],
- },
- {
- code: `
- import { render } from '@testing-library/react';
-
+ errors: [
+ {
+ line: 4,
+ column: 43,
+ messageId: 'noNodeAccess',
+ },
+ ],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
+
const { getByText } = render()
getByText('submit').closest('button');
`,
- errors: [{ line: 5, column: 29, messageId: 'noNodeAccess' }],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [{ line: 5, column: 29, messageId: 'noNodeAccess' }],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const buttons = screen.getAllByRole('button');
const childA = buttons[1].firstChild;
const button = buttons[2];
button.lastChild
`,
- errors: [
- {
- // error points to `firstChild`
- line: 5,
- column: 35,
- messageId: 'noNodeAccess',
- },
- {
- // error points to `lastChild`
- line: 7,
- column: 16,
- messageId: 'noNodeAccess',
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
-
+ errors: [
+ {
+ // error points to `firstChild`
+ line: 5,
+ column: 35,
+ messageId: 'noNodeAccess',
+ },
+ {
+ // error points to `lastChild`
+ line: 7,
+ column: 16,
+ messageId: 'noNodeAccess',
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
+
const buttonText = screen.getByText('submit');
const button = buttonText.closest('button');
`,
- errors: [{ line: 5, column: 35, messageId: 'noNodeAccess' }],
- },
- {
- code: `
- import { render } from '@testing-library/react';
-
+ errors: [{ line: 5, column: 35, messageId: 'noNodeAccess' }],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
+
const { getByText } = render()
const buttonText = getByText('submit');
const button = buttonText.closest('button');
`,
- errors: [
- {
- line: 6,
- column: 35,
- messageId: 'noNodeAccess',
- },
- ],
- },
- {
- code: `
- import { render } from '@testing-library/react';
-
+ errors: [
+ {
+ line: 6,
+ column: 35,
+ messageId: 'noNodeAccess',
+ },
+ ],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
+
const { getByText } = render()
const button = getByText('submit').closest('button');
`,
- errors: [{ line: 5, column: 44, messageId: 'noNodeAccess' }],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
-
+ errors: [{ line: 5, column: 44, messageId: 'noNodeAccess' }],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
+
function getExampleDOM() {
const container = document.createElement('div');
container.innerHTML = \`
@@ -245,18 +506,18 @@ ruleTester.run(RULE_NAME, rule, {
const buttons = screen.getAllByRole(exampleDOM, 'button');
const buttonText = buttons[1].firstChild;
`,
- errors: [
- {
- // error points to `firstChild`
- line: 19,
- column: 39,
- messageId: 'noNodeAccess',
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ // error points to `firstChild`
+ line: 19,
+ column: 39,
+ messageId: 'noNodeAccess',
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
function getExampleDOM() {
const container = document.createElement('div');
@@ -275,14 +536,90 @@ ruleTester.run(RULE_NAME, rule, {
const submitButton = screen.getByText(exampleDOM, 'Submit');
const previousButton = submitButton.previousSibling;
`,
- errors: [
- {
- // error points to `previousSibling`
- line: 19,
- column: 45,
- messageId: 'noNodeAccess',
- },
- ],
- },
- ],
+ errors: [
+ {
+ // error points to `previousSibling`
+ line: 19,
+ column: 45,
+ messageId: 'noNodeAccess',
+ },
+ ],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
+
+ const { container } = render()
+
+ expect(container.firstChild).toMatchSnapshot()
+ `,
+ errors: [
+ {
+ // error points to `firstChild`
+ line: 6,
+ column: 26,
+ messageId: 'noNodeAccess',
+ },
+ ],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
+
+ const { container } = render()
+
+ expect(container.childElementCount).toBe(2)
+ `,
+ errors: [
+ {
+ // error points to `childElementCount`
+ line: 6,
+ column: 26,
+ messageId: 'noNodeAccess',
+ },
+ ],
+ },
+ ...EVENT_HANDLER_METHODS.flatMap((method) => [
+ {
+ code: `
+ import { screen } from '${testingFramework}';
+
+ const button = document.getElementById('submit-btn').${method}();
+ `,
+ errors: [
+ {
+ line: 4,
+ column: 33,
+ messageId: 'noNodeAccess',
+ },
+ {
+ line: 4,
+ column: 62,
+ messageId: 'noNodeAccess',
+ },
+ ],
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ // case: custom module set but not imported using ${testingFramework} (aggressive reporting limited)
+ import { screen } from 'test-utils';
+
+ const button = document.getElementById('submit-btn').${method}();
+ `,
+ errors: [
+ {
+ line: 5,
+ column: 33,
+ messageId: 'noNodeAccess',
+ },
+ {
+ line: 5,
+ column: 62,
+ messageId: 'noNodeAccess',
+ },
+ ],
+ },
+ ]),
+ ]),
});
diff --git a/tests/lib/rules/no-promise-in-fire-event.test.ts b/tests/lib/rules/no-promise-in-fire-event.test.ts
index 05425dbf..82800e3f 100644
--- a/tests/lib/rules/no-promise-in-fire-event.test.ts
+++ b/tests/lib/rules/no-promise-in-fire-event.test.ts
@@ -3,173 +3,188 @@ import { createRuleTester } from '../test-utils';
const ruleTester = createRuleTester();
-ruleTester.run(RULE_NAME, rule, {
- valid: [
- {
- code: `
- import {fireEvent} from '@testing-library/foo';
-
- fireEvent.click(screen.getByRole('button'))
- `,
- },
- {
- code: `
- import {fireEvent} from '@testing-library/foo';
-
- fireEvent.click(queryByRole('button'))`,
- },
- {
- code: `
- import {fireEvent} from '@testing-library/foo';
-
- fireEvent.click(someRef)`,
- },
- {
- code: `
- import {fireEvent} from '@testing-library/foo';
-
- fireEvent.click(await screen.findByRole('button'))
- `,
- },
- {
- code: `
- import {fireEvent} from '@testing-library/foo'
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ '@testing-library/dom',
+ '@testing-library/angular',
+ '@testing-library/react',
+ '@testing-library/vue',
+ '@marko/testing-library',
+];
- const elementPromise = screen.findByRole('button')
- const button = await elementPromise
- fireEvent.click(button)`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `// invalid usage but aggressive reporting opted-out
+ruleTester.run(RULE_NAME, rule, {
+ valid: [
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ code: `
+ import {fireEvent} from '${testingFramework}';
+
+ fireEvent.click(screen.getByRole('button'))
+ `,
+ },
+ {
+ code: `
+ import {fireEvent} from '${testingFramework}';
+
+ fireEvent.click(queryByRole('button'))
+ `,
+ },
+ {
+ code: `
+ import {fireEvent} from '${testingFramework}';
+
+ fireEvent.click(someRef)
+ `,
+ },
+ {
+ code: `
+ import {fireEvent} from '${testingFramework}';
+
+ fireEvent.click(await screen.findByRole('button'))
+ `,
+ },
+ {
+ code: `
+ import {fireEvent} from '${testingFramework}'
+
+ const elementPromise = screen.findByRole('button')
+ const button = await elementPromise
+ fireEvent.click(button)
+ `,
+ },
+ ]),
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `// invalid usage but aggressive reporting opted-out
import { fireEvent } from 'somewhere-else'
fireEvent.click(findByText('submit'))
`,
- },
- `// edge case for coverage:
+ },
+ `// edge case for coverage:
// valid use case without call expression
// so there is no innermost function scope found
test('edge case for no innermost function scope', () => {
const click = fireEvent.click
})
`,
- `// edge case for coverage:
+ `// edge case for coverage:
// new expression of something else than Promise
fireEvent.click(new SomeElement())
`,
- ],
- invalid: [
- {
- // aggressive reporting opted-in
- code: `fireEvent.click(findByText('submit'))`,
- errors: [
- {
- messageId: 'noPromiseInFireEvent',
- line: 1,
- column: 17,
- endColumn: 37,
- },
- ],
- },
- {
- // aggressive reporting opted-in
- code: `fireEvent.click(Promise())`,
- errors: [
- {
- messageId: 'noPromiseInFireEvent',
- line: 1,
- column: 17,
- endColumn: 26,
- },
- ],
- },
- {
- code: `
- import {fireEvent} from '@testing-library/foo';
+ ],
+ invalid: [
+ {
+ // aggressive reporting opted-in
+ code: `fireEvent.click(findByText('submit'))`,
+ errors: [
+ {
+ messageId: 'noPromiseInFireEvent',
+ line: 1,
+ column: 17,
+ endColumn: 37,
+ },
+ ],
+ },
+ {
+ // aggressive reporting opted-in
+ code: `fireEvent.click(Promise())`,
+ errors: [
+ {
+ messageId: 'noPromiseInFireEvent',
+ line: 1,
+ column: 17,
+ endColumn: 26,
+ },
+ ],
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ code: `
+ import {fireEvent} from '${testingFramework}';
const promise = new Promise();
fireEvent.click(promise)`,
- errors: [
- {
- messageId: 'noPromiseInFireEvent',
- line: 5,
- column: 25,
- endColumn: 32,
- },
- ],
- },
- {
- code: `
- import {fireEvent} from '@testing-library/foo'
+ errors: [
+ {
+ messageId: 'noPromiseInFireEvent',
+ line: 5,
+ column: 25,
+ endColumn: 32,
+ },
+ ],
+ } as const,
+ {
+ code: `
+ import {fireEvent} from '${testingFramework}'
const elementPromise = screen.findByRole('button')
fireEvent.click(elementPromise)`,
- errors: [
- {
- messageId: 'noPromiseInFireEvent',
- line: 5,
- column: 25,
- endColumn: 39,
- },
- ],
- },
- {
- code: `
- import {fireEvent} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'noPromiseInFireEvent',
+ line: 5,
+ column: 25,
+ endColumn: 39,
+ },
+ ],
+ } as const,
+ {
+ code: `
+ import {fireEvent} from '${testingFramework}';
fireEvent.click(screen.findByRole('button'))`,
- errors: [
- {
- messageId: 'noPromiseInFireEvent',
- line: 4,
- column: 25,
- endColumn: 52,
- },
- ],
- },
- {
- code: `
- import {fireEvent} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'noPromiseInFireEvent',
+ line: 4,
+ column: 25,
+ endColumn: 52,
+ },
+ ],
+ } as const,
+ {
+ code: `
+ import {fireEvent} from '${testingFramework}';
fireEvent.click(findByText('submit'))`,
- errors: [
- {
- messageId: 'noPromiseInFireEvent',
- line: 4,
- column: 25,
- endColumn: 45,
- },
- ],
- },
- {
- code: `
- import {fireEvent} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'noPromiseInFireEvent',
+ line: 4,
+ column: 25,
+ endColumn: 45,
+ },
+ ],
+ } as const,
+ {
+ code: `
+ import {fireEvent} from '${testingFramework}';
fireEvent.click(Promise('foo'))`,
- errors: [
- {
- messageId: 'noPromiseInFireEvent',
- line: 4,
- column: 25,
- endColumn: 39,
- },
- ],
- },
- {
- code: `
- import {fireEvent} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'noPromiseInFireEvent',
+ line: 4,
+ column: 25,
+ endColumn: 39,
+ },
+ ],
+ } as const,
+ {
+ code: `
+ import {fireEvent} from '${testingFramework}';
fireEvent.click(new Promise('foo'))`,
- errors: [
- {
- messageId: 'noPromiseInFireEvent',
- line: 4,
- column: 25,
- endColumn: 43,
- },
- ],
- },
- ],
+ errors: [
+ {
+ messageId: 'noPromiseInFireEvent',
+ line: 4,
+ column: 25,
+ endColumn: 43,
+ },
+ ],
+ } as const,
+ ]),
+ ],
});
diff --git a/tests/lib/rules/no-render-in-lifecycle.test.ts b/tests/lib/rules/no-render-in-lifecycle.test.ts
new file mode 100644
index 00000000..1561b394
--- /dev/null
+++ b/tests/lib/rules/no-render-in-lifecycle.test.ts
@@ -0,0 +1,280 @@
+import rule, { RULE_NAME } from '../../../lib/rules/no-render-in-lifecycle';
+import { TESTING_FRAMEWORK_SETUP_HOOKS } from '../../../lib/utils';
+import { createRuleTester } from '../test-utils';
+
+const ruleTester = createRuleTester();
+
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ '@testing-library/angular',
+ '@testing-library/react',
+ '@testing-library/vue',
+ '@marko/testing-library',
+];
+
+ruleTester.run(RULE_NAME, rule, {
+ valid: [
+ ...SUPPORTED_TESTING_FRAMEWORKS.map((testingFramework) => ({
+ code: `
+ import { render } from '${testingFramework}';
+
+ beforeAll(() => {
+ doOtherStuff();
+ });
+
+ beforeEach(() => {
+ doSomethingElse();
+ });
+
+ it('Test', () => {
+ render()
+ })
+ `,
+ })),
+ // test config options
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ code: `
+ import { render } from '${testingFramework}';
+ beforeAll(() => {
+ render();
+ });
+ `,
+ options: [{ allowTestingFrameworkSetupHook: 'beforeAll' }],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
+ beforeEach(() => {
+ render();
+ });
+ `,
+ options: [{ allowTestingFrameworkSetupHook: 'beforeEach' }],
+ },
+ ]),
+ ...TESTING_FRAMEWORK_SETUP_HOOKS.map((setupHook) => ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { render } from 'imNoTestingLibrary';
+ ${setupHook}(() => {
+ render()
+ })
+ `,
+ })),
+ ...TESTING_FRAMEWORK_SETUP_HOOKS.map((allowedSetupHook) => {
+ const [disallowedHook] = TESTING_FRAMEWORK_SETUP_HOOKS.filter(
+ (setupHook) => setupHook !== allowedSetupHook
+ );
+ return {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ 'testing-library/custom-renders': ['show', 'renderWithRedux'],
+ },
+ code: `
+ import utils from 'imNoTestingLibrary';
+ import { show } from '../test-utils';
+ ${allowedSetupHook}(() => {
+ show()
+ })
+ ${disallowedHook}(() => {
+ utils.render()
+ })
+ `,
+ options: [
+ {
+ allowTestingFrameworkSetupHook: allowedSetupHook,
+ },
+ ],
+ };
+ }),
+ ...TESTING_FRAMEWORK_SETUP_HOOKS.map((setupHook) => ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ const { render } = require('imNoTestingLibrary')
+
+ ${setupHook}(() => {
+ render()
+ })
+ `,
+ errors: [
+ {
+ messageId: 'noRenderInSetup',
+ },
+ ],
+ })),
+ ],
+
+ invalid: [
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ ...TESTING_FRAMEWORK_SETUP_HOOKS.map(
+ (setupHook) =>
+ ({
+ code: `
+ import { render } from '${testingFramework}';
+ ${setupHook}(() => {
+ render()
+ })
+ `,
+ errors: [
+ {
+ line: 4,
+ column: 11,
+ messageId: 'noRenderInSetup',
+ },
+ ],
+ }) as const
+ ),
+ ...TESTING_FRAMEWORK_SETUP_HOOKS.map(
+ (setupHook) =>
+ ({
+ code: `
+ import { render } from '${testingFramework}';
+ ${setupHook}(function() {
+ render()
+ })
+ `,
+ errors: [
+ {
+ line: 4,
+ column: 11,
+ messageId: 'noRenderInSetup',
+ },
+ ],
+ }) as const
+ ),
+ ]),
+ // custom render function
+ ...TESTING_FRAMEWORK_SETUP_HOOKS.map(
+ (setupHook) =>
+ ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ 'testing-library/custom-renders': ['show', 'renderWithRedux'],
+ },
+ code: `
+ import { show } from '../test-utils';
+
+ ${setupHook}(() => {
+ show()
+ })
+ `,
+ errors: [
+ {
+ line: 5,
+ column: 11,
+ messageId: 'noRenderInSetup',
+ },
+ ],
+ }) as const
+ ),
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ ...TESTING_FRAMEWORK_SETUP_HOOKS.map(
+ (setupHook) =>
+ ({
+ code: `// call render within a wrapper function
+ import { render } from '${testingFramework}';
+
+ const wrapper = () => render()
+
+ ${setupHook}(() => {
+ wrapper()
+ })
+ `,
+ errors: [
+ {
+ line: 7,
+ column: 9,
+ messageId: 'noRenderInSetup',
+ },
+ ],
+ }) as const
+ ),
+ ...TESTING_FRAMEWORK_SETUP_HOOKS.map((allowedSetupHook) => {
+ const [disallowedHook] = TESTING_FRAMEWORK_SETUP_HOOKS.filter(
+ (setupHook) => setupHook !== allowedSetupHook
+ );
+ return {
+ code: `
+ import { render } from '${testingFramework}';
+ ${disallowedHook}(() => {
+ render()
+ })
+ `,
+ options: [
+ {
+ allowTestingFrameworkSetupHook: allowedSetupHook,
+ },
+ ],
+ errors: [
+ {
+ line: 4,
+ column: 13,
+ messageId: 'noRenderInSetup',
+ },
+ ],
+ } as const;
+ }),
+ ...TESTING_FRAMEWORK_SETUP_HOOKS.map(
+ (setupHook) =>
+ ({
+ code: `
+ import * as testingLibrary from '${testingFramework}';
+ ${setupHook}(() => {
+ testingLibrary.render()
+ })
+ `,
+ errors: [
+ {
+ line: 4,
+ column: 26,
+ messageId: 'noRenderInSetup',
+ },
+ ],
+ }) as const
+ ),
+ ]),
+ ...TESTING_FRAMEWORK_SETUP_HOOKS.map(
+ (setupHook) =>
+ ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { render } from 'imNoTestingLibrary';
+ import * as testUtils from '../test-utils';
+ ${setupHook}(() => {
+ testUtils.renderWithRedux()
+ })
+ it('Test', () => {
+ render()
+ })
+ `,
+ errors: [
+ {
+ line: 5,
+ column: 21,
+ messageId: 'noRenderInSetup',
+ },
+ ],
+ }) as const
+ ),
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) =>
+ TESTING_FRAMEWORK_SETUP_HOOKS.map(
+ (setupHook) =>
+ ({
+ code: `
+ const { render } = require('${testingFramework}')
+
+ ${setupHook}(() => {
+ render()
+ })
+ `,
+ errors: [
+ {
+ line: 5,
+ column: 11,
+ messageId: 'noRenderInSetup',
+ },
+ ],
+ }) as const
+ )
+ ),
+ ],
+});
diff --git a/tests/lib/rules/no-render-in-setup.test.ts b/tests/lib/rules/no-render-in-setup.test.ts
deleted file mode 100644
index 4db0796a..00000000
--- a/tests/lib/rules/no-render-in-setup.test.ts
+++ /dev/null
@@ -1,265 +0,0 @@
-import rule, { RULE_NAME } from '../../../lib/rules/no-render-in-setup';
-import { TESTING_FRAMEWORK_SETUP_HOOKS } from '../../../lib/utils';
-import { createRuleTester } from '../test-utils';
-
-const ruleTester = createRuleTester();
-
-ruleTester.run(RULE_NAME, rule, {
- valid: [
- {
- code: `
- import { render } from '@testing-library/foo';
-
- beforeAll(() => {
- doOtherStuff();
- });
-
- beforeEach(() => {
- doSomethingElse();
- });
-
- it('Test', () => {
- render()
- })
- `,
- },
- // test config options
- {
- code: `
- import { render } from '@testing-library/foo';
- beforeAll(() => {
- render();
- });
- `,
- options: [{ allowTestingFrameworkSetupHook: 'beforeAll' }],
- },
- {
- code: `
- import { render } from '@testing-library/foo';
- beforeEach(() => {
- render();
- });
- `,
- options: [{ allowTestingFrameworkSetupHook: 'beforeEach' }],
- },
- ...TESTING_FRAMEWORK_SETUP_HOOKS.map((setupHook) => ({
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { render } from 'imNoTestingLibrary';
- ${setupHook}(() => {
- render()
- })
- `,
- })),
- ...TESTING_FRAMEWORK_SETUP_HOOKS.map((allowedSetupHook) => {
- const [disallowedHook] = TESTING_FRAMEWORK_SETUP_HOOKS.filter(
- (setupHook) => setupHook !== allowedSetupHook
- );
- return {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- 'testing-library/custom-renders': ['show', 'renderWithRedux'],
- },
- code: `
- import utils from 'imNoTestingLibrary';
- import { show } from '../test-utils';
- ${allowedSetupHook}(() => {
- show()
- })
- ${disallowedHook}(() => {
- utils.render()
- })
- `,
- options: [
- {
- allowTestingFrameworkSetupHook: allowedSetupHook,
- },
- ],
- };
- }),
- ...TESTING_FRAMEWORK_SETUP_HOOKS.map((setupHook) => ({
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- const { render } = require('imNoTestingLibrary')
-
- ${setupHook}(() => {
- render()
- })
- `,
- errors: [
- {
- messageId: 'noRenderInSetup',
- },
- ],
- })),
- ],
-
- invalid: [
- ...TESTING_FRAMEWORK_SETUP_HOOKS.map(
- (setupHook) =>
- ({
- code: `
- import { render } from '@testing-library/foo';
- ${setupHook}(() => {
- render()
- })
- `,
- errors: [
- {
- line: 4,
- column: 11,
- messageId: 'noRenderInSetup',
- },
- ],
- } as const)
- ),
- ...TESTING_FRAMEWORK_SETUP_HOOKS.map(
- (setupHook) =>
- ({
- code: `
- import { render } from '@testing-library/foo';
- ${setupHook}(function() {
- render()
- })
- `,
- errors: [
- {
- line: 4,
- column: 11,
- messageId: 'noRenderInSetup',
- },
- ],
- } as const)
- ),
- // custom render function
- ...TESTING_FRAMEWORK_SETUP_HOOKS.map(
- (setupHook) =>
- ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- 'testing-library/custom-renders': ['show', 'renderWithRedux'],
- },
- code: `
- import { show } from '../test-utils';
-
- ${setupHook}(() => {
- show()
- })
- `,
- errors: [
- {
- line: 5,
- column: 11,
- messageId: 'noRenderInSetup',
- },
- ],
- } as const)
- ),
- ...TESTING_FRAMEWORK_SETUP_HOOKS.map(
- (setupHook) =>
- ({
- code: `// call render within a wrapper function
- import { render } from '@testing-library/foo';
-
- const wrapper = () => render()
-
- ${setupHook}(() => {
- wrapper()
- })
- `,
- errors: [
- {
- line: 7,
- column: 9,
- messageId: 'noRenderInSetup',
- },
- ],
- } as const)
- ),
- ...TESTING_FRAMEWORK_SETUP_HOOKS.map((allowedSetupHook) => {
- const [disallowedHook] = TESTING_FRAMEWORK_SETUP_HOOKS.filter(
- (setupHook) => setupHook !== allowedSetupHook
- );
- return {
- code: `
- import { render } from '@testing-library/foo';
- ${disallowedHook}(() => {
- render()
- })
- `,
- options: [
- {
- allowTestingFrameworkSetupHook: allowedSetupHook,
- },
- ],
- errors: [
- {
- line: 4,
- column: 13,
- messageId: 'noRenderInSetup',
- },
- ],
- } as const;
- }),
- ...TESTING_FRAMEWORK_SETUP_HOOKS.map(
- (setupHook) =>
- ({
- code: `
- import * as testingLibrary from '@testing-library/foo';
- ${setupHook}(() => {
- testingLibrary.render()
- })
- `,
- errors: [
- {
- line: 4,
- column: 26,
- messageId: 'noRenderInSetup',
- },
- ],
- } as const)
- ),
- ...TESTING_FRAMEWORK_SETUP_HOOKS.map(
- (setupHook) =>
- ({
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { render } from 'imNoTestingLibrary';
- import * as testUtils from '../test-utils';
- ${setupHook}(() => {
- testUtils.renderWithRedux()
- })
- it('Test', () => {
- render()
- })
- `,
- errors: [
- {
- line: 5,
- column: 21,
- messageId: 'noRenderInSetup',
- },
- ],
- } as const)
- ),
- ...TESTING_FRAMEWORK_SETUP_HOOKS.map(
- (setupHook) =>
- ({
- code: `
- const { render } = require('@testing-library/foo')
-
- ${setupHook}(() => {
- render()
- })
- `,
- errors: [
- {
- line: 5,
- column: 11,
- messageId: 'noRenderInSetup',
- },
- ],
- } as const)
- ),
- ],
-});
diff --git a/tests/lib/rules/no-test-id-queries.test.ts b/tests/lib/rules/no-test-id-queries.test.ts
new file mode 100644
index 00000000..23ba3335
--- /dev/null
+++ b/tests/lib/rules/no-test-id-queries.test.ts
@@ -0,0 +1,86 @@
+import rule, { RULE_NAME } from '../../../lib/rules/no-test-id-queries';
+import { createRuleTester } from '../test-utils';
+
+const ruleTester = createRuleTester();
+
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ '@testing-library/dom',
+ '@testing-library/angular',
+ '@testing-library/react',
+ '@testing-library/vue',
+ '@marko/testing-library',
+];
+
+const QUERIES = [
+ 'getByTestId',
+ 'queryByTestId',
+ 'getAllByTestId',
+ 'queryAllByTestId',
+ 'findByTestId',
+ 'findAllByTestId',
+];
+
+ruleTester.run(RULE_NAME, rule, {
+ valid: [
+ `
+ import { render } from '@testing-library/react';
+
+ test('test', async () => {
+ const { getByRole } = render();
+
+ expect(getByRole('button')).toBeInTheDocument();
+ });
+ `,
+
+ `
+ import { render } from '@testing-library/react';
+
+ test('test', async () => {
+ render();
+
+ expect(getTestId('button')).toBeInTheDocument();
+ });
+ `,
+ ],
+
+ invalid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((framework) =>
+ QUERIES.flatMap((query) => [
+ {
+ code: `
+ import { render } from '${framework}';
+
+ test('test', async () => {
+ const { ${query} } = render();
+
+ expect(${query}('my-test-id')).toBeInTheDocument();
+ });
+ `,
+ errors: [
+ {
+ messageId: 'noTestIdQueries',
+ line: 7,
+ column: 14,
+ },
+ ],
+ },
+ {
+ code: `
+ import { render, screen } from '${framework}';
+
+ test('test', async () => {
+ render();
+
+ expect(screen.${query}('my-test-id')).toBeInTheDocument();
+ });
+ `,
+ errors: [
+ {
+ messageId: 'noTestIdQueries',
+ line: 7,
+ column: 14,
+ },
+ ],
+ },
+ ])
+ ),
+});
diff --git a/tests/lib/rules/no-unnecessary-act.test.ts b/tests/lib/rules/no-unnecessary-act.test.ts
index 62407e30..26305961 100644
--- a/tests/lib/rules/no-unnecessary-act.test.ts
+++ b/tests/lib/rules/no-unnecessary-act.test.ts
@@ -1,40 +1,48 @@
-import type { TSESLint } from '@typescript-eslint/experimental-utils';
+import {
+ type InvalidTestCase,
+ type ValidTestCase,
+} from '@typescript-eslint/rule-tester';
import rule, {
- MessageIds,
- Options,
- RULE_NAME,
+ MessageIds,
+ Options,
+ RULE_NAME,
} from '../../../lib/rules/no-unnecessary-act';
import { createRuleTester } from '../test-utils';
const ruleTester = createRuleTester();
-type ValidTestCase = TSESLint.ValidTestCase;
-type InvalidTestCase = TSESLint.InvalidTestCase;
-type TestCase = InvalidTestCase | ValidTestCase;
+type RuleValidTestCase = ValidTestCase;
+type RuleInvalidTestCase = InvalidTestCase;
+type TestCase = RuleInvalidTestCase | RuleValidTestCase;
const addOptions = (
- array: T[],
- options?: Options[number]
+ array: T[],
+ options?: Options[number]
): T[] =>
- array.map((testCase) => ({
- ...testCase,
- options: [options],
- }));
+ array.map((testCase) => ({
+ ...testCase,
+ options: [options],
+ }));
const disableStrict = (array: T[]): T[] =>
- addOptions(array, { isStrict: false });
+ addOptions(array, { isStrict: false });
const enableStrict = (array: T[]): T[] =>
- addOptions(array, { isStrict: true });
+ addOptions(array, { isStrict: true });
/**
* - AGR stands for Aggressive Reporting
* - RTL stands for React Testing Library (@testing-library/react)
+ * - Marko TL stands for Marko Testing Library (@marko/testing-library)
* - RTU stands for React Test Utils (react-dom/test-utils)
*/
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ ['@testing-library/react', 'RTL'],
+ ['@marko/testing-library', 'Marko TL'],
+];
-const validNonStrictTestCases: ValidTestCase[] = [
- {
- code: `// case: RTL act wrapping both RTL and non-RTL calls
+const validNonStrictTestCases: RuleValidTestCase[] = [
+ {
+ code: `// case: RTL act wrapping both RTL and non-RTL calls
import { act, render, waitFor } from '@testing-library/react'
test('valid case', async () => {
@@ -54,13 +62,13 @@ const validNonStrictTestCases: ValidTestCase[] = [
});
});
`,
- },
+ },
];
-const validTestCases: ValidTestCase[] = [
- {
- code: `// case: RTL act wrapping non-RTL calls
- import { act } from '@testing-library/react'
+const validTestCases: RuleValidTestCase[] = [
+ ...SUPPORTED_TESTING_FRAMEWORKS.map(([testingFramework, shortName]) => ({
+ code: `// case: ${shortName} act wrapping non-${shortName} calls
+ import { act } from '${testingFramework}'
test('valid case', async () => {
act(() => {
@@ -118,9 +126,9 @@ const validTestCases: ValidTestCase[] = [
act(stuffThatDoesNotUseRTL().then(() => {}))
});
`,
- },
- {
- code: `// case: RTU act wrapping non-RTL
+ })),
+ {
+ code: `// case: RTU act wrapping non-RTL
import { act } from 'react-dom/test-utils'
test('valid case', async () => {
@@ -143,13 +151,13 @@ const validTestCases: ValidTestCase[] = [
act(() => stuffThatDoesNotUseRTL());
});
`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `// case: RTL act wrapping non-RTL - AGR disabled
- import { act } from '@testing-library/react'
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.map(([testingFramework, shortName]) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `// case: ${shortName} act wrapping non-${shortName} - AGR disabled
+ import { act } from '${testingFramework}'
import { waitFor } from 'somewhere-else'
test('valid case', async () => {
@@ -172,15 +180,15 @@ const validTestCases: ValidTestCase[] = [
act(() => waitFor());
});
`,
- },
+ })),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `// case: non-RTL act wrapping RTL - AGR disabled
+ ...SUPPORTED_TESTING_FRAMEWORKS.map(([testingFramework, shortName]) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `// case: non-${shortName} act wrapping ${shortName} - AGR disabled
import { act } from 'somewhere-else'
- import { waitFor } from '@testing-library/react'
+ import { waitFor } from '${testingFramework}'
test('valid case', async () => {
act(() => {
@@ -206,13 +214,14 @@ const validTestCases: ValidTestCase[] = [
act(function() {})
});
`,
- },
+ })),
];
-const invalidStrictTestCases: InvalidTestCase[] = [
- {
- code: `// case: RTL act wrapping both RTL and non-RTL calls with strict option
- import { act, render } from '@testing-library/react'
+const invalidStrictTestCases: RuleInvalidTestCase[] =
+ SUPPORTED_TESTING_FRAMEWORKS.flatMap(([testingFramework, shortName]) => [
+ {
+ code: `// case: ${shortName} act wrapping both ${shortName} and non-${shortName} calls with strict option
+ import { act, render } from '${testingFramework}'
await act(async () => {
userEvent.click(screen.getByText("Submit"))
@@ -223,25 +232,27 @@ const invalidStrictTestCases: InvalidTestCase[] = [
flushPromises()
})
`,
- errors: [
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 4,
- column: 13,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 8,
- column: 7,
- },
- ],
- },
-];
-
-const invalidTestCases: InvalidTestCase[] = [
- {
- code: `// case: RTL act wrapping RTL calls - callbacks with body (BlockStatement)
- import { act, fireEvent, screen, render, waitFor, waitForElementToBeRemoved } from '@testing-library/react'
+ errors: [
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 4,
+ column: 13,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 8,
+ column: 7,
+ },
+ ],
+ },
+ ]);
+
+const invalidTestCases: RuleInvalidTestCase[] = [
+ ...SUPPORTED_TESTING_FRAMEWORKS.map(
+ ([testingFramework, shortName]) =>
+ ({
+ code: `// case: ${shortName} act wrapping ${shortName} calls - callbacks with body (BlockStatement)
+ import { act, fireEvent, screen, render, waitFor, waitForElementToBeRemoved } from '${testingFramework}'
import userEvent from '@testing-library/user-event'
test('invalid case', async () => {
@@ -280,50 +291,55 @@ const invalidTestCases: InvalidTestCase[] = [
});
});
`,
- errors: [
- { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 6, column: 9 },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 10,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 14,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 18,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 22,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 26,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 30,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 34,
- column: 9,
- },
- ],
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `// case: RTL act wrapping RTL calls - callbacks with body (BlockStatement) - AGR disabled
+ errors: [
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 6,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 10,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 14,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 18,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 22,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 26,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 30,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 34,
+ column: 9,
+ },
+ ],
+ }) as const
+ ),
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `// case: RTL act wrapping RTL calls - callbacks with body (BlockStatement) - AGR disabled
import { act, fireEvent, screen, render, waitFor, waitForElementToBeRemoved } from 'test-utils'
import userEvent from '@testing-library/user-event'
@@ -363,47 +379,47 @@ const invalidTestCases: InvalidTestCase[] = [
});
});
`,
- errors: [
- { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 6, column: 9 },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 10,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 14,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 18,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 22,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 26,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 30,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 34,
- column: 9,
- },
- ],
- },
- {
- code: `// case: RTL act wrapping RTL calls - callbacks with return
+ errors: [
+ { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 6, column: 9 },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 10,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 14,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 18,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 22,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 26,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 30,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 34,
+ column: 9,
+ },
+ ],
+ },
+ {
+ code: `// case: RTL act wrapping RTL calls - callbacks with return
import { act, fireEvent, screen, render, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
@@ -450,90 +466,90 @@ const invalidTestCases: InvalidTestCase[] = [
}).then(() => {})
});
`,
- errors: [
- { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 6, column: 9 },
- { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 7, column: 9 },
- { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 8, column: 9 },
- { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 9, column: 9 },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 10,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 11,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 12,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 13,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 14,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 16,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 19,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 22,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 25,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 28,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 31,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 34,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 37,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 40,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 43,
- column: 9,
- },
- ],
- },
- {
- code: `// case: RTL act wrapping empty callback
+ errors: [
+ { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 6, column: 9 },
+ { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 7, column: 9 },
+ { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 8, column: 9 },
+ { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 9, column: 9 },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 10,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 11,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 12,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 13,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 14,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 16,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 19,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 22,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 25,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 28,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 31,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 34,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 37,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 40,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 43,
+ column: 9,
+ },
+ ],
+ },
+ {
+ code: `// case: RTL act wrapping empty callback
import { act } from '@testing-library/react'
test('invalid case', async () => {
@@ -543,18 +559,18 @@ const invalidTestCases: InvalidTestCase[] = [
act(function () {})
})
`,
- errors: [
- { messageId: 'noUnnecessaryActEmptyFunction', line: 5, column: 15 },
- { messageId: 'noUnnecessaryActEmptyFunction', line: 6, column: 9 },
- { messageId: 'noUnnecessaryActEmptyFunction', line: 7, column: 15 },
- { messageId: 'noUnnecessaryActEmptyFunction', line: 8, column: 9 },
- ],
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `// case: RTL act wrapping empty callback - require version
+ errors: [
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 5, column: 15 },
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 6, column: 9 },
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 7, column: 15 },
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 8, column: 9 },
+ ],
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `// case: RTL act wrapping empty callback - require version
const { act } = require('@testing-library/react');
test('invalid case', async () => {
@@ -565,21 +581,21 @@ const invalidTestCases: InvalidTestCase[] = [
act(function () {}).then(() => {})
})
`,
- errors: [
- { messageId: 'noUnnecessaryActEmptyFunction', line: 5, column: 15 },
- { messageId: 'noUnnecessaryActEmptyFunction', line: 6, column: 9 },
- { messageId: 'noUnnecessaryActEmptyFunction', line: 7, column: 15 },
- { messageId: 'noUnnecessaryActEmptyFunction', line: 8, column: 9 },
- { messageId: 'noUnnecessaryActEmptyFunction', line: 9, column: 9 },
- ],
- },
-
- // cases for act related to React Test Utils
- {
- settings: {
- 'testing-library/utils-module': 'custom-testing-module',
- },
- code: `// case: RTU act wrapping RTL calls - callbacks with body (BlockStatement)
+ errors: [
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 5, column: 15 },
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 6, column: 9 },
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 7, column: 15 },
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 8, column: 9 },
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 9, column: 9 },
+ ],
+ },
+
+ // cases for act related to React Test Utils
+ {
+ settings: {
+ 'testing-library/utils-module': 'custom-testing-module',
+ },
+ code: `// case: RTU act wrapping RTL calls - callbacks with body (BlockStatement)
import { act } from 'react-dom/test-utils';
import { fireEvent, screen, render, waitFor, waitForElementToBeRemoved } from 'custom-testing-module'
import userEvent from '@testing-library/user-event'
@@ -620,50 +636,50 @@ const invalidTestCases: InvalidTestCase[] = [
});
});
`,
- errors: [
- { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 7, column: 9 },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 11,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 15,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 19,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 23,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 27,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 31,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 35,
- column: 9,
- },
- ],
- },
- {
- settings: {
- 'testing-library/utils-module': 'custom-testing-module',
- },
- code: `// case: RTU act wrapping RTL calls - callbacks with return
+ errors: [
+ { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 7, column: 9 },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 11,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 15,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 19,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 23,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 27,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 31,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 35,
+ column: 9,
+ },
+ ],
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'custom-testing-module',
+ },
+ code: `// case: RTU act wrapping RTL calls - callbacks with return
import { act } from 'react-dom/test-utils';
import { fireEvent, screen, render, waitFor } from 'custom-testing-module'
import userEvent from '@testing-library/user-event'
@@ -708,92 +724,92 @@ const invalidTestCases: InvalidTestCase[] = [
});
});
`,
- errors: [
- { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 7, column: 9 },
- { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 8, column: 9 },
- { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 9, column: 9 },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 10,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 11,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 12,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 13,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 14,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 15,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 17,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 20,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 23,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 26,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 29,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 32,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 35,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 38,
- column: 9,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 41,
- column: 15,
- },
- ],
- },
- {
- settings: {
- 'testing-library/utils-module': 'off',
- },
- code: `// case: RTU act wrapping empty callback
+ errors: [
+ { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 7, column: 9 },
+ { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 8, column: 9 },
+ { messageId: 'noUnnecessaryActTestingLibraryUtil', line: 9, column: 9 },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 10,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 11,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 12,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 13,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 14,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 15,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 17,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 20,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 23,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 26,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 29,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 32,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 35,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 38,
+ column: 9,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 41,
+ column: 15,
+ },
+ ],
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'off',
+ },
+ code: `// case: RTU act wrapping empty callback
import { act } from 'react-dom/test-utils';
import { render } from '@testing-library/react'
@@ -805,18 +821,18 @@ const invalidTestCases: InvalidTestCase[] = [
act(function () {});
});
`,
- errors: [
- { messageId: 'noUnnecessaryActEmptyFunction', line: 7, column: 15 },
- { messageId: 'noUnnecessaryActEmptyFunction', line: 8, column: 9 },
- { messageId: 'noUnnecessaryActEmptyFunction', line: 9, column: 15 },
- { messageId: 'noUnnecessaryActEmptyFunction', line: 10, column: 9 },
- ],
- },
- {
- settings: {
- 'testing-library/utils-module': 'off',
- },
- code: `// case: RTU act wrapping empty callback - require version
+ errors: [
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 7, column: 15 },
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 8, column: 9 },
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 9, column: 15 },
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 10, column: 9 },
+ ],
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'off',
+ },
+ code: `// case: RTU act wrapping empty callback - require version
const { act } = require('react-dom/test-utils');
const { render } = require('@testing-library/react');
@@ -828,20 +844,20 @@ const invalidTestCases: InvalidTestCase[] = [
act(function () {});
})
`,
- errors: [
- { messageId: 'noUnnecessaryActEmptyFunction', line: 7, column: 15 },
- { messageId: 'noUnnecessaryActEmptyFunction', line: 8, column: 9 },
- { messageId: 'noUnnecessaryActEmptyFunction', line: 9, column: 15 },
- { messageId: 'noUnnecessaryActEmptyFunction', line: 10, column: 9 },
- ],
- },
-
- {
- settings: {
- 'testing-library/utils-module': 'custom-testing-module',
- 'testing-library/custom-renders': 'off',
- },
- code: `// case: mixed scenarios - AGR disabled
+ errors: [
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 7, column: 15 },
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 8, column: 9 },
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 9, column: 15 },
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 10, column: 9 },
+ ],
+ },
+
+ {
+ settings: {
+ 'testing-library/utils-module': 'custom-testing-module',
+ 'testing-library/custom-renders': 'off',
+ },
+ code: `// case: mixed scenarios - AGR disabled
import * as ReactTestUtils from 'react-dom/test-utils';
import { act as renamedAct, fireEvent, screen as renamedScreen, render, waitFor } from 'custom-testing-module'
import userEvent from '@testing-library/user-event'
@@ -860,36 +876,36 @@ const invalidTestCases: InvalidTestCase[] = [
act(function() { return renamedScreen.getByText('foo') })
});
`,
- errors: [
- { messageId: 'noUnnecessaryActEmptyFunction', line: 8, column: 24 },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 9,
- column: 30,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 10,
- column: 15,
- },
- {
- messageId: 'noUnnecessaryActTestingLibraryUtil',
- line: 11,
- column: 9,
- },
- ],
- },
+ errors: [
+ { messageId: 'noUnnecessaryActEmptyFunction', line: 8, column: 24 },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 9,
+ column: 30,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 10,
+ column: 15,
+ },
+ {
+ messageId: 'noUnnecessaryActTestingLibraryUtil',
+ line: 11,
+ column: 9,
+ },
+ ],
+ },
];
ruleTester.run(RULE_NAME, rule, {
- valid: [
- ...validTestCases,
- ...disableStrict(validNonStrictTestCases),
- ...disableStrict(validTestCases),
- ],
- invalid: [
- ...invalidTestCases,
- ...enableStrict(invalidStrictTestCases),
- ...disableStrict(invalidTestCases),
- ],
+ valid: [
+ ...validTestCases,
+ ...disableStrict(validNonStrictTestCases),
+ ...disableStrict(validTestCases),
+ ],
+ invalid: [
+ ...invalidTestCases,
+ ...enableStrict(invalidStrictTestCases),
+ ...disableStrict(invalidTestCases),
+ ],
});
diff --git a/tests/lib/rules/no-wait-for-empty-callback.test.ts b/tests/lib/rules/no-wait-for-empty-callback.test.ts
deleted file mode 100644
index d9dae719..00000000
--- a/tests/lib/rules/no-wait-for-empty-callback.test.ts
+++ /dev/null
@@ -1,214 +0,0 @@
-import rule, { RULE_NAME } from '../../../lib/rules/no-wait-for-empty-callback';
-import { createRuleTester } from '../test-utils';
-
-const ruleTester = createRuleTester();
-
-const ALL_WAIT_METHODS = ['waitFor', 'waitForElementToBeRemoved'];
-
-ruleTester.run(RULE_NAME, rule, {
- valid: [
- ...ALL_WAIT_METHODS.map((m) => ({
- code: `${m}(() => {
- screen.getByText(/submit/i)
- })`,
- })),
- ...ALL_WAIT_METHODS.map((m) => ({
- code: `${m}(function() {
- screen.getByText(/submit/i)
- })`,
- })),
- {
- code: `waitForElementToBeRemoved(someNode)`,
- },
- {
- code: `waitForElementToBeRemoved(() => someNode)`,
- },
- {
- code: `waitSomethingElse(() => {})`,
- },
- {
- code: `wait(() => {})`,
- },
- {
- code: `wait(noop)`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { waitFor } from 'somewhere-else'
- waitFor(() => {})
- `,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { waitFor as renamedWaitFor } from '@testing-library/react'
- import { waitFor } from 'somewhere-else'
- waitFor(() => {})
- `,
- },
- ],
-
- invalid: [
- ...ALL_WAIT_METHODS.map(
- (m) =>
- ({
- code: `${m}(() => {})`,
- errors: [
- {
- line: 1,
- column: 8 + m.length,
- messageId: 'noWaitForEmptyCallback',
- data: {
- methodName: m,
- },
- },
- ],
- } as const)
- ),
- ...ALL_WAIT_METHODS.map(
- (m) =>
- ({
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { ${m} } from 'test-utils';
- ${m}(() => {});
- `,
- errors: [
- {
- line: 3,
- column: 16 + m.length,
- messageId: 'noWaitForEmptyCallback',
- data: {
- methodName: m,
- },
- },
- ],
- } as const)
- ),
- ...ALL_WAIT_METHODS.map(
- (m) =>
- ({
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { ${m} as renamedAsyncUtil } from 'test-utils';
- renamedAsyncUtil(() => {});
- `,
- errors: [
- {
- line: 3,
- column: 32,
- messageId: 'noWaitForEmptyCallback',
- data: {
- methodName: 'renamedAsyncUtil',
- },
- },
- ],
- } as const)
- ),
- ...ALL_WAIT_METHODS.map(
- (m) =>
- ({
- code: `${m}((a, b) => {})`,
- errors: [
- {
- line: 1,
- column: 12 + m.length,
- messageId: 'noWaitForEmptyCallback',
- data: {
- methodName: m,
- },
- },
- ],
- } as const)
- ),
- ...ALL_WAIT_METHODS.map(
- (m) =>
- ({
- code: `${m}(() => { /* I'm empty anyway */ })`,
- errors: [
- {
- line: 1,
- column: 8 + m.length,
- messageId: 'noWaitForEmptyCallback',
- data: {
- methodName: m,
- },
- },
- ],
- } as const)
- ),
-
- ...ALL_WAIT_METHODS.map(
- (m) =>
- ({
- code: `${m}(function() {
-
- })`,
- errors: [
- {
- line: 1,
- column: 13 + m.length,
- messageId: 'noWaitForEmptyCallback',
- data: {
- methodName: m,
- },
- },
- ],
- } as const)
- ),
- ...ALL_WAIT_METHODS.map(
- (m) =>
- ({
- code: `${m}(function(a) {
-
- })`,
- errors: [
- {
- line: 1,
- column: 14 + m.length,
- messageId: 'noWaitForEmptyCallback',
- data: {
- methodName: m,
- },
- },
- ],
- } as const)
- ),
- ...ALL_WAIT_METHODS.map(
- (m) =>
- ({
- code: `${m}(function() {
- // another empty callback
- })`,
- errors: [
- {
- line: 1,
- column: 13 + m.length,
- messageId: 'noWaitForEmptyCallback',
- data: {
- methodName: m,
- },
- },
- ],
- } as const)
- ),
-
- ...ALL_WAIT_METHODS.map(
- (m) =>
- ({
- code: `${m}(noop)`,
- errors: [
- {
- line: 1,
- column: 2 + m.length,
- messageId: 'noWaitForEmptyCallback',
- data: {
- methodName: m,
- },
- },
- ],
- } as const)
- ),
- ],
-});
diff --git a/tests/lib/rules/no-wait-for-multiple-assertions.test.ts b/tests/lib/rules/no-wait-for-multiple-assertions.test.ts
index 84a3f4be..46f30b7e 100644
--- a/tests/lib/rules/no-wait-for-multiple-assertions.test.ts
+++ b/tests/lib/rules/no-wait-for-multiple-assertions.test.ts
@@ -1,148 +1,159 @@
import rule, {
- RULE_NAME,
+ RULE_NAME,
} from '../../../lib/rules/no-wait-for-multiple-assertions';
import { createRuleTester } from '../test-utils';
const ruleTester = createRuleTester();
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ '@testing-library/dom',
+ '@testing-library/angular',
+ '@testing-library/react',
+ '@testing-library/vue',
+ '@marko/testing-library',
+];
+
ruleTester.run(RULE_NAME, rule, {
- valid: [
- {
- code: `
+ valid: [
+ {
+ code: `
await waitFor(() => expect(a).toEqual('a'))
`,
- },
- {
- code: `
+ },
+ {
+ code: `
await waitFor(function() {
expect(a).toEqual('a')
})
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `// Aggressive Reporting disabled - module imported not matching
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `// Aggressive Reporting disabled - module imported not matching
import { waitFor } from 'somewhere-else'
await waitFor(() => {
expect(a).toEqual('a')
expect(b).toEqual('b')
})
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `// Aggressive Reporting disabled - waitFor renamed
- import { waitFor as renamedWaitFor } from '@testing-library/react'
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.map((testingFramework) => ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `// Aggressive Reporting disabled - waitFor renamed
+ import { waitFor as renamedWaitFor } from '${testingFramework}'
import { waitFor } from 'somewhere-else'
await waitFor(() => {
expect(a).toEqual('a')
expect(b).toEqual('b')
})
`,
- },
- // this needs to be check by other rule
- {
- code: `
+ })),
+ // this needs to be check by other rule
+ {
+ code: `
await waitFor(() => {
fireEvent.keyDown(input, {key: 'ArrowDown'})
expect(b).toEqual('b')
})
`,
- },
- {
- code: `
+ },
+ {
+ code: `
await waitFor(function() {
fireEvent.keyDown(input, {key: 'ArrowDown'})
expect(b).toEqual('b')
})
`,
- },
- {
- code: `
+ },
+ {
+ code: `
await waitFor(() => {
console.log('testing-library')
expect(b).toEqual('b')
})
`,
- },
- {
- code: `
+ },
+ {
+ code: `
await waitFor(function() {
console.log('testing-library')
expect(b).toEqual('b')
})
`,
- },
- {
- code: `
+ },
+ {
+ code: `
await waitFor(() => {})
`,
- },
- {
- code: `
+ },
+ {
+ code: `
await waitFor(function() {})
`,
- },
- {
- code: `
+ },
+ {
+ code: `
await waitFor(() => {
// testing
})
`,
- },
- ],
- invalid: [
- {
- code: `
+ },
+ ],
+ invalid: [
+ {
+ code: `
await waitFor(() => {
expect(a).toEqual('a')
expect(b).toEqual('b')
})
`,
- errors: [
- { line: 4, column: 11, messageId: 'noWaitForMultipleAssertion' },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `// Aggressive Reporting disabled
- import { waitFor } from '@testing-library/react'
+ errors: [
+ { line: 4, column: 11, messageId: 'noWaitForMultipleAssertion' },
+ ],
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.map(
+ (testingFramework) =>
+ ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `// Aggressive Reporting disabled
+ import { waitFor } from '${testingFramework}'
await waitFor(() => {
expect(a).toEqual('a')
expect(b).toEqual('b')
})
`,
- errors: [
- { line: 5, column: 11, messageId: 'noWaitForMultipleAssertion' },
- ],
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `// Aggressive Reporting disabled
+ errors: [
+ { line: 5, column: 11, messageId: 'noWaitForMultipleAssertion' },
+ ],
+ }) as const
+ ),
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `// Aggressive Reporting disabled
import { waitFor as renamedWaitFor } from 'test-utils'
await renamedWaitFor(() => {
expect(a).toEqual('a')
expect(b).toEqual('b')
})
`,
- errors: [
- { line: 5, column: 11, messageId: 'noWaitForMultipleAssertion' },
- ],
- },
- {
- code: `
+ errors: [
+ { line: 5, column: 11, messageId: 'noWaitForMultipleAssertion' },
+ ],
+ },
+ {
+ code: `
await waitFor(() => {
expect(a).toEqual('a')
console.log('testing-library')
expect(b).toEqual('b')
})
`,
- errors: [
- { line: 5, column: 11, messageId: 'noWaitForMultipleAssertion' },
- ],
- },
- {
- code: `
+ errors: [
+ { line: 5, column: 11, messageId: 'noWaitForMultipleAssertion' },
+ ],
+ },
+ {
+ code: `
test('should whatever', async () => {
await waitFor(() => {
expect(a).toEqual('a')
@@ -151,24 +162,24 @@ ruleTester.run(RULE_NAME, rule, {
})
})
`,
- errors: [
- { line: 6, column: 13, messageId: 'noWaitForMultipleAssertion' },
- ],
- },
- {
- code: `
+ errors: [
+ { line: 6, column: 13, messageId: 'noWaitForMultipleAssertion' },
+ ],
+ },
+ {
+ code: `
await waitFor(async () => {
expect(a).toEqual('a')
await somethingAsync()
expect(b).toEqual('b')
})
`,
- errors: [
- { line: 5, column: 11, messageId: 'noWaitForMultipleAssertion' },
- ],
- },
- {
- code: `
+ errors: [
+ { line: 5, column: 11, messageId: 'noWaitForMultipleAssertion' },
+ ],
+ },
+ {
+ code: `
await waitFor(function() {
expect(a).toEqual('a')
expect(b).toEqual('b')
@@ -176,35 +187,35 @@ ruleTester.run(RULE_NAME, rule, {
expect(d).toEqual('d')
})
`,
- errors: [
- { line: 4, column: 11, messageId: 'noWaitForMultipleAssertion' },
- { line: 5, column: 11, messageId: 'noWaitForMultipleAssertion' },
- { line: 6, column: 11, messageId: 'noWaitForMultipleAssertion' },
- ],
- },
- {
- code: `
+ errors: [
+ { line: 4, column: 11, messageId: 'noWaitForMultipleAssertion' },
+ { line: 5, column: 11, messageId: 'noWaitForMultipleAssertion' },
+ { line: 6, column: 11, messageId: 'noWaitForMultipleAssertion' },
+ ],
+ },
+ {
+ code: `
await waitFor(function() {
expect(a).toEqual('a')
console.log('testing-library')
expect(b).toEqual('b')
})
`,
- errors: [
- { line: 5, column: 11, messageId: 'noWaitForMultipleAssertion' },
- ],
- },
- {
- code: `
+ errors: [
+ { line: 5, column: 11, messageId: 'noWaitForMultipleAssertion' },
+ ],
+ },
+ {
+ code: `
await waitFor(async function() {
expect(a).toEqual('a')
const el = await somethingAsync()
expect(b).toEqual('b')
})
`,
- errors: [
- { line: 5, column: 11, messageId: 'noWaitForMultipleAssertion' },
- ],
- },
- ],
+ errors: [
+ { line: 5, column: 11, messageId: 'noWaitForMultipleAssertion' },
+ ],
+ },
+ ],
});
diff --git a/tests/lib/rules/no-wait-for-side-effects.test.ts b/tests/lib/rules/no-wait-for-side-effects.test.ts
index e5102126..c7eed01a 100644
--- a/tests/lib/rules/no-wait-for-side-effects.test.ts
+++ b/tests/lib/rules/no-wait-for-side-effects.test.ts
@@ -1,135 +1,173 @@
-import rule, { RULE_NAME } from '../../../lib/rules/no-wait-for-side-effects';
+import { InvalidTestCase } from '@typescript-eslint/rule-tester';
+
+import rule, {
+ RULE_NAME,
+ type MessageIds,
+} from '../../../lib/rules/no-wait-for-side-effects';
import { createRuleTester } from '../test-utils';
const ruleTester = createRuleTester();
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ '@testing-library/dom',
+ '@testing-library/angular',
+ '@testing-library/react',
+ '@testing-library/vue',
+ '@marko/testing-library',
+];
+
ruleTester.run(RULE_NAME, rule, {
- valid: [
- {
- code: `
- import { waitFor } from '@testing-library/react';
- await waitFor(() => expect(a).toEqual('a'))
- `,
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
- await waitFor(function() {
- expect(a).toEqual('a')
- })
- `,
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
- await waitFor(() => {
- console.log('testing-library')
- expect(b).toEqual('b')
- })
- `,
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
- await waitFor(function() {
- console.log('testing-library')
- expect(b).toEqual('b')
- })
- `,
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
- await waitFor(() => {})
- `,
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
- await waitFor(function() {})
- `,
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
- await waitFor(() => {
- // testing
- })
- `,
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
- await waitFor(function() {
- // testing
- })
- `,
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
- fireEvent.keyDown(input, {key: 'ArrowDown'})
- await waitFor(() => {
- expect(b).toEqual('b')
- })
- `,
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
- fireEvent.keyDown(input, {key: 'ArrowDown'})
- await waitFor(function() {
- expect(b).toEqual('b')
- })
- `,
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
- userEvent.click(button)
- await waitFor(function() {
- expect(b).toEqual('b')
- })
- `,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { waitFor } from 'somewhere-else';
+ valid: [
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
+ await waitFor(() => expect(a).toEqual('a'))
+ `,
+ },
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
+ await waitFor(function() {
+ expect(a).toEqual('a')
+ })
+ `,
+ },
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
+ await waitFor(() => {
+ console.log('testing-library')
+ expect(b).toEqual('b')
+ })
+ `,
+ },
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
+ await waitFor(function() {
+ console.log('testing-library')
+ expect(b).toEqual('b')
+ })
+ `,
+ },
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
+ await waitFor(() => {})
+ `,
+ },
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
+ await waitFor(function() {})
+ `,
+ },
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
+ await waitFor(() => {
+ // testing
+ })
+ `,
+ },
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
+ await waitFor(function() {
+ // testing
+ })
+ `,
+ },
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
+ fireEvent.keyDown(input, {key: 'ArrowDown'})
+ await waitFor(() => {
+ expect(b).toEqual('b')
+ })
+ `,
+ },
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
+ fireEvent.keyDown(input, {key: 'ArrowDown'})
+ await waitFor(function() {
+ expect(b).toEqual('b')
+ })
+ `,
+ },
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
+ userEvent.click(button)
+ await waitFor(function() {
+ expect(b).toEqual('b')
+ })
+ `,
+ },
+ {
+ // Issue #500, https://github.com/testing-library/eslint-plugin-testing-library/issues/500
+ code: `
+ import { waitFor } from '${testingFramework}';
+ userEvent.click(button)
+ waitFor(function() {
+ expect(b).toEqual('b')
+ }).then(() => {
+ // Side effects are allowed inside .then()
+ userEvent.click(button);
+ })
+ `,
+ },
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
+ import { notUserEvent } from 'somewhere-else';
+
+ waitFor(() => {
+ await notUserEvent.click(button)
+ })
+ `,
+ },
+ ]),
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { waitFor } from 'somewhere-else';
await waitFor(function() {
fireEvent.keyDown(input, {key: 'ArrowDown'})
expect(b).toEqual('b')
})
`,
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
-
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.map((testingFramework) => ({
+ code: `
+ import { waitFor } from '${testingFramework}';
+
anotherFunction(() => {
fireEvent.keyDown(input, {key: 'ArrowDown'});
userEvent.click(button);
});
-
+
test('side effects in functions other than waitFor are valid', () => {
fireEvent.keyDown(input, {key: 'ArrowDown'})
userEvent.click(button)
expect(b).toEqual('b')
});
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ })),
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { waitFor } from 'somewhere-else';
await waitFor(() => {
fireEvent.keyDown(input, {key: 'ArrowDown'})
})
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { waitFor as renamedWaitFor, fireEvent } from 'test-utils';
import { waitFor, userEvent } from 'somewhere-else';
@@ -138,10 +176,10 @@ ruleTester.run(RULE_NAME, rule, {
userEvent.click(button)
})
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { waitFor, fireEvent as renamedFireEvent, userEvent as renamedUserEvent } from 'test-utils';
import { fireEvent, userEvent } from 'somewhere-else';
@@ -150,558 +188,594 @@ ruleTester.run(RULE_NAME, rule, {
userEvent.click(button)
})
`,
- },
- {
- code: `// weird case to cover 100% coverage
+ },
+ {
+ code: `// weird case to cover 100% coverage
await waitFor(() => {
const click = firEvent['click']
})
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { waitFor } from 'somewhere-else';
await waitFor(() => fireEvent.keyDown(input, {key: 'ArrowDown'}))
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.map((testingFramework) => ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { waitFor } from 'somewhere-else';
- import { userEvent } from '@testing-library/react';
+ import { userEvent } from '${testingFramework}';
await waitFor(() => userEvent.click(button))
`,
- },
- {
- settings: { 'testing-library/utils-module': '~/test-utils' },
- code: `
+ })),
+ {
+ settings: { 'testing-library/utils-module': '~/test-utils' },
+ code: `
import { waitFor, userEvent } from '~/test-utils';
await waitFor(() => userEvent.click(button))
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { waitFor } from 'somewhere-else';
await waitFor(() => render())
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { waitFor } from 'somewhere-else';
await waitFor(() => {
const { container } = render()
})
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { waitFor } from 'somewhere-else';
const { rerender } = render()
await waitFor(() => {
rerender()
})
`,
- },
- {
- settings: { 'testing-library/utils-module': '~/test-utils' },
- code: `
+ },
+ {
+ settings: { 'testing-library/utils-module': '~/test-utils' },
+ code: `
import { waitFor } from '~/test-utils';
import { render } from 'somewhere-else';
await waitFor(() => render())
`,
- },
- {
- settings: { 'testing-library/utils-module': '~/test-utils' },
- code: `
- import { waitFor } from '@testing-library/react';
- import { render } from 'somewhere-else';
- await waitFor(() => render())
- `,
- },
- {
- settings: { 'testing-library/custom-renders': ['renderHelper'] },
- code: `
- import { waitFor } from '@testing-library/react';
- import { renderWrapper } from 'somewhere-else';
- await waitFor(() => renderWrapper())
- `,
- },
- {
- settings: { 'testing-library/custom-renders': ['renderHelper'] },
- code: `
- import { waitFor } from '@testing-library/react';
- import { renderWrapper } from 'somewhere-else';
- await waitFor(() => {
- renderWrapper()
- })
- `,
- },
- {
- settings: { 'testing-library/custom-renders': ['renderHelper'] },
- code: `
- import { waitFor } from '@testing-library/react';
- import { renderWrapper } from 'somewhere-else';
- await waitFor(() => {
- const { container } = renderWrapper()
- })
- `,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ settings: { 'testing-library/utils-module': '~/test-utils' },
+ code: `
+ import { waitFor } from '${testingFramework}';
+ import { render } from 'somewhere-else';
+ await waitFor(() => render())
+ `,
+ },
+ {
+ settings: { 'testing-library/custom-renders': ['renderHelper'] },
+ code: `
+ import { waitFor } from '${testingFramework}';
+ import { renderWrapper } from 'somewhere-else';
+ await waitFor(() => renderWrapper())
+ `,
+ },
+ {
+ settings: { 'testing-library/custom-renders': ['renderHelper'] },
+ code: `
+ import { waitFor } from '${testingFramework}';
+ import { renderWrapper } from 'somewhere-else';
+ await waitFor(() => {
+ renderWrapper()
+ })
+ `,
+ },
+ {
+ settings: { 'testing-library/custom-renders': ['renderHelper'] },
+ code: `
+ import { waitFor } from '${testingFramework}';
+ import { renderWrapper } from 'somewhere-else';
+ await waitFor(() => {
+ const { container } = renderWrapper()
+ })
+ `,
+ },
+ ]),
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { waitFor } from 'somewhere-else';
await waitFor(() => {
render()
})
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { waitFor } from 'test-utils';
import { render } from 'somewhere-else';
await waitFor(() => {
render()
})
`,
- },
- {
- settings: { 'testing-library/custom-renders': ['renderHelper'] },
- code: `
- import { waitFor } from '@testing-library/react';
- import { renderWrapper } from 'somewhere-else';
- await waitFor(() => {
- renderWrapper()
- })
- `,
- },
- {
- settings: { 'testing-library/custom-renders': ['renderHelper'] },
- code: `
- import { waitFor } from '@testing-library/react';
- await waitFor(() => result = renderWrapper())
- `,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ settings: { 'testing-library/custom-renders': ['renderHelper'] },
+ code: `
+ import { waitFor } from '${testingFramework}';
+ await waitFor(() => result = renderWrapper())
+ `,
+ },
+ ]),
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { waitFor } from 'test-utils';
import { render } from 'somewhere-else';
await waitFor(() => result = render())
`,
- },
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ },
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { waitFor } from 'somewhere-else';
await waitFor(() => result = render())
`,
- },
- ],
- invalid: [
- // render
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ },
+ ],
+ invalid: [
+ // render
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => render())
`,
- errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(function() {
render()
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(function() {
const { container } = renderHelper()
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- settings: { 'testing-library/custom-renders': ['renderHelper'] },
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ settings: { 'testing-library/custom-renders': ['renderHelper'] },
+ code: `
+ import { waitFor } from '${testingFramework}';
import { renderHelper } from 'somewhere-else';
await waitFor(() => renderHelper())
`,
- errors: [{ line: 4, column: 29, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- settings: { 'testing-library/custom-renders': ['renderHelper'] },
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 4, column: 29, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ settings: { 'testing-library/custom-renders': ['renderHelper'] },
+ code: `
+ import { waitFor } from '${testingFramework}';
import { renderHelper } from 'somewhere-else';
await waitFor(() => {
renderHelper()
})
`,
- errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- settings: { 'testing-library/custom-renders': ['renderHelper'] },
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ settings: { 'testing-library/custom-renders': ['renderHelper'] },
+ code: `
+ import { waitFor } from '${testingFramework}';
import { renderHelper } from 'somewhere-else';
await waitFor(() => {
const { container } = renderHelper()
})
`,
- errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- settings: { 'testing-library/custom-renders': ['renderHelper'] },
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ settings: { 'testing-library/custom-renders': ['renderHelper'] },
+ code: `
+ import { waitFor } from '${testingFramework}';
import { renderHelper } from 'somewhere-else';
let container;
await waitFor(() => {
({ container } = renderHelper())
})
`,
- errors: [{ line: 6, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 6, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => result = render())
`,
- errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => (a = 5, result = render()))
`,
- errors: [{ line: 3, column: 30, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 3, column: 30, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
const { rerender } = render()
await waitFor(() => rerender())
`,
- errors: [{ line: 4, column: 29, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor, render } from '@testing-library/react';
+ errors: [{ line: 4, column: 29, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor, render } from '${testingFramework}';
await waitFor(() => render())
`,
- errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
- const { rerender } = render()
- await waitFor(() => rerender())
- `,
- errors: [{ line: 4, column: 29, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => renderHelper())
`,
- errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
import { render } from 'somewhere-else';
await waitFor(() => render())
`,
- errors: [{ line: 4, column: 29, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- settings: { 'testing-library/utils-module': '~/test-utils' },
- code: `
+ errors: [{ line: 4, column: 29, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ ]),
+ {
+ settings: { 'testing-library/utils-module': '~/test-utils' },
+ code: `
import { waitFor, render } from '~/test-utils';
await waitFor(() => render())
`,
- errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- settings: { 'testing-library/custom-renders': ['renderWrapper'] },
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ settings: { 'testing-library/custom-renders': ['renderWrapper'] },
+ code: `
+ import { waitFor } from '${testingFramework}';
import { renderWrapper } from 'somewhere-else';
await waitFor(() => renderWrapper())
`,
- errors: [{ line: 4, column: 29, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 4, column: 29, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => {
render()
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => {
const { container } = render()
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => {
result = render()
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => {
const a = 5,
{ container } = render()
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
const { rerender } = render()
await waitFor(() => {
rerender()
})
`,
- errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => {
render()
fireEvent.keyDown(input, {key: 'ArrowDown'})
})
`,
- errors: [
- { line: 4, column: 11, messageId: 'noSideEffectsWaitFor' },
- { line: 5, column: 11, messageId: 'noSideEffectsWaitFor' },
- ],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [
+ { line: 4, column: 11, messageId: 'noSideEffectsWaitFor' },
+ { line: 5, column: 11, messageId: 'noSideEffectsWaitFor' },
+ ],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => {
render()
userEvent.click(button)
})
`,
- errors: [
- { line: 4, column: 11, messageId: 'noSideEffectsWaitFor' },
- { line: 5, column: 11, messageId: 'noSideEffectsWaitFor' },
- ],
- },
- // fireEvent
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [
+ { line: 4, column: 11, messageId: 'noSideEffectsWaitFor' },
+ { line: 5, column: 11, messageId: 'noSideEffectsWaitFor' },
+ ],
+ } as const,
+ ]),
+ // fireEvent
+ ...SUPPORTED_TESTING_FRAMEWORKS.map(
+ (testingFramework) =>
+ ({
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => fireEvent.keyDown(input, {key: 'ArrowDown'}))
`,
- errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- settings: { 'testing-library/utils-module': '~/test-utils' },
- code: `
+ errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
+ }) as const
+ ),
+ {
+ settings: { 'testing-library/utils-module': '~/test-utils' },
+ code: `
import { waitFor, fireEvent } from '~/test-utils';
await waitFor(() => fireEvent.keyDown(input, {key: 'ArrowDown'}))
`,
- errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => {
fireEvent.keyDown(input, {key: 'ArrowDown'})
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor, fireEvent as renamedFireEvent } from '@testing-library/react';
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor, fireEvent as renamedFireEvent } from '${testingFramework}';
await waitFor(() => {
renamedFireEvent.keyDown(input, {key: 'ArrowDown'})
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- settings: { 'testing-library/utils-module': '~/test-utils' },
- code: `
- import { waitFor, fireEvent } from '~/test-utils';
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ ]),
+ {
+ settings: { 'testing-library/utils-module': '~/test-utils' },
+ code: `
+ import { waitFor, fireEvent } from '~/test-utils';
await waitFor(() => {
fireEvent.keyDown(input, {key: 'ArrowDown'})
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => {
expect(b).toEqual('b')
fireEvent.keyDown(input, {key: 'ArrowDown'})
})
`,
- errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => {
fireEvent.keyDown(input, {key: 'ArrowDown'})
expect(b).toEqual('b')
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(function() {
fireEvent.keyDown(input, {key: 'ArrowDown'})
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(function() {
expect(b).toEqual('b')
fireEvent.keyDown(input, {key: 'ArrowDown'})
})
`,
- errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(function() {
fireEvent.keyDown(input, {key: 'ArrowDown'})
expect(b).toEqual('b')
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- // userEvent
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ ]),
+ // userEvent
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => userEvent.click(button))
`,
- errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 3, column: 29, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => {
userEvent.click(button)
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
- import renamedUserEvent from '@testing-library/user-event'
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
+ import renamedUserEvent from '@testing-library/user-event'
await waitFor(() => {
renamedUserEvent.click(button)
})
`,
- errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- settings: { 'testing-library/utils-module': '~/test-utils' },
- code: `
+ errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ ]),
+ {
+ settings: { 'testing-library/utils-module': '~/test-utils' },
+ code: `
import { waitFor } from '~/test-utils';
- import userEvent from '@testing-library/user-event'
+ import userEvent from '@testing-library/user-event'
await waitFor(() => {
userEvent.click();
})
`,
- errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ },
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => {
expect(b).toEqual('b')
userEvent.click(button)
})
`,
- errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(() => {
userEvent.click(button)
expect(b).toEqual('b')
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(function() {
userEvent.click(button)
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(function() {
expect(b).toEqual('b')
userEvent.click(button)
})
`,
- errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
- {
- code: `
- import { waitFor } from '@testing-library/react';
+ errors: [{ line: 5, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
await waitFor(function() {
userEvent.click(button)
expect(b).toEqual('b')
})
`,
- errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
- },
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ // Issue #500, https://github.com/testing-library/eslint-plugin-testing-library/issues/500
+ code: `
+ import { waitFor } from '${testingFramework}';
+ waitFor(function() {
+ userEvent.click(button)
+ expect(b).toEqual('b')
+ }).then(() => {
+ userEvent.click(button) // Side effects are allowed inside .then()
+ expect(b).toEqual('b')
+ })
+ `,
+ errors: [{ line: 4, column: 11, messageId: 'noSideEffectsWaitFor' }],
+ } as const,
+ {
+ // Issue #500, https://github.com/testing-library/eslint-plugin-testing-library/issues/500
+ code: `
+ import { waitFor } from '${testingFramework}';
+ waitFor(function() {
+ userEvent.click(button)
+ expect(b).toEqual('b')
+ }).then(() => {
+ userEvent.click(button) // Side effects are allowed inside .then()
+ expect(b).toEqual('b')
+ await waitFor(() => {
+ fireEvent.keyDown(input, {key: 'ArrowDown'}) // But not if there is a another waitFor with side effects inside the .then()
+ expect(b).toEqual('b')
+ })
+ })
+ `,
+ errors: [
+ { line: 4, column: 11, messageId: 'noSideEffectsWaitFor' },
+ { line: 10, column: 13, messageId: 'noSideEffectsWaitFor' },
+ ],
+ } as const,
+ ]),
- {
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `// all mixed
+ {
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `// all mixed
import { waitFor, fireEvent as renamedFireEvent, screen } from '~/test-utils';
import userEvent from '@testing-library/user-event'
import { fireEvent } from 'somewhere-else'
-
+
test('check all mixed', async () => {
const button = await screen.findByRole('button')
await waitFor(() => {
@@ -713,10 +787,48 @@ ruleTester.run(RULE_NAME, rule, {
})
})
`,
- errors: [
- { line: 9, column: 13, messageId: 'noSideEffectsWaitFor' },
- { line: 12, column: 13, messageId: 'noSideEffectsWaitFor' },
- ],
- },
- ],
+ errors: [
+ { line: 9, column: 13, messageId: 'noSideEffectsWaitFor' },
+ { line: 12, column: 13, messageId: 'noSideEffectsWaitFor' },
+ ],
+ },
+ // side effects (userEvent, fireEvent or render) in variable declarations
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ // Issue #368, https://github.com/testing-library/eslint-plugin-testing-library/issues/368
+ code: `
+ import { waitFor } from '${testingFramework}';
+ import userEvent from '@testing-library/user-event'
+ await waitFor(() => {
+ const a = userEvent.click(button);
+ const b = fireEvent.click(button);
+ const wrapper = render();
+ })
+ `,
+ errors: [
+ { line: 5, column: 11, messageId: 'noSideEffectsWaitFor' },
+ { line: 6, column: 11, messageId: 'noSideEffectsWaitFor' },
+ { line: 7, column: 11, messageId: 'noSideEffectsWaitFor' },
+ ],
+ } as const,
+ ]),
+
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap>(
+ (testingFramework) => [
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
+ import userEvent from '@testing-library/user-event'
+
+ it("some test", async () => {
+ await waitFor(async () => {
+ await fireEvent.click(screen.getByTestId("something"));
+ });
+ });
+ `,
+ errors: [{ line: 7, column: 13, messageId: 'noSideEffectsWaitFor' }],
+ },
+ ]
+ ),
+ ],
});
diff --git a/tests/lib/rules/no-wait-for-snapshot.test.ts b/tests/lib/rules/no-wait-for-snapshot.test.ts
index b6f3e474..03eff8f4 100644
--- a/tests/lib/rules/no-wait-for-snapshot.test.ts
+++ b/tests/lib/rules/no-wait-for-snapshot.test.ts
@@ -4,68 +4,78 @@ import { createRuleTester } from '../test-utils';
const ruleTester = createRuleTester();
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ '@testing-library/dom',
+ '@testing-library/angular',
+ '@testing-library/react',
+ '@testing-library/vue',
+ '@marko/testing-library',
+];
+
ruleTester.run(RULE_NAME, rule, {
- valid: [
- ...ASYNC_UTILS.map((asyncUtil) => ({
- code: `
- import { ${asyncUtil} } from '@testing-library/dom';
- test('snapshot calls outside of ${asyncUtil} are valid', () => {
- expect(foo).toMatchSnapshot()
- await ${asyncUtil}(() => expect(foo).toBeDefined())
- expect(foo).toMatchInlineSnapshot()
- })
- `,
- })),
- ...ASYNC_UTILS.map((asyncUtil) => ({
- code: `
- import { ${asyncUtil} } from '@testing-library/dom';
- test('snapshot calls outside of ${asyncUtil} are valid', () => {
- expect(foo).toMatchSnapshot()
- await ${asyncUtil}(() => {
- expect(foo).toBeDefined()
+ valid: [
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ ...ASYNC_UTILS.map((asyncUtil) => ({
+ code: `
+ import { ${asyncUtil} } from '${testingFramework}';
+ test('snapshot calls outside of ${asyncUtil} are valid', () => {
+ expect(foo).toMatchSnapshot()
+ await ${asyncUtil}(() => expect(foo).toBeDefined())
+ expect(foo).toMatchInlineSnapshot()
})
- expect(foo).toMatchInlineSnapshot()
- })
- `,
- })),
- ...ASYNC_UTILS.map((asyncUtil) => ({
- code: `
- import * as asyncUtils from '@testing-library/dom';
- test('snapshot calls outside of ${asyncUtil} are valid', () => {
- expect(foo).toMatchSnapshot()
- await asyncUtils.${asyncUtil}(() => expect(foo).toBeDefined())
- expect(foo).toMatchInlineSnapshot()
- })
- `,
- })),
- ...ASYNC_UTILS.map((asyncUtil) => ({
- code: `
- import * as asyncUtils from '@testing-library/dom';
- test('snapshot calls outside of ${asyncUtil} are valid', () => {
- expect(foo).toMatchSnapshot()
- await asyncUtils.${asyncUtil}(() => {
- expect(foo).toBeDefined()
+ `,
+ })),
+ ...ASYNC_UTILS.map((asyncUtil) => ({
+ code: `
+ import { ${asyncUtil} } from '${testingFramework}';
+ test('snapshot calls outside of ${asyncUtil} are valid', () => {
+ expect(foo).toMatchSnapshot()
+ await ${asyncUtil}(() => {
+ expect(foo).toBeDefined()
+ })
+ expect(foo).toMatchInlineSnapshot()
})
- expect(foo).toMatchInlineSnapshot()
- })
- `,
- })),
- ...ASYNC_UTILS.map((asyncUtil) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ `,
+ })),
+ ...ASYNC_UTILS.map((asyncUtil) => ({
+ code: `
+ import * as asyncUtils from '${testingFramework}';
+ test('snapshot calls outside of ${asyncUtil} are valid', () => {
+ expect(foo).toMatchSnapshot()
+ await asyncUtils.${asyncUtil}(() => expect(foo).toBeDefined())
+ expect(foo).toMatchInlineSnapshot()
+ })
+ `,
+ })),
+ ...ASYNC_UTILS.map((asyncUtil) => ({
+ code: `
+ import * as asyncUtils from '${testingFramework}';
+ test('snapshot calls outside of ${asyncUtil} are valid', () => {
+ expect(foo).toMatchSnapshot()
+ await asyncUtils.${asyncUtil}(() => {
+ expect(foo).toBeDefined()
+ })
+ expect(foo).toMatchInlineSnapshot()
+ })
+ `,
+ })),
+ ]),
+ ...ASYNC_UTILS.map((asyncUtil) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { ${asyncUtil} } from 'some-other-library';
test('aggressive reporting disabled - snapshot calls within ${asyncUtil} not related to Testing Library are valid', async () => {
await ${asyncUtil}(() => expect(foo).toMatchSnapshot());
});
`,
- })),
- ...ASYNC_UTILS.map((asyncUtil) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ })),
+ ...ASYNC_UTILS.map((asyncUtil) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { ${asyncUtil} } from 'some-other-library';
test('(alt) aggressive reporting disabled - snapshot calls within ${asyncUtil} not related to Testing Library are valid', async () => {
await ${asyncUtil}(() => {
@@ -74,23 +84,23 @@ ruleTester.run(RULE_NAME, rule, {
});
});
`,
- })),
- ...ASYNC_UTILS.map((asyncUtil) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ })),
+ ...ASYNC_UTILS.map((asyncUtil) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import * as asyncUtils from 'some-other-library';
test('aggressive reporting disabled - snapshot calls within ${asyncUtil} from wildcard import not related to Testing Library are valid', async () => {
await asyncUtils.${asyncUtil}(() => expect(foo).toMatchSnapshot());
});
`,
- })),
- ...ASYNC_UTILS.map((asyncUtil) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ })),
+ ...ASYNC_UTILS.map((asyncUtil) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import * as asyncUtils from 'some-other-library';
test('(alt) aggressive reporting disabled - snapshot calls within ${asyncUtil} from wildcard import not related to Testing Library are valid', async () => {
await asyncUtils.${asyncUtil}(() => {
@@ -99,23 +109,23 @@ ruleTester.run(RULE_NAME, rule, {
});
});
`,
- })),
- ...ASYNC_UTILS.map((asyncUtil) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ })),
+ ...ASYNC_UTILS.map((asyncUtil) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { ${asyncUtil} } from 'some-other-library';
test('aggressive reporting disabled - inline snapshot calls within ${asyncUtil} import not related to Testing Library are valid', async () => {
await ${asyncUtil}(() => expect(foo).toMatchInlineSnapshot());
});
`,
- })),
- ...ASYNC_UTILS.map((asyncUtil) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ })),
+ ...ASYNC_UTILS.map((asyncUtil) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { ${asyncUtil} } from 'some-other-library';
test('(alt) aggressive reporting disabled - inline snapshot calls within ${asyncUtil} import not related to Testing Library are valid', async () => {
await ${asyncUtil}(() => {
@@ -124,23 +134,23 @@ ruleTester.run(RULE_NAME, rule, {
});
});
`,
- })),
- ...ASYNC_UTILS.map((asyncUtil) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ })),
+ ...ASYNC_UTILS.map((asyncUtil) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import * as asyncUtils from 'some-other-library';
test('aggressive reporting disabled - inline snapshot calls within ${asyncUtil} from wildcard import not related to Testing Library are valid', async () => {
await asyncUtils.${asyncUtil}(() => expect(foo).toMatchInlineSnapshot());
});
`,
- })),
- ...ASYNC_UTILS.map((asyncUtil) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ })),
+ ...ASYNC_UTILS.map((asyncUtil) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import * as asyncUtils from 'some-other-library';
test('(alt) aggressive reporting disabled - inline snapshot calls within ${asyncUtil} from wildcard import not related to Testing Library are valid', async () => {
await asyncUtils.${asyncUtil}(() => {
@@ -149,168 +159,168 @@ ruleTester.run(RULE_NAME, rule, {
});
});
`,
- })),
- ],
- invalid: [
- ...ASYNC_UTILS.map(
- (asyncUtil) =>
- ({
- code: `
- import { ${asyncUtil} } from '@testing-library/dom';
+ })),
+ ],
+ invalid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ ...ASYNC_UTILS.map(
+ (asyncUtil) =>
+ ({
+ code: `
+ import { ${asyncUtil} } from '${testingFramework}';
test('snapshot calls within ${asyncUtil} are not valid', async () => {
await ${asyncUtil}(() => expect(foo).toMatchSnapshot());
});
`,
- errors: [
- {
- line: 4,
- messageId: 'noWaitForSnapshot',
- data: { name: asyncUtil },
- column: 36 + asyncUtil.length,
- },
- ],
- } as const)
- ),
- ...ASYNC_UTILS.map(
- (asyncUtil) =>
- ({
- code: `
- import { ${asyncUtil} } from '@testing-library/dom';
+ errors: [
+ {
+ line: 4,
+ messageId: 'noWaitForSnapshot',
+ data: { name: asyncUtil },
+ column: 36 + asyncUtil.length,
+ },
+ ],
+ }) as const
+ ),
+ ...ASYNC_UTILS.map(
+ (asyncUtil) =>
+ ({
+ code: `
+ import { ${asyncUtil} } from '${testingFramework}';
test('snapshot calls within ${asyncUtil} are not valid', async () => {
await ${asyncUtil}(() => {
expect(foo).toMatchSnapshot()
});
});
`,
- errors: [
- {
- line: 5,
- messageId: 'noWaitForSnapshot',
- data: { name: asyncUtil },
- column: 27,
- },
- ],
- } as const)
- ),
- ...ASYNC_UTILS.map(
- (asyncUtil) =>
- ({
- code: `
- import * as asyncUtils from '@testing-library/dom';
+ errors: [
+ {
+ line: 5,
+ messageId: 'noWaitForSnapshot',
+ data: { name: asyncUtil },
+ column: 27,
+ },
+ ],
+ }) as const
+ ),
+ ...ASYNC_UTILS.map(
+ (asyncUtil) =>
+ ({
+ code: `
+ import * as asyncUtils from '${testingFramework}';
test('snapshot calls within ${asyncUtil} are not valid', async () => {
await asyncUtils.${asyncUtil}(() => expect(foo).toMatchSnapshot());
});
`,
- errors: [
- {
- line: 4,
- messageId: 'noWaitForSnapshot',
- data: { name: asyncUtil },
- column: 47 + asyncUtil.length,
- },
- ],
- } as const)
- ),
- ...ASYNC_UTILS.map(
- (asyncUtil) =>
- ({
- code: `
- import * as asyncUtils from '@testing-library/dom';
+ errors: [
+ {
+ line: 4,
+ messageId: 'noWaitForSnapshot',
+ data: { name: asyncUtil },
+ column: 47 + asyncUtil.length,
+ },
+ ],
+ }) as const
+ ),
+ ...ASYNC_UTILS.map(
+ (asyncUtil) =>
+ ({
+ code: `
+ import * as asyncUtils from '${testingFramework}';
test('snapshot calls within ${asyncUtil} are not valid', async () => {
await asyncUtils.${asyncUtil}(() => {
expect(foo).toMatchSnapshot()
});
});
`,
- errors: [
- {
- line: 5,
- messageId: 'noWaitForSnapshot',
- data: { name: asyncUtil },
- column: 27,
- },
- ],
- } as const)
- ),
- ...ASYNC_UTILS.map(
- (asyncUtil) =>
- ({
- code: `
- import { ${asyncUtil} } from '@testing-library/dom';
+ errors: [
+ {
+ line: 5,
+ messageId: 'noWaitForSnapshot',
+ data: { name: asyncUtil },
+ column: 27,
+ },
+ ],
+ }) as const
+ ),
+ ...ASYNC_UTILS.map(
+ (asyncUtil) =>
+ ({
+ code: `
+ import { ${asyncUtil} } from '${testingFramework}';
test('snapshot calls within ${asyncUtil} are not valid', async () => {
await ${asyncUtil}(() => expect(foo).toMatchInlineSnapshot());
});
`,
- errors: [
- {
- line: 4,
- messageId: 'noWaitForSnapshot',
- data: { name: asyncUtil },
- column: 36 + asyncUtil.length,
- },
- ],
- } as const)
- ),
- ...ASYNC_UTILS.map(
- (asyncUtil) =>
- ({
- code: `
- import { ${asyncUtil} } from '@testing-library/dom';
+ errors: [
+ {
+ line: 4,
+ messageId: 'noWaitForSnapshot',
+ data: { name: asyncUtil },
+ column: 36 + asyncUtil.length,
+ },
+ ],
+ }) as const
+ ),
+ ...ASYNC_UTILS.map(
+ (asyncUtil) =>
+ ({
+ code: `
+ import { ${asyncUtil} } from '${testingFramework}';
test('snapshot calls within ${asyncUtil} are not valid', async () => {
await ${asyncUtil}(() => {
expect(foo).toMatchInlineSnapshot()
});
});
`,
- errors: [
- {
- line: 5,
- messageId: 'noWaitForSnapshot',
- data: { name: asyncUtil },
- column: 27,
- },
- ],
- } as const)
- ),
- ...ASYNC_UTILS.map(
- (asyncUtil) =>
- ({
- code: `
- import * as asyncUtils from '@testing-library/dom';
+ errors: [
+ {
+ line: 5,
+ messageId: 'noWaitForSnapshot',
+ data: { name: asyncUtil },
+ column: 27,
+ },
+ ],
+ }) as const
+ ),
+ ...ASYNC_UTILS.map(
+ (asyncUtil) =>
+ ({
+ code: `
+ import * as asyncUtils from '${testingFramework}';
test('snapshot calls within ${asyncUtil} are not valid', async () => {
await asyncUtils.${asyncUtil}(() => expect(foo).toMatchInlineSnapshot());
});
`,
- errors: [
- {
- line: 4,
- messageId: 'noWaitForSnapshot',
- data: { name: asyncUtil },
- column: 47 + asyncUtil.length,
- },
- ],
- } as const)
- ),
- ...ASYNC_UTILS.map(
- (asyncUtil) =>
- ({
- code: `
- import * as asyncUtils from '@testing-library/dom';
+ errors: [
+ {
+ line: 4,
+ messageId: 'noWaitForSnapshot',
+ data: { name: asyncUtil },
+ column: 47 + asyncUtil.length,
+ },
+ ],
+ }) as const
+ ),
+ ...ASYNC_UTILS.map(
+ (asyncUtil) =>
+ ({
+ code: `
+ import * as asyncUtils from '${testingFramework}';
test('snapshot calls within ${asyncUtil} are not valid', async () => {
await asyncUtils.${asyncUtil}(() => {
expect(foo).toMatchInlineSnapshot()
});
});
`,
- errors: [
- {
- line: 5,
- messageId: 'noWaitForSnapshot',
- data: { name: asyncUtil },
- column: 27,
- },
- ],
- } as const)
- ),
- ],
+ errors: [
+ {
+ line: 5,
+ messageId: 'noWaitForSnapshot',
+ data: { name: asyncUtil },
+ column: 27,
+ },
+ ],
+ }) as const
+ ),
+ ]),
});
diff --git a/tests/lib/rules/prefer-explicit-assert.test.ts b/tests/lib/rules/prefer-explicit-assert.test.ts
index 08dd5fb8..8cc09614 100644
--- a/tests/lib/rules/prefer-explicit-assert.test.ts
+++ b/tests/lib/rules/prefer-explicit-assert.test.ts
@@ -7,283 +7,283 @@ const ruleTester = createRuleTester();
const COMBINED_QUERIES_METHODS = [...ALL_QUERIES_METHODS, 'ByIcon'];
ruleTester.run(RULE_NAME, rule, {
- valid: [
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `get${queryMethod}('Hello')`,
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `get${queryMethod}`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `
+ valid: [
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `get${queryMethod}('Hello')`,
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `get${queryMethod}`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `
const utils = render()
utils.get${queryMethod}
`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `screen.get${queryMethod}`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `expect(get${queryMethod}('foo')).toBeDefined()`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `screen.get${queryMethod}`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `expect(get${queryMethod}('foo')).toBeDefined()`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `
const utils = render()
expect(utils.get${queryMethod}('foo')).toBeDefined()
`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `expect(screen.get${queryMethod}('foo')).toBeDefined()`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `expect(getBy${queryMethod}('foo').bar).toBeInTheDocument()`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `expect(screen.get${queryMethod}('foo')).toBeDefined()`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `expect(getBy${queryMethod}('foo').bar).toBeInTheDocument()`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `
async () => {
await waitForElement(() => get${queryMethod}('foo'))
}
`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `fireEvent.click(get${queryMethod}('bar'));`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `const quxElement = get${queryMethod}('qux')`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `fireEvent.click(get${queryMethod}('bar'));`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `const quxElement = get${queryMethod}('qux')`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `
async () => {
const quxElement = await find${queryMethod}('qux')
}`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `
async () => {
expect(await find${queryMethod}('qux')).toBeInTheDocument();
}`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `
async () => {
await find${queryMethod}('foo')
}`,
- options: [
- {
- includeFindQueries: false,
- },
- ],
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `const quxElement = find${queryMethod}('qux')`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `const quxElement = screen.find${queryMethod}('qux')`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `
+ options: [
+ {
+ includeFindQueries: false,
+ },
+ ],
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `const quxElement = find${queryMethod}('qux')`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `const quxElement = screen.find${queryMethod}('qux')`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `
async () => {
const quxElement = await screen.find${queryMethod}('qux')
}`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `
function findBySubmit() {
return screen.find${queryMethod}('foo')
}`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `
function findBySubmit() {
return find${queryMethod}('foo')
}`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `
() => { return screen.find${queryMethod}('foo') }`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `
() => { return find${queryMethod}('foo') }`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `
() => screen.find${queryMethod}('foo')`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `
() => find${queryMethod}('foo')`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `() => { return get${queryMethod}('foo') }`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `function bar() { return get${queryMethod}('foo') }`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `const { get${queryMethod} } = render()`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `it('test', () => { const { get${queryMethod} } = render() })`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `it('test', () => { const [ get${queryMethod} ] = render() })`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `const a = [ get${queryMethod}('foo') ]`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `const a = { foo: get${queryMethod}('bar') }`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `query${queryMethod}("foo")`,
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `() => { return get${queryMethod}('foo') }`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `function bar() { return get${queryMethod}('foo') }`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `const { get${queryMethod} } = render()`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `it('test', () => { const { get${queryMethod} } = render() })`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `it('test', () => { const [ get${queryMethod} ] = render() })`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `const a = [ get${queryMethod}('foo') ]`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `const a = { foo: get${queryMethod}('bar') }`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `query${queryMethod}("foo")`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `
expect(get${queryMethod}('foo')).toBeTruthy()
fireEvent.click(get${queryMethod}('bar'));
`,
- options: [
- {
- assertion: 'toBeTruthy',
- },
- ],
- })),
- ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
- code: `expect(get${queryMethod}('foo')).toBeEnabled()`,
- options: [
- {
- assertion: 'toBeInTheDocument',
- },
- ],
- })),
- {
- // https://github.com/testing-library/eslint-plugin-testing-library/issues/475
- code: `
+ options: [
+ {
+ assertion: 'toBeTruthy',
+ },
+ ],
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `expect(get${queryMethod}('foo')).toBeEnabled()`,
+ options: [
+ {
+ assertion: 'toBeInTheDocument',
+ },
+ ],
+ })),
+ {
+ // https://github.com/testing-library/eslint-plugin-testing-library/issues/475
+ code: `
// incomplete expect statement should be ignored
expect('something');
expect(getByText('foo'));
`,
- options: [
- {
- assertion: 'toBeInTheDocument',
- },
- ],
- },
- ],
- invalid: [
- ...COMBINED_QUERIES_METHODS.map(
- (queryMethod) =>
- ({
- code: `get${queryMethod}('foo')`,
- errors: [
- {
- messageId: 'preferExplicitAssert',
- data: {
- queryType: 'getBy*',
- },
- },
- ],
- } as const)
- ),
- ...COMBINED_QUERIES_METHODS.map(
- (queryMethod) =>
- ({
- code: `find${queryMethod}('foo')`,
- errors: [
- {
- messageId: 'preferExplicitAssert',
- data: { queryType: 'findBy*' },
- },
- ],
- } as const)
- ),
- ...COMBINED_QUERIES_METHODS.map(
- (queryMethod) =>
- ({
- code: `screen.find${queryMethod}('foo')`,
- errors: [
- {
- messageId: 'preferExplicitAssert',
- data: { queryType: 'findBy*' },
- },
- ],
- } as const)
- ),
- ...COMBINED_QUERIES_METHODS.map(
- (queryMethod) =>
- ({
- code: `
+ options: [
+ {
+ assertion: 'toBeInTheDocument',
+ },
+ ],
+ },
+ ],
+ invalid: [
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `get${queryMethod}('foo')`,
+ errors: [
+ {
+ messageId: 'preferExplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `find${queryMethod}('foo')`,
+ errors: [
+ {
+ messageId: 'preferExplicitAssert',
+ data: { queryType: 'findBy*' },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `screen.find${queryMethod}('foo')`,
+ errors: [
+ {
+ messageId: 'preferExplicitAssert',
+ data: { queryType: 'findBy*' },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `
async () => {
await screen.find${queryMethod}('foo')
}
`,
- errors: [
- {
- messageId: 'preferExplicitAssert',
- data: { queryType: 'findBy*' },
- },
- ],
- } as const)
- ),
- ...COMBINED_QUERIES_METHODS.map(
- (queryMethod) =>
- ({
- code: `
+ errors: [
+ {
+ messageId: 'preferExplicitAssert',
+ data: { queryType: 'findBy*' },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `
async () => {
await find${queryMethod}('foo')
}
`,
- errors: [
- {
- messageId: 'preferExplicitAssert',
- data: { queryType: 'findBy*' },
- },
- ],
- } as const)
- ),
- ...COMBINED_QUERIES_METHODS.map(
- (queryMethod) =>
- ({
- code: `
+ errors: [
+ {
+ messageId: 'preferExplicitAssert',
+ data: { queryType: 'findBy*' },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `
const utils = render()
utils.get${queryMethod}('foo')
`,
- errors: [
- {
- messageId: 'preferExplicitAssert',
- line: 3,
- column: 15,
- data: {
- queryType: 'getBy*',
- },
- },
- ],
- } as const)
- ),
- ...COMBINED_QUERIES_METHODS.map(
- (queryMethod) =>
- ({
- code: `screen.get${queryMethod}('foo')`,
- errors: [
- {
- messageId: 'preferExplicitAssert',
- line: 1,
- column: 8,
- data: {
- queryType: 'getBy*',
- },
- },
- ],
- } as const)
- ),
- ...COMBINED_QUERIES_METHODS.map(
- (queryMethod) =>
- ({
- code: `
+ errors: [
+ {
+ messageId: 'preferExplicitAssert',
+ line: 3,
+ column: 15,
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `screen.get${queryMethod}('foo')`,
+ errors: [
+ {
+ messageId: 'preferExplicitAssert',
+ line: 1,
+ column: 8,
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `
() => {
get${queryMethod}('foo')
doSomething()
@@ -292,102 +292,94 @@ ruleTester.run(RULE_NAME, rule, {
const quxElement = get${queryMethod}('qux')
}
`,
- errors: [
- {
- messageId: 'preferExplicitAssert',
- line: 3,
- data: {
- queryType: 'getBy*',
- },
- },
- {
- messageId: 'preferExplicitAssert',
- line: 6,
- data: {
- queryType: 'getBy*',
- },
- },
- ],
- } as const)
- ),
- ...COMBINED_QUERIES_METHODS.map(
- (queryMethod) =>
- ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [
+ {
+ messageId: 'preferExplicitAssert',
+ line: 3,
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ {
+ messageId: 'preferExplicitAssert',
+ line: 6,
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import "test-utils"
getBy${queryMethod}("Hello")
`,
- errors: [
- {
- messageId: 'preferExplicitAssert',
- data: {
- queryType: 'getBy*',
- },
- },
- ],
- } as const)
- ),
- {
- code: `getByIcon('foo')`, // custom `getBy` query extended through options
- errors: [
- {
- messageId: 'preferExplicitAssert',
- },
- ],
- },
- ...COMBINED_QUERIES_METHODS.map(
- (queryMethod) =>
- ({
- code: `expect(get${queryMethod}('foo')).toBeDefined()`,
- options: [
- {
- assertion: 'toBeInTheDocument',
- },
- ],
- errors: [
- {
- messageId: 'preferExplicitAssertAssertion',
- data: { assertion: 'toBeInTheDocument' },
- },
- ],
- } as const)
- ),
- ...COMBINED_QUERIES_METHODS.map(
- (queryMethod) =>
- ({
- code: `expect(get${queryMethod}('foo')).not.toBeNull()`,
- options: [
- {
- assertion: 'toBeInTheDocument',
- },
- ],
- errors: [
- {
- messageId: 'preferExplicitAssertAssertion',
- data: { assertion: 'toBeInTheDocument' },
- },
- ],
- } as const)
- ),
- ...COMBINED_QUERIES_METHODS.map(
- (queryMethod) =>
- ({
- code: `expect(get${queryMethod}('foo')).not.toBeFalsy()`,
- options: [
- {
- assertion: 'toBeInTheDocument',
- },
- ],
- errors: [
- {
- messageId: 'preferExplicitAssertAssertion',
- data: { assertion: 'toBeInTheDocument' },
- },
- ],
- } as const)
- ),
- ],
+ errors: [
+ {
+ messageId: 'preferExplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(get${queryMethod}('foo')).toBeDefined()`,
+ options: [
+ {
+ assertion: 'toBeInTheDocument',
+ },
+ ],
+ errors: [
+ {
+ messageId: 'preferExplicitAssertAssertion',
+ data: { assertion: 'toBeInTheDocument' },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(get${queryMethod}('foo')).not.toBeNull()`,
+ options: [
+ {
+ assertion: 'toBeInTheDocument',
+ },
+ ],
+ errors: [
+ {
+ messageId: 'preferExplicitAssertAssertion',
+ data: { assertion: 'toBeInTheDocument' },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(get${queryMethod}('foo')).not.toBeFalsy()`,
+ options: [
+ {
+ assertion: 'toBeInTheDocument',
+ },
+ ],
+ errors: [
+ {
+ messageId: 'preferExplicitAssertAssertion',
+ data: { assertion: 'toBeInTheDocument' },
+ },
+ ],
+ }) as const
+ ),
+ ],
});
diff --git a/tests/lib/rules/prefer-find-by.test.ts b/tests/lib/rules/prefer-find-by.test.ts
index aaafdaef..b4728c15 100644
--- a/tests/lib/rules/prefer-find-by.test.ts
+++ b/tests/lib/rules/prefer-find-by.test.ts
@@ -1,103 +1,116 @@
-import { TSESLint } from '@typescript-eslint/experimental-utils';
+import {
+ type InvalidTestCase,
+ type ValidTestCase,
+} from '@typescript-eslint/rule-tester';
import rule, {
- WAIT_METHODS,
- RULE_NAME,
- getFindByQueryVariant,
- MessageIds,
+ RULE_NAME,
+ getFindByQueryVariant,
+ MessageIds,
} from '../../../lib/rules/prefer-find-by';
import {
- ASYNC_QUERIES_COMBINATIONS,
- SYNC_QUERIES_COMBINATIONS,
+ ASYNC_QUERIES_COMBINATIONS,
+ SYNC_QUERIES_COMBINATIONS,
} from '../../../lib/utils';
import { createRuleTester } from '../test-utils';
const ruleTester = createRuleTester();
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ '@testing-library/dom',
+ '@testing-library/angular',
+ '@testing-library/react',
+ '@testing-library/vue',
+ '@marko/testing-library',
+];
+
function buildFindByMethod(queryMethod: string) {
- return `${getFindByQueryVariant(queryMethod)}${queryMethod.split('By')[1]}`;
+ return `${getFindByQueryVariant(queryMethod)}${queryMethod.split('By')[1]}`;
}
function createScenario<
- T extends
- | TSESLint.InvalidTestCase
- | TSESLint.ValidTestCase<[]>
+ T extends InvalidTestCase | ValidTestCase<[]>,
>(callback: (waitMethod: string, queryMethod: string) => T) {
- return WAIT_METHODS.reduce(
- (acc: T[], waitMethod) =>
- acc.concat(
- SYNC_QUERIES_COMBINATIONS.map((queryMethod) =>
- callback(waitMethod, queryMethod)
- )
- ),
- []
- );
+ return SYNC_QUERIES_COMBINATIONS.map((queryMethod) =>
+ callback('waitFor', queryMethod)
+ );
}
ruleTester.run(RULE_NAME, rule, {
- valid: [
- ...ASYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
- code: `
+ valid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ ...ASYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ code: `
it('tests', async () => {
- const { ${queryMethod} } = setup()
+ const { ${queryMethod} } = setup() // implicitly using ${testingFramework}
const submitButton = await ${queryMethod}('foo')
})
`,
- })),
- ...ASYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
- code: `
- import {screen} from '@testing-library/foo';
+ })),
+ ...ASYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ code: `
+ import {screen} from '${testingFramework}';
it('tests', async () => {
const submitButton = await screen.${queryMethod}('foo')
})
`,
- })),
- ...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
- code: `
- import {waitForElementToBeRemoved} from '@testing-library/foo';
+ })),
+ ...ASYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ code: `
+ import {waitFor} from '${testingFramework}';
+ it('tests', async () => {
+ await waitFor(async () => {
+ const button = screen.${queryMethod}("button", { name: "Submit" })
+ expect(button).toBeInTheDocument()
+ })
+ })
+ `,
+ })),
+ ...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ code: `
+ import {waitForElementToBeRemoved} from '${testingFramework}';
it('tests', async () => {
await waitForElementToBeRemoved(() => ${queryMethod}(baz))
})
`,
- })),
- ...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
- code: `
- import {waitFor} from '@testing-library/foo';
+ })),
+ ...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ code: `
+ import {waitFor} from '${testingFramework}';
it('tests', async () => {
await waitFor(function() {
return ${queryMethod}('baz', { name: 'foo' })
})
})
`,
- })),
- {
- code: `
- import {waitFor} from '@testing-library/foo';
+ })),
+ {
+ code: `
+ import {waitFor} from '${testingFramework}';
it('tests', async () => {
await waitFor(() => myCustomFunction())
})
`,
- },
- {
- code: `
- import {waitFor} from '@testing-library/foo';
+ },
+ {
+ code: `
+ import {waitFor} from '${testingFramework}';
it('tests', async () => {
await waitFor(customFunctionReference)
})
`,
- },
- {
- code: `
- import {waitForElementToBeRemoved} from '@testing-library/foo';
+ },
+ {
+ code: `
+ import {waitForElementToBeRemoved} from '${testingFramework}';
it('tests', async () => {
const { container } = render()
await waitForElementToBeRemoved(container.querySelector('foo'))
})
`,
- },
- ...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
- code: `
- import {waitFor} from '@testing-library/foo';
+ },
+ ...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ code: `
+ import {waitFor} from '${testingFramework}';
it('tests', async () => {
await waitFor(() => {
foo()
@@ -105,579 +118,632 @@ ruleTester.run(RULE_NAME, rule, {
})
})
`,
- })),
- ...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
- code: `
- import {screen, waitFor} from '@testing-library/foo';
+ })),
+ ...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ code: `
+ import {screen, waitFor} from '${testingFramework}';
it('tests', async () => {
await waitFor(() => expect(screen.${queryMethod}('baz')).toBeDisabled());
})
`,
- })),
- ...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
- code: `
- import {screen, waitFor} from '@testing-library/foo';
+ })),
+ ...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ code: `
+ import {screen, waitFor} from '${testingFramework}';
it('tests', async () => {
const { ${queryMethod} } = render()
await waitFor(() => expect(${queryMethod}('baz')).toBeDisabled());
})
`,
- })),
- ...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
- code: `
- import {waitFor} from '@testing-library/foo';
+ })),
+ ...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ code: `
+ import {waitFor} from '${testingFramework}';
it('tests', async () => {
await waitFor(() => expect(screen.${queryMethod}('baz')).not.toBeInTheDocument());
})
`,
- })),
- ...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
- code: `
- import {waitFor} from '@testing-library/foo';
+ })),
+ ...SYNC_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ code: `
+ import {waitFor} from '${testingFramework}';
it('tests', async () => {
const { ${queryMethod} } = render()
await waitFor(() => expect(${queryMethod}('baz')).not.toBeInTheDocument());
})
`,
- })),
- {
- code: `
- import {waitFor} from '@testing-library/foo';
+ })),
+ {
+ code: `
+ import {waitFor} from '${testingFramework}';
it('tests', async () => {
await waitFor();
- await wait();
})
`,
- },
- ],
- invalid: [
- ...createScenario((waitMethod: string, queryMethod: string) => ({
- code: `
- import {${waitMethod}, screen} from '@testing-library/foo';
+ },
+ {
+ code: `
+ import {screen, waitFor} from '${testingFramework}';
+ it('tests', async () => {
+ await waitFor(() => expect(screen.querySelector('baz')).toBeInTheDocument());
+ })
+ `,
+ },
+ {
+ code: `
+ import {waitFor} from '${testingFramework}';
+ it('tests', async () => {
+ const { container } = render()
+ await waitFor(() => expect(container.querySelector('baz')).toBeInTheDocument());
+ })
+ `,
+ },
+ {
+ code: `
+ import {waitFor} from '${testingFramework}';
+ it('tests', async () => {
+ await waitFor(async () => {
+ const button = await foo("button", { name: "Submit" })
+ expect(button).toBeInTheDocument()
+ })
+ })
+ `,
+ },
+ ]),
+ invalid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ ...createScenario((waitMethod, queryMethod) => ({
+ code: `
+ import {${waitMethod}, screen} from '${testingFramework}';
it('tests', async () => {
const submitButton = await ${waitMethod}(() => screen.${queryMethod}('foo', { name: 'baz' }))
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: getFindByQueryVariant(queryMethod),
- queryMethod: queryMethod.split('By')[1],
- prevQuery: queryMethod,
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod}, screen} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: getFindByQueryVariant(queryMethod),
+ queryMethod: queryMethod.split('By')[1],
+ prevQuery: queryMethod,
+ waitForMethodName: waitMethod,
+ },
+ },
+ ],
+ output: `
+ import {${waitMethod}, screen} from '${testingFramework}';
it('tests', async () => {
const submitButton = await screen.${buildFindByMethod(
- queryMethod
- )}('foo', { name: 'baz' })
+ queryMethod
+ )}('foo', { name: 'baz' })
})
`,
- })),
- // // this scenario verifies it works when the render function is defined in another scope
- ...WAIT_METHODS.map(
- (waitMethod: string) =>
- ({
- code: `
- import {${waitMethod}} from '@testing-library/foo';
+ })),
+ // // this scenario verifies it works when the render function is defined in another scope
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
const { getByText, queryByLabelText, findAllByRole } = customRender()
it('tests', async () => {
- const submitButton = await ${waitMethod}(() => getByText('baz', { name: 'button' }))
+ const submitButton = await waitFor(() => getByText('baz', { name: 'button' }))
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: 'findBy',
- queryMethod: 'Text',
- prevQuery: 'getByText',
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod}} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: 'findBy',
+ queryMethod: 'Text',
+ prevQuery: 'getByText',
+ },
+ },
+ ],
+ output: `
+ import { waitFor } from '${testingFramework}';
const { getByText, queryByLabelText, findAllByRole, findByText } = customRender()
it('tests', async () => {
const submitButton = await findByText('baz', { name: 'button' })
})
`,
- } as const)
- ),
- // // this scenario verifies when findBy* were already defined (because it was used elsewhere)
- ...WAIT_METHODS.map(
- (waitMethod: string) =>
- ({
- code: `
- import {${waitMethod}} from '@testing-library/foo';
+ },
+ // // this scenario verifies when findBy* were already defined (because it was used elsewhere)
+ {
+ code: `
+ import { waitFor } from '${testingFramework}';
const { getAllByRole, findAllByRole } = customRender()
it('tests', async () => {
- const submitButton = await ${waitMethod}(() => getAllByRole('baz', { name: 'button' }))
+ const submitButton = await waitFor(() => getAllByRole('baz', { name: 'button' }))
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: 'findAllBy',
- queryMethod: 'Role',
- prevQuery: 'getAllByRole',
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod}} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: 'findAllBy',
+ queryMethod: 'Role',
+ prevQuery: 'getAllByRole',
+ },
+ },
+ ],
+ output: `
+ import { waitFor } from '${testingFramework}';
const { getAllByRole, findAllByRole } = customRender()
it('tests', async () => {
const submitButton = await findAllByRole('baz', { name: 'button' })
})
`,
- } as const)
- ),
- // invalid code, as we need findBy* to be defined somewhere, but required for getting 100% coverage
- {
- code: `const submitButton = await waitFor(() => getByText('baz', { name: 'button' }))`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: 'findBy',
- queryMethod: 'Text',
- prevQuery: 'getByText',
- waitForMethodName: 'waitFor',
- },
- },
- ],
- output: `const submitButton = await findByText('baz', { name: 'button' })`,
- },
- // this code would be invalid too, as findByRole is not defined anywhere.
- {
- code: `
- const getByRole = render().getByRole
+ },
+ // invalid code, as we need findBy* to be defined somewhere, but required for getting 100% coverage
+ {
+ code: `const submitButton = await waitFor(() => getByText('baz', { name: 'button' })) // implicitly using ${testingFramework}`,
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: 'findBy',
+ queryMethod: 'Text',
+ prevQuery: 'getByText',
+ waitForMethodName: 'waitFor',
+ },
+ },
+ ],
+ output: `const submitButton = await findByText('baz', { name: 'button' }) // implicitly using ${testingFramework}`,
+ },
+ // this code would be invalid too, as findByRole is not defined anywhere.
+ {
+ code: `
+ const getByRole = render().getByRole // implicitly using ${testingFramework}
const submitButton = await waitFor(() => getByRole('baz', { name: 'button' }))
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: 'findBy',
- queryMethod: 'Role',
- prevQuery: 'getByRole',
- waitForMethodName: 'waitFor',
- },
- },
- ],
- output: `
- const getByRole = render().getByRole
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: 'findBy',
+ queryMethod: 'Role',
+ prevQuery: 'getByRole',
+ waitForMethodName: 'waitFor',
+ },
+ },
+ ],
+ output: `
+ const getByRole = render().getByRole // implicitly using ${testingFramework}
const submitButton = await findByRole('baz', { name: 'button' })
`,
- },
- // custom query triggers the error but there is no fix - so output is the same
- ...WAIT_METHODS.map(
- (waitMethod: string) =>
- ({
- code: `
- import {${waitMethod},render} from '@testing-library/foo';
- it('tests', async () => {
- const { getByCustomQuery } = render()
- const submitButton = await ${waitMethod}(() => getByCustomQuery('baz'))
- })
- `,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: 'findBy',
- queryMethod: 'CustomQuery',
- prevQuery: 'getByCustomQuery',
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod},render} from '@testing-library/foo';
+ },
+ // custom query triggers the error but there is no fix - so output is the same
+ {
+ code: `
+ import { waitFor, render} from '${testingFramework}';
it('tests', async () => {
const { getByCustomQuery } = render()
- const submitButton = await ${waitMethod}(() => getByCustomQuery('baz'))
+ const submitButton = await waitFor(() => getByCustomQuery('baz'))
})
`,
- } as const)
- ),
- // custom query triggers the error but there is no fix - so output is the same
- ...WAIT_METHODS.map(
- (waitMethod: string) =>
- ({
- code: `
- import {${waitMethod},render,screen} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: 'findBy',
+ queryMethod: 'CustomQuery',
+ prevQuery: 'getByCustomQuery',
+ },
+ },
+ ],
+ output: null,
+ },
+ // custom query triggers the error but there is no fix - so output is the same
+ {
+ code: `
+ import {waitFor,render,screen} from '${testingFramework}';
it('tests', async () => {
const { getByCustomQuery } = render()
- const submitButton = await ${waitMethod}(() => screen.getByCustomQuery('baz'))
+ const submitButton = await waitFor(() => screen.getByCustomQuery('baz'))
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: 'findBy',
- queryMethod: 'CustomQuery',
- prevQuery: 'getByCustomQuery',
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod},render,screen} from '@testing-library/foo';
- it('tests', async () => {
- const { getByCustomQuery } = render()
- const submitButton = await ${waitMethod}(() => screen.getByCustomQuery('baz'))
- })
- `,
- } as const)
- ),
- // presence matchers
- ...createScenario((waitMethod: string, queryMethod: string) => ({
- code: `
- import {${waitMethod}} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: 'findBy',
+ queryMethod: 'CustomQuery',
+ prevQuery: 'getByCustomQuery',
+ },
+ },
+ ],
+ output: null,
+ },
+ // presence matchers
+ ...createScenario((waitMethod, queryMethod) => ({
+ code: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const { ${queryMethod} } = render()
const submitButton = await ${waitMethod}(() => ${queryMethod}('foo', { name: 'baz' }))
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: getFindByQueryVariant(queryMethod),
- queryMethod: queryMethod.split('By')[1],
- prevQuery: queryMethod,
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod}} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: getFindByQueryVariant(queryMethod),
+ queryMethod: queryMethod.split('By')[1],
+ prevQuery: queryMethod,
+ waitForMethodName: waitMethod,
+ },
+ },
+ ],
+ output: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const { ${queryMethod}, ${buildFindByMethod(queryMethod)} } = render()
const submitButton = await ${buildFindByMethod(
- queryMethod
- )}('foo', { name: 'baz' })
+ queryMethod
+ )}('foo', { name: 'baz' })
})
`,
- })),
- ...createScenario((waitMethod: string, queryMethod: string) => ({
- code: `
- import {${waitMethod}} from '@testing-library/foo';
+ })),
+ ...createScenario((waitMethod, queryMethod) => ({
+ code: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const { ${queryMethod} } = render()
const submitButton = await ${waitMethod}(() => expect(${queryMethod}('foo', { name: 'baz' })).toBeInTheDocument())
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: getFindByQueryVariant(queryMethod),
- queryMethod: queryMethod.split('By')[1],
- prevQuery: queryMethod,
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod}} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: getFindByQueryVariant(queryMethod),
+ queryMethod: queryMethod.split('By')[1],
+ prevQuery: queryMethod,
+ waitForMethodName: waitMethod,
+ },
+ },
+ ],
+ output: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const { ${queryMethod}, ${buildFindByMethod(queryMethod)} } = render()
const submitButton = await ${buildFindByMethod(
- queryMethod
- )}('foo', { name: 'baz' })
+ queryMethod
+ )}('foo', { name: 'baz' })
})
`,
- })),
- ...createScenario((waitMethod: string, queryMethod: string) => ({
- code: `
- import {${waitMethod}} from '@testing-library/foo';
+ })),
+ ...createScenario((waitMethod, queryMethod) => ({
+ code: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const { ${queryMethod} } = render()
const submitButton = await ${waitMethod}(() => expect(${queryMethod}('foo', { name: 'baz' })).toBeDefined())
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: getFindByQueryVariant(queryMethod),
- queryMethod: queryMethod.split('By')[1],
- prevQuery: queryMethod,
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod}} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: getFindByQueryVariant(queryMethod),
+ queryMethod: queryMethod.split('By')[1],
+ prevQuery: queryMethod,
+ waitForMethodName: waitMethod,
+ },
+ },
+ ],
+ output: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const { ${queryMethod}, ${buildFindByMethod(queryMethod)} } = render()
const submitButton = await ${buildFindByMethod(
- queryMethod
- )}('foo', { name: 'baz' })
+ queryMethod
+ )}('foo', { name: 'baz' })
})
`,
- })),
- ...createScenario((waitMethod: string, queryMethod: string) => ({
- code: `
- import {${waitMethod}} from '@testing-library/foo';
+ })),
+ ...createScenario((waitMethod, queryMethod) => ({
+ code: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const { ${queryMethod} } = render()
const submitButton = await ${waitMethod}(() => expect(${queryMethod}('foo', { name: 'baz' })).not.toBeNull())
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: getFindByQueryVariant(queryMethod),
- queryMethod: queryMethod.split('By')[1],
- prevQuery: queryMethod,
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod}} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: getFindByQueryVariant(queryMethod),
+ queryMethod: queryMethod.split('By')[1],
+ prevQuery: queryMethod,
+ waitForMethodName: waitMethod,
+ },
+ },
+ ],
+ output: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const { ${queryMethod}, ${buildFindByMethod(queryMethod)} } = render()
const submitButton = await ${buildFindByMethod(
- queryMethod
- )}('foo', { name: 'baz' })
+ queryMethod
+ )}('foo', { name: 'baz' })
})
`,
- })),
- ...createScenario((waitMethod: string, queryMethod: string) => ({
- code: `
- import {${waitMethod}} from '@testing-library/foo';
+ })),
+ ...createScenario((waitMethod, queryMethod) => ({
+ code: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const {${queryMethod}} = render()
const submitButton = await ${waitMethod}(() => expect(${queryMethod}('foo', { name: 'baz' })).not.toBeNull())
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: getFindByQueryVariant(queryMethod),
- queryMethod: queryMethod.split('By')[1],
- prevQuery: queryMethod,
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod}} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: getFindByQueryVariant(queryMethod),
+ queryMethod: queryMethod.split('By')[1],
+ prevQuery: queryMethod,
+ waitForMethodName: waitMethod,
+ },
+ },
+ ],
+ output: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const {${queryMethod}, ${buildFindByMethod(queryMethod)}} = render()
const submitButton = await ${buildFindByMethod(
- queryMethod
- )}('foo', { name: 'baz' })
+ queryMethod
+ )}('foo', { name: 'baz' })
})
`,
- })),
- ...createScenario((waitMethod: string, queryMethod: string) => ({
- code: `
- import {${waitMethod}} from '@testing-library/foo';
+ })),
+ ...createScenario((waitMethod, queryMethod) => ({
+ code: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const { ${queryMethod} } = render()
const submitButton = await ${waitMethod}(() => expect(${queryMethod}('foo', { name: 'baz' })).toBeTruthy())
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: getFindByQueryVariant(queryMethod),
- queryMethod: queryMethod.split('By')[1],
- prevQuery: queryMethod,
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod}} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: getFindByQueryVariant(queryMethod),
+ queryMethod: queryMethod.split('By')[1],
+ prevQuery: queryMethod,
+ waitForMethodName: waitMethod,
+ },
+ },
+ ],
+ output: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const { ${queryMethod}, ${buildFindByMethod(queryMethod)} } = render()
const submitButton = await ${buildFindByMethod(
- queryMethod
- )}('foo', { name: 'baz' })
+ queryMethod
+ )}('foo', { name: 'baz' })
})
`,
- })),
- ...createScenario((waitMethod: string, queryMethod: string) => ({
- code: `
- import {${waitMethod}} from '@testing-library/foo';
+ })),
+ ...createScenario((waitMethod, queryMethod) => ({
+ code: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const { ${queryMethod} } = render()
const submitButton = await ${waitMethod}(() => expect(${queryMethod}('foo', { name: 'baz' })).not.toBeFalsy())
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: getFindByQueryVariant(queryMethod),
- queryMethod: queryMethod.split('By')[1],
- prevQuery: queryMethod,
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod}} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: getFindByQueryVariant(queryMethod),
+ queryMethod: queryMethod.split('By')[1],
+ prevQuery: queryMethod,
+ waitForMethodName: waitMethod,
+ },
+ },
+ ],
+ output: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const { ${queryMethod}, ${buildFindByMethod(queryMethod)} } = render()
const submitButton = await ${buildFindByMethod(
- queryMethod
- )}('foo', { name: 'baz' })
+ queryMethod
+ )}('foo', { name: 'baz' })
})
`,
- })),
- ...createScenario((waitMethod: string, queryMethod: string) => ({
- code: `
- import {${waitMethod}} from '@testing-library/foo';
+ })),
+ ...createScenario((waitMethod, queryMethod) => ({
+ code: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const submitButton = await ${waitMethod}(() => expect(screen.${queryMethod}('foo', { name: 'baz' })).toBeInTheDocument())
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: getFindByQueryVariant(queryMethod),
- queryMethod: queryMethod.split('By')[1],
- prevQuery: queryMethod,
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod}} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: getFindByQueryVariant(queryMethod),
+ queryMethod: queryMethod.split('By')[1],
+ prevQuery: queryMethod,
+ waitForMethodName: waitMethod,
+ },
+ },
+ ],
+ output: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const submitButton = await screen.${buildFindByMethod(
- queryMethod
- )}('foo', { name: 'baz' })
+ queryMethod
+ )}('foo', { name: 'baz' })
})
`,
- })),
- ...createScenario((waitMethod: string, queryMethod: string) => ({
- code: `
- import {${waitMethod}} from '@testing-library/foo';
+ })),
+ ...createScenario((waitMethod, queryMethod) => ({
+ code: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const submitButton = await ${waitMethod}(() => expect(screen.${queryMethod}('foo', { name: 'baz' })).toBeDefined())
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: getFindByQueryVariant(queryMethod),
- queryMethod: queryMethod.split('By')[1],
- prevQuery: queryMethod,
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod}} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: getFindByQueryVariant(queryMethod),
+ queryMethod: queryMethod.split('By')[1],
+ prevQuery: queryMethod,
+ waitForMethodName: waitMethod,
+ },
+ },
+ ],
+ output: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const submitButton = await screen.${buildFindByMethod(
- queryMethod
- )}('foo', { name: 'baz' })
+ queryMethod
+ )}('foo', { name: 'baz' })
})
`,
- })),
- ...createScenario((waitMethod: string, queryMethod: string) => ({
- code: `
- import {${waitMethod}} from '@testing-library/foo';
+ })),
+ ...createScenario((waitMethod, queryMethod) => ({
+ code: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const submitButton = await ${waitMethod}(() => expect(screen.${queryMethod}('foo', { name: 'baz' })).not.toBeNull())
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: getFindByQueryVariant(queryMethod),
- queryMethod: queryMethod.split('By')[1],
- prevQuery: queryMethod,
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod}} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: getFindByQueryVariant(queryMethod),
+ queryMethod: queryMethod.split('By')[1],
+ prevQuery: queryMethod,
+ waitForMethodName: waitMethod,
+ },
+ },
+ ],
+ output: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const submitButton = await screen.${buildFindByMethod(
- queryMethod
- )}('foo', { name: 'baz' })
+ queryMethod
+ )}('foo', { name: 'baz' })
})
`,
- })),
- ...createScenario((waitMethod: string, queryMethod: string) => ({
- code: `
- import {${waitMethod}} from '@testing-library/foo';
+ })),
+ ...createScenario((waitMethod, queryMethod) => ({
+ code: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const submitButton = await ${waitMethod}(() => expect(screen.${queryMethod}('foo', { name: 'baz' })).toBeTruthy())
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: getFindByQueryVariant(queryMethod),
- queryMethod: queryMethod.split('By')[1],
- prevQuery: queryMethod,
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod}} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: getFindByQueryVariant(queryMethod),
+ queryMethod: queryMethod.split('By')[1],
+ prevQuery: queryMethod,
+ waitForMethodName: waitMethod,
+ },
+ },
+ ],
+ output: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const submitButton = await screen.${buildFindByMethod(
- queryMethod
- )}('foo', { name: 'baz' })
+ queryMethod
+ )}('foo', { name: 'baz' })
})
`,
- })),
- ...createScenario((waitMethod: string, queryMethod: string) => ({
- code: `
- import {${waitMethod}} from '@testing-library/foo';
+ })),
+ ...createScenario((waitMethod, queryMethod) => ({
+ code: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const submitButton = await ${waitMethod}(() => expect(screen.${queryMethod}('foo', { name: 'baz' })).not.toBeFalsy())
})
`,
- errors: [
- {
- messageId: 'preferFindBy',
- data: {
- queryVariant: getFindByQueryVariant(queryMethod),
- queryMethod: queryMethod.split('By')[1],
- prevQuery: queryMethod,
- waitForMethodName: waitMethod,
- },
- },
- ],
- output: `
- import {${waitMethod}} from '@testing-library/foo';
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: getFindByQueryVariant(queryMethod),
+ queryMethod: queryMethod.split('By')[1],
+ prevQuery: queryMethod,
+ waitForMethodName: waitMethod,
+ },
+ },
+ ],
+ output: `
+ import {${waitMethod}} from '${testingFramework}';
it('tests', async () => {
const submitButton = await screen.${buildFindByMethod(
- queryMethod
- )}('foo', { name: 'baz' })
+ queryMethod
+ )}('foo', { name: 'baz' })
})
`,
- })),
- ],
+ })),
+ // Issue #579, https://github.com/testing-library/eslint-plugin-testing-library/issues/579
+ // findBy can have two sets of options: await screen.findByText('text', queryOptions, waitForOptions)
+ ...createScenario((waitMethod, queryMethod) => ({
+ code: `import {${waitMethod}} from '${testingFramework}';
+ const button = await ${waitMethod}(() => screen.${queryMethod}('Count is: 0'), { timeout: 100, interval: 200 })
+ `,
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: getFindByQueryVariant(queryMethod),
+ queryMethod: queryMethod.split('By')[1],
+ prevQuery: queryMethod,
+ waitForMethodName: waitMethod,
+ },
+ },
+ ],
+ output: `import {${waitMethod}} from '${testingFramework}';
+ const button = await screen.${buildFindByMethod(
+ queryMethod
+ )}('Count is: 0', { timeout: 100, interval: 200 })
+ `,
+ })),
+ ...ASYNC_QUERIES_COMBINATIONS.map>(
+ (queryMethod) => ({
+ code: `
+ import {waitFor} from '${testingFramework}';
+ it('tests', async () => {
+ await waitFor(async () => {
+ const button = await screen.${queryMethod}("button", { name: "Submit" })
+ expect(button).toBeInTheDocument()
+ })
+ })
+ `,
+ errors: [
+ {
+ messageId: 'preferFindBy',
+ data: {
+ queryVariant: getFindByQueryVariant(queryMethod),
+ queryMethod: queryMethod.split('By')[1],
+ prevQuery: queryMethod,
+ waitForMethodName: 'waitFor',
+ },
+ },
+ ],
+ output: `
+ import {waitFor} from '${testingFramework}';
+ it('tests', async () => {
+ const button = await screen.${queryMethod}("button", { name: "Submit" })
+ expect(button).toBeInTheDocument()
+ })
+ `,
+ })
+ ),
+ ]),
});
diff --git a/tests/lib/rules/prefer-implicit-assert.test.ts b/tests/lib/rules/prefer-implicit-assert.test.ts
new file mode 100644
index 00000000..c73ac0c7
--- /dev/null
+++ b/tests/lib/rules/prefer-implicit-assert.test.ts
@@ -0,0 +1,552 @@
+import rule, { RULE_NAME } from '../../../lib/rules/prefer-implicit-assert';
+import { ALL_QUERIES_METHODS } from '../../../lib/utils';
+import { createRuleTester } from '../test-utils';
+
+const ruleTester = createRuleTester();
+
+const COMBINED_QUERIES_METHODS = [...ALL_QUERIES_METHODS, 'ByIcon'];
+
+ruleTester.run(RULE_NAME, rule, {
+ valid: [
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `await find${queryMethod}('qux');`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `screen.find${queryMethod}('foo')`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `await screen.find${queryMethod}('foo')`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `const utils = render();
+ await utils.find${queryMethod}('foo')`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `get${queryMethod}('qux');`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `screen.get${queryMethod}('foo')`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `const utils = render();
+ utils.get${queryMethod}('foo')`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `expect(query${queryMethod}('qux')).toBeInTheDocument();`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `expect(query${queryMethod}('qux')).not.toBeInTheDocument();`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `const something = await find${queryMethod}('qux');`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `const something = get${queryMethod}('qux');`,
+ })),
+ ...COMBINED_QUERIES_METHODS.map((queryMethod) => ({
+ code: `const something = query${queryMethod}('qux');`,
+ })),
+ ],
+ invalid: [
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(await find${queryMethod}('qux')).toBeInTheDocument();`,
+ errors: [
+ {
+ line: 1,
+ column: 14,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'findBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(await find${queryMethod}('qux')).toBeTruthy();`,
+ errors: [
+ {
+ line: 1,
+ column: 14,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'findBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(await find${queryMethod}('qux')).toBeDefined();`,
+ errors: [
+ {
+ line: 1,
+ column: 14,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'findBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(await find${queryMethod}('qux')).not.toBeNull();`,
+ errors: [
+ {
+ line: 1,
+ column: 14,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'findBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(await find${queryMethod}('qux')).not.toBeFalsy();`,
+ errors: [
+ {
+ line: 1,
+ column: 14,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'findBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(await screen.find${queryMethod}('qux')).toBeInTheDocument();`,
+ errors: [
+ {
+ line: 1,
+ column: 14,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'findBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(await screen.find${queryMethod}('qux')).toBeTruthy();`,
+ errors: [
+ {
+ line: 1,
+ column: 14,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'findBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(await screen.find${queryMethod}('qux')).toBeDefined();`,
+ errors: [
+ {
+ line: 1,
+ column: 14,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'findBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(await screen.find${queryMethod}('qux')).not.toBeNull();`,
+ errors: [
+ {
+ line: 1,
+ column: 14,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'findBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(await screen.find${queryMethod}('qux')).not.toBeFalsy();`,
+ errors: [
+ {
+ line: 1,
+ column: 14,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'findBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `
+ const utils = render();
+ expect(await utils.find${queryMethod}('foo')).toBeInTheDocument();`,
+ errors: [
+ {
+ line: 3,
+ column: 20,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'findBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `
+ const utils = render();
+ expect(await utils.find${queryMethod}('foo')).toBeTruthy();`,
+ errors: [
+ {
+ line: 3,
+ column: 20,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'findBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `
+ const utils = render();
+ expect(await utils.find${queryMethod}('foo')).toBeDefined();`,
+ errors: [
+ {
+ line: 3,
+ column: 20,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'findBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `
+ const utils = render();
+ expect(await utils.find${queryMethod}('foo')).not.toBeFalsy();`,
+ errors: [
+ {
+ line: 3,
+ column: 20,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'findBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `
+ const utils = render();
+ expect(await utils.find${queryMethod}('foo')).not.toBeNull();`,
+ errors: [
+ {
+ line: 3,
+ column: 20,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'findBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(get${queryMethod}('qux')).toBeInTheDocument();`,
+ errors: [
+ {
+ line: 1,
+ column: 8,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(get${queryMethod}('qux')).toBeTruthy();`,
+ errors: [
+ {
+ line: 1,
+ column: 8,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(get${queryMethod}('qux')).toBeDefined();`,
+ errors: [
+ {
+ line: 1,
+ column: 8,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(get${queryMethod}('qux')).not.toBeNull();`,
+ errors: [
+ {
+ line: 1,
+ column: 8,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(get${queryMethod}('qux')).not.toBeFalsy();`,
+ errors: [
+ {
+ line: 1,
+ column: 8,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(screen.get${queryMethod}('qux')).toBeInTheDocument();`,
+ errors: [
+ {
+ line: 1,
+ column: 8,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(screen.get${queryMethod}('qux')).toBeTruthy();`,
+ errors: [
+ {
+ line: 1,
+ column: 8,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(screen.get${queryMethod}('qux')).toBeDefined();`,
+ errors: [
+ {
+ line: 1,
+ column: 8,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(screen.get${queryMethod}('qux')).not.toBeNull();`,
+ errors: [
+ {
+ line: 1,
+ column: 8,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `expect(screen.get${queryMethod}('qux')).not.toBeFalsy();`,
+ errors: [
+ {
+ line: 1,
+ column: 8,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `
+ const utils = render();
+ expect(utils.get${queryMethod}('foo')).toBeInTheDocument();`,
+ errors: [
+ {
+ line: 3,
+ column: 14,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `
+ const utils = render();
+ expect(utils.get${queryMethod}('foo')).toBeTruthy();`,
+ errors: [
+ {
+ line: 3,
+ column: 14,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `
+ const utils = render();
+ expect(utils.get${queryMethod}('foo')).toBeDefined();`,
+ errors: [
+ {
+ line: 3,
+ column: 14,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `
+ const utils = render();
+ expect(utils.get${queryMethod}('foo')).not.toBeFalsy();`,
+ errors: [
+ {
+ line: 3,
+ column: 14,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...COMBINED_QUERIES_METHODS.map(
+ (queryMethod) =>
+ ({
+ code: `
+ const utils = render();
+ expect(utils.get${queryMethod}('foo')).not.toBeNull();`,
+ errors: [
+ {
+ line: 3,
+ column: 14,
+ messageId: 'preferImplicitAssert',
+ data: {
+ queryType: 'getBy*',
+ },
+ },
+ ],
+ }) as const
+ ),
+ ],
+});
diff --git a/tests/lib/rules/prefer-presence-queries.test.ts b/tests/lib/rules/prefer-presence-queries.test.ts
index 8ff037d4..72cbe4d5 100644
--- a/tests/lib/rules/prefer-presence-queries.test.ts
+++ b/tests/lib/rules/prefer-presence-queries.test.ts
@@ -1,8 +1,12 @@
-import { TSESLint } from '@typescript-eslint/experimental-utils';
+import {
+ type InvalidTestCase,
+ type ValidTestCase,
+} from '@typescript-eslint/rule-tester';
import rule, {
- RULE_NAME,
- MessageIds,
+ RULE_NAME,
+ MessageIds,
+ Options,
} from '../../../lib/rules/prefer-presence-queries';
import { ALL_QUERIES_METHODS } from '../../../lib/utils';
import { createRuleTester } from '../test-utils';
@@ -13,681 +17,1558 @@ const getByQueries = ALL_QUERIES_METHODS.map((method) => `get${method}`);
const getAllByQueries = ALL_QUERIES_METHODS.map((method) => `getAll${method}`);
const queryByQueries = ALL_QUERIES_METHODS.map((method) => `query${method}`);
const queryAllByQueries = ALL_QUERIES_METHODS.map(
- (method) => `queryAll${method}`
+ (method) => `queryAll${method}`
);
-type RuleValidTestCase = TSESLint.ValidTestCase<[]>;
-type RuleInvalidTestCase = TSESLint.InvalidTestCase;
+type RuleValidTestCase = ValidTestCase;
+type RuleInvalidTestCase = InvalidTestCase;
type AssertionFnParams = {
- query: string;
- matcher: string;
- messageId: MessageIds;
- shouldUseScreen?: boolean;
+ query: string;
+ matcher: string;
+ messageId: MessageIds;
+ shouldUseScreen?: boolean;
+ assertionType: keyof Options[number];
};
-const getValidAssertion = ({
- query,
- matcher,
- shouldUseScreen = false,
+const getValidAssertions = ({
+ query,
+ matcher,
+ shouldUseScreen = false,
+ assertionType,
+}: Omit): RuleValidTestCase[] => {
+ const finalQuery = shouldUseScreen ? `screen.${query}` : query;
+ const code = `expect(${finalQuery}('Hello'))${matcher}`;
+ return [
+ {
+ code,
+ },
+ {
+ code,
+ options: [
+ {
+ [assertionType]: true,
+ [assertionType === 'absence' ? 'presence' : 'absence']: false,
+ },
+ ],
+ },
+ {
+ code,
+ options: [
+ {
+ presence: false,
+ absence: false,
+ },
+ ],
+ },
+ ];
+};
+
+const getDisabledValidAssertion = ({
+ query,
+ matcher,
+ shouldUseScreen = false,
+ assertionType,
}: Omit): RuleValidTestCase => {
- const finalQuery = shouldUseScreen ? `screen.${query}` : query;
- return {
- code: `expect(${finalQuery}('Hello'))${matcher}`,
- } as const;
+ const finalQuery = shouldUseScreen ? `screen.${query}` : query;
+ return {
+ code: `expect(${finalQuery}('Hello'))${matcher}`,
+ options: [
+ {
+ [assertionType]: false,
+ [assertionType === 'absence' ? 'presence' : 'absence']: true,
+ },
+ ],
+ };
};
-const getInvalidAssertion = ({
- query,
- matcher,
- messageId,
- shouldUseScreen = false,
-}: AssertionFnParams): RuleInvalidTestCase => {
- const finalQuery = shouldUseScreen ? `screen.${query}` : query;
- return {
- code: `expect(${finalQuery}('Hello'))${matcher}`,
- errors: [{ messageId, line: 1, column: shouldUseScreen ? 15 : 8 }],
- };
+const toggleQueryPrefix = (query: string): string => {
+ if (query.startsWith('get')) return query.replace(/^get/, 'query');
+ if (query.startsWith('query')) return query.replace(/^query/, 'get');
+ return query;
+};
+
+const applyScreenPrefix = (query: string, shouldUseScreen: boolean): string =>
+ shouldUseScreen ? `screen.${query}` : query;
+
+const getInvalidAssertions = ({
+ query,
+ matcher,
+ messageId,
+ shouldUseScreen = false,
+ assertionType,
+}: AssertionFnParams): RuleInvalidTestCase[] => {
+ const finalQuery = applyScreenPrefix(query, shouldUseScreen);
+ const code = `expect(${finalQuery}('Hello'))${matcher}`;
+
+ const outputQuery = toggleQueryPrefix(query);
+ const finalOutputQuery = applyScreenPrefix(outputQuery, shouldUseScreen);
+ const output = `expect(${finalOutputQuery}('Hello'))${matcher}`;
+
+ return [
+ {
+ code,
+ errors: [{ messageId, line: 1, column: shouldUseScreen ? 15 : 8 }],
+ output,
+ },
+ {
+ code,
+ options: [
+ {
+ [assertionType]: true,
+ [assertionType === 'absence' ? 'presence' : 'absence']: false,
+ },
+ ],
+ errors: [{ messageId, line: 1, column: shouldUseScreen ? 15 : 8 }],
+ output,
+ },
+ ];
};
ruleTester.run(RULE_NAME, rule, {
- valid: [
- // cases: methods not matching Testing Library queries pattern
- `expect(queryElement('foo')).toBeInTheDocument()`,
- `expect(getElement('foo')).not.toBeInTheDocument()`,
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ valid: [
+ // cases: methods not matching Testing Library queries pattern
+ `expect(queryElement('foo')).toBeInTheDocument()`,
+ `expect(getElement('foo')).not.toBeInTheDocument()`,
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
// case: invalid presence assert but not reported because custom module is not imported
expect(queryByRole('button')).toBeInTheDocument()
`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
// case: invalid absence assert but not reported because custom module is not imported
expect(getByRole('button')).not.toBeInTheDocument()
`,
- },
- // cases: asserting presence correctly with `getBy*` queries
- ...getByQueries.reduce(
- (validRules, queryName) => [
- ...validRules,
- getValidAssertion({
- query: queryName,
- matcher: '.toBeInTheDocument()',
- }),
- getValidAssertion({ query: queryName, matcher: '.toBeTruthy()' }),
- getValidAssertion({ query: queryName, matcher: '.toBeDefined()' }),
- getValidAssertion({ query: queryName, matcher: '.toBe("foo")' }),
- getValidAssertion({ query: queryName, matcher: '.toEqual("World")' }),
- getValidAssertion({ query: queryName, matcher: '.not.toBeFalsy()' }),
- getValidAssertion({ query: queryName, matcher: '.not.toBeNull()' }),
- getValidAssertion({ query: queryName, matcher: '.not.toBeDisabled()' }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toHaveClass("btn")',
- }),
- ],
- []
- ),
- // cases: asserting presence correctly with `screen.getBy*` queries
- ...getByQueries.reduce(
- (validRules, queryName) => [
- ...validRules,
- getValidAssertion({
- query: queryName,
- matcher: '.toBeInTheDocument()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.toBeTruthy()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.toBeDefined()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.toBe("foo")',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.toEqual("World")',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toBeFalsy()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toBeNull()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toBeDisabled()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toHaveClass("btn")',
- shouldUseScreen: true,
- }),
- ],
- []
- ),
- // cases: asserting presence correctly with `getAllBy*` queries
- ...getAllByQueries.reduce(
- (validRules, queryName) => [
- ...validRules,
- getValidAssertion({
- query: queryName,
- matcher: '.toBeInTheDocument()',
- }),
- getValidAssertion({ query: queryName, matcher: '.toBeTruthy()' }),
- getValidAssertion({ query: queryName, matcher: '.toBeDefined()' }),
- getValidAssertion({ query: queryName, matcher: '.toBe("foo")' }),
- getValidAssertion({ query: queryName, matcher: '.toEqual("World")' }),
- getValidAssertion({ query: queryName, matcher: '.not.toBeFalsy()' }),
- getValidAssertion({ query: queryName, matcher: '.not.toBeNull()' }),
- getValidAssertion({ query: queryName, matcher: '.not.toBeDisabled()' }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toHaveClass("btn")',
- }),
- ],
- []
- ),
- // cases: asserting presence correctly with `screen.getAllBy*` queries
- ...getAllByQueries.reduce(
- (validRules, queryName) => [
- ...validRules,
- getValidAssertion({
- query: queryName,
- matcher: '.toBeInTheDocument()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.toBeTruthy()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.toBeDefined()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.toBe("foo")',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.toEqual("World")',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toBeFalsy()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toBeNull()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toBeDisabled()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toHaveClass("btn")',
- shouldUseScreen: true,
- }),
- ],
- []
- ),
- // cases: asserting absence correctly with `queryBy*` queries
- ...queryByQueries.reduce(
- (validRules, queryName) => [
- ...validRules,
- getValidAssertion({ query: queryName, matcher: '.toBeNull()' }),
- getValidAssertion({ query: queryName, matcher: '.toBeFalsy()' }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toBeInTheDocument()',
- }),
- getValidAssertion({ query: queryName, matcher: '.not.toBeTruthy()' }),
- getValidAssertion({ query: queryName, matcher: '.not.toBeDefined()' }),
- getValidAssertion({ query: queryName, matcher: '.toEqual("World")' }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toHaveClass("btn")',
- }),
- ],
- []
- ),
- // cases: asserting absence correctly with `screen.queryBy*` queries
- ...queryByQueries.reduce(
- (validRules, queryName) => [
- ...validRules,
- getValidAssertion({
- query: queryName,
- matcher: '.toBeNull()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.toBeFalsy()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toBeInTheDocument()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toBeTruthy()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toBeDefined()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.toEqual("World")',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toHaveClass("btn")',
- shouldUseScreen: true,
- }),
- ],
- []
- ),
- // cases: asserting absence correctly with `queryAllBy*` queries
- ...queryAllByQueries.reduce(
- (validRules, queryName) => [
- ...validRules,
- getValidAssertion({ query: queryName, matcher: '.toBeNull()' }),
- getValidAssertion({ query: queryName, matcher: '.toBeFalsy()' }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toBeInTheDocument()',
- }),
- getValidAssertion({ query: queryName, matcher: '.not.toBeTruthy()' }),
- getValidAssertion({ query: queryName, matcher: '.not.toBeDefined()' }),
- getValidAssertion({ query: queryName, matcher: '.toEqual("World")' }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toHaveClass("btn")',
- }),
- ],
- []
- ),
- // cases: asserting absence correctly with `screen.queryAllBy*` queries
- ...queryAllByQueries.reduce(
- (validRules, queryName) => [
- ...validRules,
- getValidAssertion({
- query: queryName,
- matcher: '.toBeNull()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.toBeFalsy()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toBeInTheDocument()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toBeTruthy()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toBeDefined()',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.toEqual("World")',
- shouldUseScreen: true,
- }),
- getValidAssertion({
- query: queryName,
- matcher: '.not.toHaveClass("btn")',
- shouldUseScreen: true,
- }),
- ],
- []
- ),
- {
- code: 'const el = getByText("button")',
- },
- {
- code: 'const el = queryByText("button")',
- },
- {
- code: `async () => {
+ },
+ // cases: asserting presence correctly with `getBy*` queries
+ ...getByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeInTheDocument()',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeOnTheScreen()',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeTruthy()',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeDefined()',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBe("foo")',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toEqual("World")',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeFalsy()',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeNull()',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeDisabled()',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toHaveClass("btn")',
+ assertionType: 'presence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting presence correctly with `screen.getBy*` queries
+ ...getByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeInTheDocument()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeOnTheScreen()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeTruthy()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeDefined()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBe("foo")',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toEqual("World")',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeFalsy()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeNull()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeDisabled()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toHaveClass("btn")',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting presence correctly with `getAllBy*` queries
+ ...getAllByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeInTheDocument()',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeOnTheScreen()',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeTruthy()',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeDefined()',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBe("foo")',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toEqual("World")',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeFalsy()',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeNull()',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeDisabled()',
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toHaveClass("btn")',
+ assertionType: 'presence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting presence correctly with `screen.getAllBy*` queries
+ ...getAllByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeInTheDocument()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeOnTheScreen()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeTruthy()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeDefined()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBe("foo")',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toEqual("World")',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeFalsy()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeNull()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeDisabled()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toHaveClass("btn")',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting absence correctly with `queryBy*` queries
+ ...queryByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeNull()',
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeFalsy()',
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeInTheDocument()',
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeOnTheScreen()',
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeTruthy()',
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeDefined()',
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toEqual("World")',
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toHaveClass("btn")',
+ assertionType: 'absence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting absence correctly with `screen.queryBy*` queries
+ ...queryByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeNull()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeFalsy()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeInTheDocument()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeOnTheScreen()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeTruthy()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeDefined()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toEqual("World")',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toHaveClass("btn")',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting absence correctly with `queryAllBy*` queries
+ ...queryAllByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeNull()',
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeFalsy()',
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeInTheDocument()',
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeOnTheScreen()',
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeTruthy()',
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeDefined()',
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toEqual("World")',
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toHaveClass("btn")',
+ assertionType: 'absence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting absence correctly with `screen.queryAllBy*` queries
+ ...queryAllByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeNull()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeFalsy()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeInTheDocument()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeOnTheScreen()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeTruthy()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeDefined()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toEqual("World")',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toHaveClass("btn")',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ],
+ []
+ ),
+
+ // cases: asserting absence incorrectly with `getBy*` queries with absence rule disabled
+ ...getByQueries.reduce(
+ (invalidRules, queryName) => [
+ ...invalidRules,
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeNull()',
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeFalsy()',
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeInTheDocument',
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeOnTheScreen',
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeTruthy()',
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeDefined()',
+ assertionType: 'absence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting absence incorrectly with `screen.getBy*` queries with absence rule disabled
+ ...getByQueries.reduce(
+ (invalidRules, queryName) => [
+ ...invalidRules,
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeNull()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeFalsy()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeInTheDocument',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeOnTheScreen',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeTruthy()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeDefined()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting absence incorrectly with `getAllBy*` queries with absence rule disabled
+ ...getAllByQueries.reduce(
+ (invalidRules, queryName) => [
+ ...invalidRules,
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeNull()',
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeFalsy()',
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeInTheDocument',
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeOnTheScreen',
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeTruthy()',
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeDefined()',
+ assertionType: 'absence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting absence incorrectly with `screen.getAllBy*` queries with absence rule disabled
+ ...getAllByQueries.reduce(
+ (invalidRules, queryName) => [
+ ...invalidRules,
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeNull()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeFalsy()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeInTheDocument',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeOnTheScreen',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeTruthy()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeDefined()',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting presence incorrectly with `queryBy*` queries with presence rule disabled
+ ...queryByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeTruthy()',
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeDefined()',
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeInTheDocument()',
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeOnTheScreen()',
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeFalsy()',
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeNull()',
+ assertionType: 'presence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting presence incorrectly with `screen.queryBy*` queries with presence rule disabled
+ ...queryByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeTruthy()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeDefined()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeInTheDocument()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeOnTheScreen()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeFalsy()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeNull()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting presence incorrectly with `queryAllBy*` queries with presence rule disabled
+ ...queryAllByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeTruthy()',
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeDefined()',
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeInTheDocument()',
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeOnTheScreen()',
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeFalsy()',
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeNull()',
+ assertionType: 'presence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting presence incorrectly with `screen.queryAllBy*` queries with presence rule disabled
+ ...queryAllByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeTruthy()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeDefined()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeInTheDocument()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.toBeOnTheScreen()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeFalsy()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ getDisabledValidAssertion({
+ query: queryName,
+ matcher: '.not.toBeNull()',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ],
+ []
+ ),
+
+ {
+ code: 'const el = getByText("button")',
+ },
+ {
+ code: 'const el = queryByText("button")',
+ },
+ {
+ code: `async () => {
const el = await findByText('button')
expect(el).toBeInTheDocument()
}`,
- },
- `// case: query an element with getBy but then check its absence after doing
+ },
+ `// case: query an element with getBy but then check its absence after doing
// some action which makes it disappear.
// submit button exists
const submitButton = screen.getByRole('button')
fireEvent.click(submitButton)
-
+
// right after clicking submit button it disappears
expect(submitButton).not.toBeInTheDocument()
`,
- ],
- invalid: [
- // cases: asserting absence incorrectly with `getBy*` queries
- ...getByQueries.reduce(
- (invalidRules, queryName) => [
- ...invalidRules,
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeNull()',
- messageId: 'wrongAbsenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeFalsy()',
- messageId: 'wrongAbsenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeInTheDocument()',
- messageId: 'wrongAbsenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeTruthy()',
- messageId: 'wrongAbsenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeDefined()',
- messageId: 'wrongAbsenceQuery',
- }),
- ],
- []
- ),
- // cases: asserting absence incorrectly with `screen.getBy*` queries
- ...getByQueries.reduce(
- (invalidRules, queryName) => [
- ...invalidRules,
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeNull()',
- messageId: 'wrongAbsenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeFalsy()',
- messageId: 'wrongAbsenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeInTheDocument()',
- messageId: 'wrongAbsenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeTruthy()',
- messageId: 'wrongAbsenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeDefined()',
- messageId: 'wrongAbsenceQuery',
- shouldUseScreen: true,
- }),
- ],
- []
- ),
- // cases: asserting absence incorrectly with `getAllBy*` queries
- ...getAllByQueries.reduce(
- (invalidRules, queryName) => [
- ...invalidRules,
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeNull()',
- messageId: 'wrongAbsenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeFalsy()',
- messageId: 'wrongAbsenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeInTheDocument()',
- messageId: 'wrongAbsenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeTruthy()',
- messageId: 'wrongAbsenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeDefined()',
- messageId: 'wrongAbsenceQuery',
- }),
- ],
- []
- ),
- // cases: asserting absence incorrectly with `screen.getAllBy*` queries
- ...getAllByQueries.reduce(
- (invalidRules, queryName) => [
- ...invalidRules,
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeNull()',
- messageId: 'wrongAbsenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeFalsy()',
- messageId: 'wrongAbsenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeInTheDocument()',
- messageId: 'wrongAbsenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeTruthy()',
- messageId: 'wrongAbsenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeDefined()',
- messageId: 'wrongAbsenceQuery',
- shouldUseScreen: true,
- }),
- ],
- []
- ),
- // cases: asserting presence incorrectly with `queryBy*` queries
- ...queryByQueries.reduce(
- (validRules, queryName) => [
- ...validRules,
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeTruthy()',
- messageId: 'wrongPresenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeDefined()',
- messageId: 'wrongPresenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeInTheDocument()',
- messageId: 'wrongPresenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeFalsy()',
- messageId: 'wrongPresenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeNull()',
- messageId: 'wrongPresenceQuery',
- }),
- ],
- []
- ),
- // cases: asserting presence incorrectly with `screen.queryBy*` queries
- ...queryByQueries.reduce(
- (validRules, queryName) => [
- ...validRules,
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeTruthy()',
- messageId: 'wrongPresenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeDefined()',
- messageId: 'wrongPresenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeInTheDocument()',
- messageId: 'wrongPresenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeFalsy()',
- messageId: 'wrongPresenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeNull()',
- messageId: 'wrongPresenceQuery',
- shouldUseScreen: true,
- }),
- ],
- []
- ),
- // cases: asserting presence incorrectly with `queryAllBy*` queries
- ...queryAllByQueries.reduce(
- (validRules, queryName) => [
- ...validRules,
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeTruthy()',
- messageId: 'wrongPresenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeDefined()',
- messageId: 'wrongPresenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeInTheDocument()',
- messageId: 'wrongPresenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeFalsy()',
- messageId: 'wrongPresenceQuery',
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeNull()',
- messageId: 'wrongPresenceQuery',
- }),
- ],
- []
- ),
- // cases: asserting presence incorrectly with `screen.queryAllBy*` queries
- ...queryAllByQueries.reduce(
- (validRules, queryName) => [
- ...validRules,
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeTruthy()',
- messageId: 'wrongPresenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeDefined()',
- messageId: 'wrongPresenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.toBeInTheDocument()',
- messageId: 'wrongPresenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeFalsy()',
- messageId: 'wrongPresenceQuery',
- shouldUseScreen: true,
- }),
- getInvalidAssertion({
- query: queryName,
- matcher: '.not.toBeNull()',
- messageId: 'wrongPresenceQuery',
- shouldUseScreen: true,
- }),
- ],
- []
- ),
- {
- code: 'expect(screen.getAllByText("button")[1]).not.toBeInTheDocument()',
- errors: [{ messageId: 'wrongAbsenceQuery', line: 1, column: 15 }],
- },
- {
- code: 'expect(screen.queryAllByText("button")[1]).toBeInTheDocument()',
- errors: [{ messageId: 'wrongPresenceQuery', line: 1, column: 15 }],
- },
- {
- code: `
+ `// checking absence on getBy* inside a within with queryBy* outside the within
+ expect(within(screen.getByRole("button")).queryByText("Hello")).not.toBeInTheDocument()
+ `,
+ `// checking presence on getBy* inside a within with getBy* outside the within
+ expect(within(screen.getByRole("button")).getByText("Hello")).toBeInTheDocument()
+ `,
+ ],
+ invalid: [
+ // cases: asserting absence incorrectly with `getBy*` queries
+ ...getByQueries.reduce(
+ (invalidRules, queryName) => [
+ ...invalidRules,
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeNull()',
+ messageId: 'wrongAbsenceQuery',
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeFalsy()',
+ messageId: 'wrongAbsenceQuery',
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeInTheDocument()',
+ messageId: 'wrongAbsenceQuery',
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeOnTheScreen()',
+ messageId: 'wrongAbsenceQuery',
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeTruthy()',
+ messageId: 'wrongAbsenceQuery',
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeDefined()',
+ messageId: 'wrongAbsenceQuery',
+ assertionType: 'absence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting absence incorrectly with `screen.getBy*` queries
+ ...getByQueries.reduce(
+ (invalidRules, queryName) => [
+ ...invalidRules,
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeNull()',
+ messageId: 'wrongAbsenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeFalsy()',
+ messageId: 'wrongAbsenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeInTheDocument()',
+ messageId: 'wrongAbsenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeOnTheScreen()',
+ messageId: 'wrongAbsenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeTruthy()',
+ messageId: 'wrongAbsenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeDefined()',
+ messageId: 'wrongAbsenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting absence incorrectly with `getAllBy*` queries
+ ...getAllByQueries.reduce(
+ (invalidRules, queryName) => [
+ ...invalidRules,
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeNull()',
+ messageId: 'wrongAbsenceQuery',
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeFalsy()',
+ messageId: 'wrongAbsenceQuery',
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeInTheDocument()',
+ messageId: 'wrongAbsenceQuery',
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeOnTheScreen()',
+ messageId: 'wrongAbsenceQuery',
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeTruthy()',
+ messageId: 'wrongAbsenceQuery',
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeDefined()',
+ messageId: 'wrongAbsenceQuery',
+ assertionType: 'absence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting absence incorrectly with `screen.getAllBy*` queries
+ ...getAllByQueries.reduce(
+ (invalidRules, queryName) => [
+ ...invalidRules,
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeNull()',
+ messageId: 'wrongAbsenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeFalsy()',
+ messageId: 'wrongAbsenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeInTheDocument()',
+ messageId: 'wrongAbsenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeOnTheScreen()',
+ messageId: 'wrongAbsenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeTruthy()',
+ messageId: 'wrongAbsenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeDefined()',
+ messageId: 'wrongAbsenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'absence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting presence incorrectly with `queryBy*` queries
+ ...queryByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeTruthy()',
+ messageId: 'wrongPresenceQuery',
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeDefined()',
+ messageId: 'wrongPresenceQuery',
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeInTheDocument()',
+ messageId: 'wrongPresenceQuery',
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeOnTheScreen()',
+ messageId: 'wrongPresenceQuery',
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeFalsy()',
+ messageId: 'wrongPresenceQuery',
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeNull()',
+ messageId: 'wrongPresenceQuery',
+ assertionType: 'presence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting presence incorrectly with `screen.queryBy*` queries
+ ...queryByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeTruthy()',
+ messageId: 'wrongPresenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeDefined()',
+ messageId: 'wrongPresenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeInTheDocument()',
+ messageId: 'wrongPresenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeOnTheScreen()',
+ messageId: 'wrongPresenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeFalsy()',
+ messageId: 'wrongPresenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeNull()',
+ messageId: 'wrongPresenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting presence incorrectly with `queryAllBy*` queries
+ ...queryAllByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeTruthy()',
+ messageId: 'wrongPresenceQuery',
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeDefined()',
+ messageId: 'wrongPresenceQuery',
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeInTheDocument()',
+ messageId: 'wrongPresenceQuery',
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeOnTheScreen()',
+ messageId: 'wrongPresenceQuery',
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeFalsy()',
+ messageId: 'wrongPresenceQuery',
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeNull()',
+ messageId: 'wrongPresenceQuery',
+ assertionType: 'presence',
+ }),
+ ],
+ []
+ ),
+ // cases: asserting presence incorrectly with `screen.queryAllBy*` queries
+ ...queryAllByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeTruthy()',
+ messageId: 'wrongPresenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeDefined()',
+ messageId: 'wrongPresenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeInTheDocument()',
+ messageId: 'wrongPresenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeOnTheScreen()',
+ messageId: 'wrongPresenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeFalsy()',
+ messageId: 'wrongPresenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.not.toBeNull()',
+ messageId: 'wrongPresenceQuery',
+ shouldUseScreen: true,
+ assertionType: 'presence',
+ }),
+ ],
+ []
+ ),
+ {
+ code: 'expect(screen.getAllByText("button")[1]).not.toBeInTheDocument()',
+ errors: [{ messageId: 'wrongAbsenceQuery', line: 1, column: 15 }],
+ output:
+ 'expect(screen.queryAllByText("button")[1]).not.toBeInTheDocument()',
+ },
+ {
+ code: 'expect(screen.getAllByText("button")[1]).not.toBeOnTheScreen()',
+ errors: [{ messageId: 'wrongAbsenceQuery', line: 1, column: 15 }],
+ output:
+ 'expect(screen.queryAllByText("button")[1]).not.toBeOnTheScreen()',
+ },
+ {
+ code: 'expect(screen.queryAllByText("button")[1]).toBeInTheDocument()',
+ errors: [{ messageId: 'wrongPresenceQuery', line: 1, column: 15 }],
+ output: 'expect(screen.getAllByText("button")[1]).toBeInTheDocument()',
+ },
+ {
+ code: 'expect(screen.queryAllByText("button")[1]).toBeOnTheScreen()',
+ errors: [{ messageId: 'wrongPresenceQuery', line: 1, column: 15 }],
+ output: 'expect(screen.getAllByText("button")[1]).toBeOnTheScreen()',
+ },
+ {
+ code: `
// case: asserting presence incorrectly with custom queryBy* query
expect(queryByCustomQuery("button")).toBeInTheDocument()
`,
- errors: [{ messageId: 'wrongPresenceQuery', line: 3, column: 16 }],
- },
- {
- code: `
+ errors: [{ messageId: 'wrongPresenceQuery', line: 3, column: 16 }],
+ output: `
+ // case: asserting presence incorrectly with custom queryBy* query
+ expect(getByCustomQuery("button")).toBeInTheDocument()
+ `,
+ },
+ {
+ code: `
+ // case: asserting presence incorrectly with custom queryBy* query
+ expect(queryByCustomQuery("button")).toBeOnTheScreen()
+ `,
+ errors: [{ messageId: 'wrongPresenceQuery', line: 3, column: 16 }],
+ output: `
+ // case: asserting presence incorrectly with custom queryBy* query
+ expect(getByCustomQuery("button")).toBeOnTheScreen()
+ `,
+ },
+ {
+ code: `
// case: asserting absence incorrectly with custom getBy* query
expect(getByCustomQuery("button")).not.toBeInTheDocument()
`,
- errors: [{ messageId: 'wrongAbsenceQuery', line: 3, column: 16 }],
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [{ messageId: 'wrongAbsenceQuery', line: 3, column: 16 }],
+ output: `
+ // case: asserting absence incorrectly with custom getBy* query
+ expect(queryByCustomQuery("button")).not.toBeInTheDocument()
+ `,
+ },
+ {
+ code: `
+ // case: asserting absence incorrectly with custom getBy* query
+ expect(getByCustomQuery("button")).not.toBeOnTheScreen()
+ `,
+ errors: [{ messageId: 'wrongAbsenceQuery', line: 3, column: 16 }],
+ output: `
+ // case: asserting absence incorrectly with custom getBy* query
+ expect(queryByCustomQuery("button")).not.toBeOnTheScreen()
+ `,
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
// case: asserting presence incorrectly importing custom module
import 'test-utils'
expect(queryByRole("button")).toBeInTheDocument()
`,
- errors: [{ line: 4, column: 14, messageId: 'wrongPresenceQuery' }],
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [{ line: 4, column: 14, messageId: 'wrongPresenceQuery' }],
+ output: `
+ // case: asserting presence incorrectly importing custom module
+ import 'test-utils'
+ expect(getByRole("button")).toBeInTheDocument()
+ `,
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
+ // case: asserting presence incorrectly importing custom module
+ import 'test-utils'
+ expect(queryByRole("button")).toBeOnTheScreen()
+ `,
+ errors: [{ line: 4, column: 14, messageId: 'wrongPresenceQuery' }],
+ output: `
+ // case: asserting presence incorrectly importing custom module
+ import 'test-utils'
+ expect(getByRole("button")).toBeOnTheScreen()
+ `,
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
// case: asserting absence incorrectly importing custom module
import 'test-utils'
expect(getByRole("button")).not.toBeInTheDocument()
`,
- errors: [{ line: 4, column: 14, messageId: 'wrongAbsenceQuery' }],
- },
- ],
+ errors: [{ line: 4, column: 14, messageId: 'wrongAbsenceQuery' }],
+ output: `
+ // case: asserting absence incorrectly importing custom module
+ import 'test-utils'
+ expect(queryByRole("button")).not.toBeInTheDocument()
+ `,
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
+ // case: asserting absence incorrectly importing custom module
+ import 'test-utils'
+ expect(getByRole("button")).not.toBeOnTheScreen()
+ `,
+ errors: [{ line: 4, column: 14, messageId: 'wrongAbsenceQuery' }],
+ output: `
+ // case: asserting absence incorrectly importing custom module
+ import 'test-utils'
+ expect(queryByRole("button")).not.toBeOnTheScreen()
+ `,
+ },
+ {
+ code: `
+ // case: asserting within check does still work with improper outer clause
+ expect(within(screen.getByRole("button")).getByText("Hello")).not.toBeInTheDocument()`,
+ errors: [{ line: 3, column: 46, messageId: 'wrongAbsenceQuery' }],
+ output: `
+ // case: asserting within check does still work with improper outer clause
+ expect(within(screen.getByRole("button")).queryByText("Hello")).not.toBeInTheDocument()`,
+ },
+ {
+ code: `
+ // case: asserting within check does still work with improper outer clause
+ expect(within(screen.getByRole("button")).queryByText("Hello")).toBeInTheDocument()`,
+ errors: [{ line: 3, column: 46, messageId: 'wrongPresenceQuery' }],
+ output: `
+ // case: asserting within check does still work with improper outer clause
+ expect(within(screen.getByRole("button")).getByText("Hello")).toBeInTheDocument()`,
+ },
+ {
+ code: `
+ // case: asserting within check does still work with improper outer clause and improper inner clause
+ expect(within(screen.queryByRole("button")).getByText("Hello")).not.toBeInTheDocument()`,
+ errors: [
+ { line: 3, column: 25, messageId: 'wrongPresenceQuery' },
+ { line: 3, column: 48, messageId: 'wrongAbsenceQuery' },
+ ],
+ output: `
+ // case: asserting within check does still work with improper outer clause and improper inner clause
+ expect(within(screen.getByRole("button")).queryByText("Hello")).not.toBeInTheDocument()`,
+ },
+ {
+ code: `
+ // case: asserting within check does still work with proper outer clause and improper inner clause
+ expect(within(screen.queryByRole("button")).queryByText("Hello")).not.toBeInTheDocument()`,
+ errors: [{ line: 3, column: 25, messageId: 'wrongPresenceQuery' }],
+ output: `
+ // case: asserting within check does still work with proper outer clause and improper inner clause
+ expect(within(screen.getByRole("button")).queryByText("Hello")).not.toBeInTheDocument()`,
+ },
+ {
+ code: `
+ // case: asserting within check does still work with proper outer clause and improper inner clause
+ expect(within(screen.queryByRole("button")).getByText("Hello")).toBeInTheDocument()`,
+ errors: [{ line: 3, column: 25, messageId: 'wrongPresenceQuery' }],
+ output: `
+ // case: asserting within check does still work with proper outer clause and improper inner clause
+ expect(within(screen.getByRole("button")).getByText("Hello")).toBeInTheDocument()`,
+ },
+ {
+ code: `
+ // case: asserting within check does still work with improper outer clause and improper inner clause
+ expect(within(screen.queryByRole("button")).queryByText("Hello")).toBeInTheDocument()`,
+ errors: [
+ { line: 3, column: 25, messageId: 'wrongPresenceQuery' },
+ { line: 3, column: 48, messageId: 'wrongPresenceQuery' },
+ ],
+ output: `
+ // case: asserting within check does still work with improper outer clause and improper inner clause
+ expect(within(screen.getByRole("button")).getByText("Hello")).toBeInTheDocument()`,
+ },
+ {
+ code: `
+ // case: asserting within check does still work with improper outer clause
+ expect(within(screen.getByRole("button")).getByText("Hello")).not.toBeOnTheScreen()`,
+ errors: [{ line: 3, column: 46, messageId: 'wrongAbsenceQuery' }],
+ output: `
+ // case: asserting within check does still work with improper outer clause
+ expect(within(screen.getByRole("button")).queryByText("Hello")).not.toBeOnTheScreen()`,
+ },
+ {
+ code: `
+ // case: asserting within check does still work with improper outer clause
+ expect(within(screen.getByRole("button")).queryByText("Hello")).toBeOnTheScreen()`,
+ errors: [{ line: 3, column: 46, messageId: 'wrongPresenceQuery' }],
+ output: `
+ // case: asserting within check does still work with improper outer clause
+ expect(within(screen.getByRole("button")).getByText("Hello")).toBeOnTheScreen()`,
+ },
+ {
+ code: `
+ // case: asserting within check does still work with improper outer clause and improper inner clause
+ expect(within(screen.queryByRole("button")).getByText("Hello")).not.toBeOnTheScreen()`,
+ errors: [
+ { line: 3, column: 25, messageId: 'wrongPresenceQuery' },
+ { line: 3, column: 48, messageId: 'wrongAbsenceQuery' },
+ ],
+ output: `
+ // case: asserting within check does still work with improper outer clause and improper inner clause
+ expect(within(screen.getByRole("button")).queryByText("Hello")).not.toBeOnTheScreen()`,
+ },
+ {
+ code: `
+ // case: asserting within check does still work with proper outer clause and improper inner clause
+ expect(within(screen.queryByRole("button")).queryByText("Hello")).not.toBeOnTheScreen()`,
+ errors: [{ line: 3, column: 25, messageId: 'wrongPresenceQuery' }],
+ output: `
+ // case: asserting within check does still work with proper outer clause and improper inner clause
+ expect(within(screen.getByRole("button")).queryByText("Hello")).not.toBeOnTheScreen()`,
+ },
+ {
+ code: `
+ // case: asserting within check does still work with proper outer clause and improper inner clause
+ expect(within(screen.queryByRole("button")).getByText("Hello")).toBeOnTheScreen()`,
+ errors: [{ line: 3, column: 25, messageId: 'wrongPresenceQuery' }],
+ output: `
+ // case: asserting within check does still work with proper outer clause and improper inner clause
+ expect(within(screen.getByRole("button")).getByText("Hello")).toBeOnTheScreen()`,
+ },
+ {
+ code: `
+ // case: asserting within check does still work with improper outer clause and improper inner clause
+ expect(within(screen.queryByRole("button")).queryByText("Hello")).toBeOnTheScreen()`,
+ errors: [
+ { line: 3, column: 25, messageId: 'wrongPresenceQuery' },
+ { line: 3, column: 48, messageId: 'wrongPresenceQuery' },
+ ],
+ output: `
+ // case: asserting within check does still work with improper outer clause and improper inner clause
+ expect(within(screen.getByRole("button")).getByText("Hello")).toBeOnTheScreen()`,
+ },
+ ],
});
diff --git a/tests/lib/rules/prefer-query-by-disappearance.test.ts b/tests/lib/rules/prefer-query-by-disappearance.test.ts
index 3f9d5460..32eb2f3b 100644
--- a/tests/lib/rules/prefer-query-by-disappearance.test.ts
+++ b/tests/lib/rules/prefer-query-by-disappearance.test.ts
@@ -1,757 +1,765 @@
import rule, {
- RULE_NAME,
+ RULE_NAME,
} from '../../../lib/rules/prefer-query-by-disappearance';
import { createRuleTester } from '../test-utils';
const ruleTester = createRuleTester();
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ '@testing-library/dom',
+ '@testing-library/angular',
+ '@testing-library/react',
+ '@testing-library/vue',
+ '@marko/testing-library',
+];
+
ruleTester.run(RULE_NAME, rule, {
- valid: [
- {
- code: `
- import { screen } from '@testing-library/react';
+ valid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const button = screen.getByRole('button')
await waitForElementToBeRemoved(button)
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const callback = () => screen.getByRole('button')
await waitForElementToBeRemoved(callback)
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
await waitForElementToBeRemoved(() => screen.queryByText("hello"))
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
await waitForElementToBeRemoved(() => {
screen.queryByText("hello")
})
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
await waitForElementToBeRemoved(() => {
otherCode()
screen.queryByText("hello")
})
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
await waitForElementToBeRemoved(() => {
otherCode()
return screen.queryByText("hello")
})
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
await waitForElementToBeRemoved(() => {
return screen.queryByText("hello")
})
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
await waitForElementToBeRemoved(function() {
screen.queryByText("hello")
})
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
await waitForElementToBeRemoved(function() {
otherCode()
screen.queryByText("hello")
})
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
await waitForElementToBeRemoved(function() {
return screen.queryByText('hey')
})
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
await waitForElementToBeRemoved(function() {
otherCode()
return screen.queryByText('hey')
})
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
await waitForElementToBeRemoved(screen.queryByText("hello"))
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { queryByText } = screen
await waitForElementToBeRemoved(queryByText("hello"))
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { queryByText } = screen
await waitForElementToBeRemoved(() => queryByText("hello"))
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { queryByText } = screen
await waitForElementToBeRemoved(() => {
queryByText("hello")
})
`,
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { queryByText } = screen
await waitForElementToBeRemoved(() => {
return queryByText("hello")
})
`,
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { queryByText } = render()
await waitForElementToBeRemoved(() => {
return queryByText("hello")
})
`,
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { queryByText } = render()
await waitForElementToBeRemoved(() => {
queryByText("hello")
})
`,
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { queryByText } = render()
await waitForElementToBeRemoved(() => queryByText("hello"))
`,
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { queryByText } = render()
await waitForElementToBeRemoved(queryByText("hello"))
`,
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { queryByText } = render()
await waitForElementToBeRemoved(function() {
queryByText("hello")
})
`,
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { queryByText } = render()
await waitForElementToBeRemoved(function() {
return queryByText("hello")
})
`,
- },
- ],
- invalid: [
- {
- code: `
- import { screen, waitForElementToBeRemoved } from '@testing-library/react';
+ },
+ ]),
+ invalid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ code: `
+ import { screen, waitForElementToBeRemoved } from '${testingFramework}';
await waitForElementToBeRemoved(() => screen.getByText("hello"))
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 4,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen, waitForElementToBeRemoved } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 4,
+ column: 54,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen, waitForElementToBeRemoved } from '${testingFramework}';
await waitForElementToBeRemoved(() => screen.findByText("hello"))
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 4,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen, waitForElementToBeRemoved } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 4,
+ column: 54,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen, waitForElementToBeRemoved } from '${testingFramework}';
await waitForElementToBeRemoved(() => {
screen.getByText("hello")
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 4,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen, waitForElementToBeRemoved } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 5,
+ column: 18,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen, waitForElementToBeRemoved } from '${testingFramework}';
await waitForElementToBeRemoved(() => {
screen.findByText("hello")
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 4,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen, waitForElementToBeRemoved } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 5,
+ column: 18,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen, waitForElementToBeRemoved } from '${testingFramework}';
await waitForElementToBeRemoved(() => {
return screen.getByText("hello")
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 4,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen, waitForElementToBeRemoved } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 5,
+ column: 25,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen, waitForElementToBeRemoved } from '${testingFramework}';
await waitForElementToBeRemoved(() => {
return screen.findByText("hello")
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 4,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen, waitForElementToBeRemoved } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 5,
+ column: 25,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen, waitForElementToBeRemoved } from '${testingFramework}';
await waitForElementToBeRemoved(screen.getByText("hello"))
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 4,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen, waitForElementToBeRemoved } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 4,
+ column: 48,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen, waitForElementToBeRemoved } from '${testingFramework}';
await waitForElementToBeRemoved(screen.findByText("hello"))
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 4,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 4,
+ column: 48,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
await waitForElementToBeRemoved(function() {
return screen.getByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 4,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 5,
+ column: 25,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
await waitForElementToBeRemoved(function() {
return screen.findByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 4,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 5,
+ column: 25,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
await waitForElementToBeRemoved(function() {
screen.getByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 4,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 5,
+ column: 18,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
await waitForElementToBeRemoved(function() {
screen.findByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 4,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 5,
+ column: 18,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { getByText } = screen
await waitForElementToBeRemoved(function() {
getByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 11,
+ },
+ ],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { getByText } = render()
await waitForElementToBeRemoved(function() {
getByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 11,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { findByText } = screen
await waitForElementToBeRemoved(function() {
findByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 11,
+ },
+ ],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { findByText } = render
await waitForElementToBeRemoved(function() {
findByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 11,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { getByText } = screen
await waitForElementToBeRemoved(function() {
return getByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 18,
+ },
+ ],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { getByText } = render()
await waitForElementToBeRemoved(function() {
return getByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 18,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { findByText } = screen
await waitForElementToBeRemoved(function() {
return findByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 18,
+ },
+ ],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { findByText } = render()
await waitForElementToBeRemoved(function() {
return findByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 18,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { getByText } = screen
await waitForElementToBeRemoved(() => {
return getByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 18,
+ },
+ ],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { getByText } = render()
await waitForElementToBeRemoved(() => {
return getByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 18,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { findByText } = screen
await waitForElementToBeRemoved(() => {
return findByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 18,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { findByText } = render()
await waitForElementToBeRemoved(() => {
return findByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 18,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { getByText } = screen
await waitForElementToBeRemoved(() => {
getByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 11,
+ },
+ ],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { getByText } = render()
await waitForElementToBeRemoved(() => {
getByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 11,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { findByText } = screen
await waitForElementToBeRemoved(() => {
findByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 11,
+ },
+ ],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { findByText } = render()
await waitForElementToBeRemoved(() => {
findByText('hey')
})
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 6,
+ column: 11,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { findByText } = screen
await waitForElementToBeRemoved(() => findByText('hey'))
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 5,
+ column: 47,
+ },
+ ],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { findByText } = render()
await waitForElementToBeRemoved(() => findByText('hey'))
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 5,
+ column: 47,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { getByText } = screen
await waitForElementToBeRemoved(getByText('hey'))
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 5,
+ column: 41,
+ },
+ ],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { getByText } = render()
await waitForElementToBeRemoved(getByText('hey'))
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { screen } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 5,
+ column: 41,
+ },
+ ],
+ },
+ {
+ code: `
+ import { screen } from '${testingFramework}';
const { findByText } = screen
await waitForElementToBeRemoved(findByText('hey'))
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- {
- code: `
- import { render } from '@testing-library/react';
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 5,
+ column: 41,
+ },
+ ],
+ },
+ {
+ code: `
+ import { render } from '${testingFramework}';
const { findByText } = render()
await waitForElementToBeRemoved(findByText('hey'))
`,
- errors: [
- {
- messageId: 'preferQueryByDisappearance',
- line: 5,
- column: 41,
- },
- ],
- },
- ],
+ errors: [
+ {
+ messageId: 'preferQueryByDisappearance',
+ line: 5,
+ column: 41,
+ },
+ ],
+ },
+ ]),
});
diff --git a/tests/lib/rules/prefer-query-matchers.test.ts b/tests/lib/rules/prefer-query-matchers.test.ts
new file mode 100644
index 00000000..66f03644
--- /dev/null
+++ b/tests/lib/rules/prefer-query-matchers.test.ts
@@ -0,0 +1,417 @@
+import {
+ type InvalidTestCase,
+ type ValidTestCase,
+} from '@typescript-eslint/rule-tester';
+
+import rule, {
+ RULE_NAME,
+ MessageIds,
+ Options,
+} from '../../../lib/rules/prefer-query-matchers';
+import { ALL_QUERIES_METHODS } from '../../../lib/utils';
+import { createRuleTester } from '../test-utils';
+
+const ruleTester = createRuleTester();
+
+const getByQueries = ALL_QUERIES_METHODS.map((method) => `get${method}`);
+const getAllByQueries = ALL_QUERIES_METHODS.map((method) => `getAll${method}`);
+const queryByQueries = ALL_QUERIES_METHODS.map((method) => `query${method}`);
+const queryAllByQueries = ALL_QUERIES_METHODS.map(
+ (method) => `queryAll${method}`
+);
+
+type RuleValidTestCase = ValidTestCase;
+type RuleInvalidTestCase = InvalidTestCase;
+
+type AssertionFnParams = {
+ query: string;
+ matcher: string;
+ options: Options;
+ onlyOptions?: boolean;
+};
+
+const wrapExpectInTest = (expectStatement: string) => `
+import { render, screen } from '@testing-library/react'
+
+test('a fake test', () => {
+ render()
+
+ ${expectStatement}
+})`;
+
+const getValidAssertions = ({
+ query,
+ matcher,
+ options,
+ onlyOptions = false,
+}: AssertionFnParams): RuleValidTestCase[] => {
+ const expectStatement = `expect(${query}('Hello'))${matcher}`;
+ const expectScreenStatement = `expect(screen.${query}('Hello'))${matcher}`;
+ const casesWithOptions = [
+ {
+ // name: `${expectStatement} with provided options`,
+ code: wrapExpectInTest(expectStatement),
+ options,
+ },
+ {
+ // name: `${expectScreenStatement} with provided options`,
+ code: wrapExpectInTest(expectScreenStatement),
+ options,
+ },
+ ];
+
+ if (onlyOptions) {
+ return casesWithOptions;
+ }
+
+ return [
+ {
+ // name: `${expectStatement} with default options of empty validEntries`,
+ code: wrapExpectInTest(expectStatement),
+ },
+ {
+ // name: `${expectScreenStatement} with default options of empty validEntries`,
+ code: wrapExpectInTest(expectScreenStatement),
+ },
+ ...casesWithOptions,
+ ];
+};
+
+const getInvalidAssertions = ({
+ query,
+ matcher,
+ options,
+}: AssertionFnParams): RuleInvalidTestCase[] => {
+ const expectStatement = `expect(${query}('Hello'))${matcher}`;
+ const expectScreenStatement = `expect(screen.${query}('Hello'))${matcher}`;
+ const messageId: MessageIds = 'wrongQueryForMatcher';
+ const [
+ {
+ validEntries: [validEntry],
+ },
+ ] = options;
+ return [
+ {
+ // name: `${expectStatement} with provided options`,
+ code: wrapExpectInTest(expectStatement),
+ options,
+ errors: [
+ {
+ messageId,
+ line: 7,
+ column: 10,
+ data: { query: validEntry.query, matcher: validEntry.matcher },
+ },
+ ],
+ },
+ {
+ // name: `${expectScreenStatement} with provided options`,
+ code: wrapExpectInTest(expectScreenStatement),
+ options,
+ errors: [
+ {
+ messageId,
+ line: 7,
+ column: 17,
+ data: { query: validEntry.query, matcher: validEntry.matcher },
+ },
+ ],
+ },
+ ];
+};
+
+ruleTester.run(RULE_NAME, rule, {
+ valid: [
+ // cases: methods not matching Testing Library queries pattern
+ `expect(queryElement('foo')).toBeVisible()`,
+ `expect(getElement('foo')).not.toBeVisible()`,
+ // cases: asserting with a configured allowed `[screen.]getBy*` query
+ ...getByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeVisible()',
+ options: [
+ { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeHelloWorld()',
+ options: [
+ { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeVisible()',
+ options: [
+ { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeHelloWorld()',
+ options: [
+ { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeHelloWorld()',
+ options: [
+ { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
+ ],
+ onlyOptions: true,
+ }),
+ ],
+ []
+ ),
+ // cases: asserting with a configured allowed `[screen.]getAllBy*` query
+ ...getAllByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeVisible()',
+ options: [
+ { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeHelloWorld()',
+ options: [
+ { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeVisible()',
+ options: [
+ { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeHelloWorld()',
+ options: [
+ { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeHelloWorld()',
+ options: [
+ { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
+ ],
+ onlyOptions: true,
+ }),
+ ],
+ []
+ ),
+ // cases: asserting with a configured allowed `[screen.]queryBy*` query
+ ...queryByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeVisible()',
+ options: [
+ { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeHelloWorld()',
+ options: [
+ { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeVisible()',
+ options: [
+ { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeHelloWorld()',
+ options: [
+ { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeHelloWorld()',
+ options: [
+ { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
+ ],
+ onlyOptions: true,
+ }),
+ ],
+ []
+ ),
+ // cases: asserting with a configured allowed `[screen.]queryAllBy*` query
+ ...queryAllByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeVisible()',
+ options: [
+ { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeHelloWorld()',
+ options: [
+ { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeVisible()',
+ options: [
+ { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.not.toBeHelloWorld()',
+ options: [
+ { validEntries: [{ query: 'query', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ...getValidAssertions({
+ query: queryName,
+ matcher: '.toBeHelloWorld()',
+ options: [
+ { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
+ ],
+ onlyOptions: true,
+ }),
+ ],
+ []
+ ),
+ // case: getting outside an expectation
+ {
+ code: 'const el = getByText("button")',
+ },
+ // case: querying outside an expectation
+ {
+ code: 'const el = queryByText("button")',
+ },
+ // case: finding outside an expectation
+ {
+ code: `async () => {
+ const el = await findByText('button')
+ expect(el).toBeVisible()
+ }`,
+ },
+ {
+ code: `// case: query an element with getBy but then check its absence after doing
+ // some action which makes it disappear.
+
+ // submit button exists
+ const submitButton = screen.getByRole('button')
+ fireEvent.click(submitButton)
+
+ // right after clicking submit button it disappears
+ expect(submitButton).toBeHelloWorld()
+ `,
+ options: [
+ { validEntries: [{ query: 'query', matcher: 'toBeHelloWorld' }] },
+ ],
+ },
+ ],
+ invalid: [
+ // cases: asserting with a disallowed `[screen.]getBy*` query
+ ...getByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeHelloWorld()',
+ options: [
+ { validEntries: [{ query: 'query', matcher: 'toBeHelloWorld' }] },
+ ],
+ }),
+ ],
+ []
+ ),
+ // cases: asserting with a disallowed `[screen.]getAllBy*` query
+ ...getAllByQueries.reduce(
+ (validRules, queryName) => [
+ ...validRules,
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeHelloWorld()',
+ options: [
+ { validEntries: [{ query: 'query', matcher: 'toBeHelloWorld' }] },
+ ],
+ }),
+ ],
+ []
+ ),
+ // cases: asserting with a disallowed `[screen.]getBy*` query
+ ...queryByQueries.reduce(
+ (invalidRules, queryName) => [
+ ...invalidRules,
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeVisible()',
+ options: [
+ { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ],
+ []
+ ),
+ // cases: asserting with a disallowed `[screen.]queryAllBy*` query
+ ...queryAllByQueries.reduce(
+ (invalidRules, queryName) => [
+ ...invalidRules,
+ ...getInvalidAssertions({
+ query: queryName,
+ matcher: '.toBeVisible()',
+ options: [
+ { validEntries: [{ query: 'get', matcher: 'toBeVisible' }] },
+ ],
+ }),
+ ],
+ []
+ ),
+ // cases: indexing into an `AllBy` result within the expectation
+ {
+ code: 'expect(screen.queryAllByText("button")[1]).toBeVisible()',
+ options: [{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }],
+ errors: [
+ {
+ messageId: 'wrongQueryForMatcher',
+ line: 1,
+ column: 15,
+ data: { query: 'get', matcher: 'toBeVisible' },
+ },
+ ],
+ },
+ {
+ code: `
+ // case: asserting presence incorrectly with custom queryBy* query
+ expect(queryByCustomQuery("button")).toBeVisible()
+ `,
+ options: [{ validEntries: [{ query: 'get', matcher: 'toBeVisible' }] }],
+ errors: [
+ {
+ messageId: 'wrongQueryForMatcher',
+ line: 3,
+ column: 12,
+ data: { query: 'get', matcher: 'toBeVisible' },
+ },
+ ],
+ },
+ ],
+});
diff --git a/tests/lib/rules/prefer-screen-queries.test.ts b/tests/lib/rules/prefer-screen-queries.test.ts
index c94a1474..b49ae2ad 100644
--- a/tests/lib/rules/prefer-screen-queries.test.ts
+++ b/tests/lib/rules/prefer-screen-queries.test.ts
@@ -1,492 +1,486 @@
import rule, { RULE_NAME } from '../../../lib/rules/prefer-screen-queries';
import {
- ALL_QUERIES_COMBINATIONS,
- ALL_QUERIES_VARIANTS,
- combineQueries,
+ ALL_QUERIES_COMBINATIONS,
+ ALL_QUERIES_VARIANTS,
+ combineQueries,
} from '../../../lib/utils';
import { createRuleTester } from '../test-utils';
const ruleTester = createRuleTester();
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ '@testing-library/dom',
+ '@testing-library/angular',
+ '@testing-library/react',
+ '@testing-library/vue',
+ '@marko/testing-library',
+];
+
const CUSTOM_QUERY_COMBINATIONS = combineQueries(ALL_QUERIES_VARIANTS, [
- 'ByIcon',
+ 'ByIcon',
]);
ruleTester.run(RULE_NAME, rule, {
- valid: [
- {
- code: `const baz = () => 'foo'`,
- },
- ...ALL_QUERIES_COMBINATIONS.map((queryMethod) => ({
- code: `screen.${queryMethod}()`,
- })),
- {
- code: `otherFunctionShouldNotThrow()`,
- },
- {
- code: `component.otherFunctionShouldNotThrow()`,
- },
- ...ALL_QUERIES_COMBINATIONS.map((queryMethod) => ({
- code: `within(component).${queryMethod}()`,
- })),
- ...ALL_QUERIES_COMBINATIONS.map((queryMethod) => ({
- code: `within(screen.${queryMethod}()).${queryMethod}()`,
- })),
- ...ALL_QUERIES_COMBINATIONS.map((queryMethod) => ({
- code: `
+ valid: [
+ {
+ code: `const baz = () => 'foo'`,
+ },
+ ...ALL_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ code: `screen.${queryMethod}()`,
+ })),
+ {
+ code: `otherFunctionShouldNotThrow()`,
+ },
+ {
+ code: `component.otherFunctionShouldNotThrow()`,
+ },
+ ...ALL_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ code: `within(component).${queryMethod}()`,
+ })),
+ ...ALL_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ code: `within(screen.${queryMethod}()).${queryMethod}()`,
+ })),
+ ...ALL_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ code: `
const { ${queryMethod} } = within(screen.getByText('foo'))
${queryMethod}(baz)
`,
- })),
- ...ALL_QUERIES_COMBINATIONS.map((queryMethod) => ({
- code: `
+ })),
+ ...ALL_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ code: `
const myWithinVariable = within(foo)
myWithinVariable.${queryMethod}('baz')
`,
- })),
- ...CUSTOM_QUERY_COMBINATIONS.map(
- (query) => `
- import { render } from '@testing-library/react'
- import { ${query} } from 'custom-queries'
-
- test("imported custom queries, since they can't be used through screen", () => {
- render(foo)
- ${query}('bar')
- })
- `
- ),
- ...CUSTOM_QUERY_COMBINATIONS.map(
- (query) => `
- import { render } from '@testing-library/react'
-
- test("render-returned custom queries, since they can't be used through screen", () => {
- const { ${query} } = render(foo)
- ${query}('bar')
- })
- `
- ),
- ...CUSTOM_QUERY_COMBINATIONS.map((query) => ({
- settings: {
- 'testing-library/custom-queries': [query, 'ByComplexText'],
- },
- code: `
- import { render } from '@testing-library/react'
-
- test("custom queries + custom-queries setting, since they can't be used through screen", () => {
- const { ${query} } = render(foo)
- ${query}('bar')
- })
- `,
- })),
- {
- code: `
+ })),
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ ...CUSTOM_QUERY_COMBINATIONS.map((query) => ({
+ code: `
+ import { render } from '${testingFramework}'
+ import { ${query} } from 'custom-queries'
+
+ test("imported custom queries, since they can't be used through screen", () => {
+ render(foo)
+ ${query}('bar')
+ })
+ `,
+ })),
+ ...CUSTOM_QUERY_COMBINATIONS.map((query) => ({
+ code: `
+ import { render } from '${testingFramework}'
+
+ test("render-returned custom queries, since they can't be used through screen", () => {
+ const { ${query} } = render(foo)
+ ${query}('bar')
+ })
+ `,
+ })),
+ ...CUSTOM_QUERY_COMBINATIONS.map((query) => ({
+ settings: {
+ 'testing-library/custom-queries': [query, 'ByComplexText'],
+ },
+ code: `
+ import { render } from '${testingFramework}'
+
+ test("custom queries + custom-queries setting, since they can't be used through screen", () => {
+ const { ${query} } = render(foo)
+ ${query}('bar')
+ })
+ `,
+ })),
+ ]),
+ {
+ code: `
const screen = render(baz);
screen.container.querySelector('foo');
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const screen = render(baz);
screen.baseElement.querySelector('foo');
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const { rerender } = render(baz);
rerender();
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const utils = render(baz);
utils.rerender();
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const utils = render(baz);
utils.asFragment();
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const { asFragment } = render(baz);
asFragment();
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const { unmount } = render(baz);
unmount();
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const utils = render(baz);
utils.unmount();
`,
- },
- ...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
- code: `
+ },
+ ...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
+ code: `
const { ${queryMethod} } = render(baz, { baseElement: treeA })
expect(${queryMethod}(baz)).toBeDefined()
`,
- })),
- ...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
- code: `
+ })),
+ ...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
+ code: `
const { ${queryMethod}: aliasMethod } = render(baz, { baseElement: treeA })
expect(aliasMethod(baz)).toBeDefined()
`,
- })),
- ...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
- code: `
+ })),
+ ...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
+ code: `
const { ${queryMethod} } = render(baz, { container: treeA })
expect(${queryMethod}(baz)).toBeDefined()
`,
- })),
- ...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
- code: `
+ })),
+ ...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
+ code: `
const { ${queryMethod}: aliasMethod } = render(baz, { container: treeA })
expect(aliasMethod(baz)).toBeDefined()
`,
- })),
- ...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
- code: `
+ })),
+ ...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
+ code: `
const { ${queryMethod} } = render(baz, { baseElement: treeB, container: treeA })
expect(${queryMethod}(baz)).toBeDefined()
`,
- })),
- ...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
- code: `
+ })),
+ ...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
+ code: `
const { ${queryMethod}: aliasMethod } = render(baz, { baseElement: treeB, container: treeA })
expect(aliasMethod(baz)).toBeDefined()
`,
- })),
- ...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
- code: `
+ })),
+ ...ALL_QUERIES_COMBINATIONS.map((queryMethod: string) => ({
+ code: `
render(foo, { baseElement: treeA }).${queryMethod}()
`,
- })),
- ...ALL_QUERIES_COMBINATIONS.map((queryMethod) => ({
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ })),
+ ...ALL_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { render as testUtilRender } from 'test-utils'
import { render } from 'somewhere-else'
const { ${queryMethod} } = render(foo)
${queryMethod}()`,
- })),
- ...ALL_QUERIES_COMBINATIONS.map((queryMethod) => ({
- settings: {
- 'testing-library/custom-renders': ['customRender'],
- },
- code: `
+ })),
+ ...ALL_QUERIES_COMBINATIONS.map((queryMethod) => ({
+ settings: {
+ 'testing-library/custom-renders': ['customRender'],
+ },
+ code: `
import { anotherRender } from 'whatever'
const { ${queryMethod} } = anotherRender(foo)
${queryMethod}()`,
- })),
- ],
+ })),
+ ],
- invalid: [
- ...ALL_QUERIES_COMBINATIONS.map(
- (queryMethod) =>
- ({
- code: `
+ invalid: [
+ ...ALL_QUERIES_COMBINATIONS.map(
+ (queryMethod) =>
+ ({
+ code: `
const { ${queryMethod} } = render(foo)
${queryMethod}()`,
- errors: [
- {
- messageId: 'preferScreenQueries',
- data: {
- name: queryMethod,
- },
- },
- ],
- } as const)
- ),
- ...ALL_QUERIES_COMBINATIONS.map(
- (queryMethod) =>
- ({
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
+ errors: [
+ {
+ messageId: 'preferScreenQueries',
+ data: {
+ name: queryMethod,
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...ALL_QUERIES_COMBINATIONS.map(
+ (queryMethod) =>
+ ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
import { render } from 'test-utils'
const { ${queryMethod} } = render(foo)
${queryMethod}()`,
- errors: [
- {
- line: 4,
- column: 9,
- messageId: 'preferScreenQueries',
- data: {
- name: queryMethod,
- },
- },
- ],
- } as const)
- ),
+ errors: [
+ {
+ line: 4,
+ column: 9,
+ messageId: 'preferScreenQueries',
+ data: {
+ name: queryMethod,
+ },
+ },
+ ],
+ }) as const
+ ),
- ...ALL_QUERIES_COMBINATIONS.map(
- (queryMethod) =>
- ({
- settings: {
- 'testing-library/custom-renders': ['customRender'],
- },
- code: `
+ ...ALL_QUERIES_COMBINATIONS.map(
+ (queryMethod) =>
+ ({
+ settings: {
+ 'testing-library/custom-renders': ['customRender'],
+ },
+ code: `
import { customRender } from 'whatever'
const { ${queryMethod} } = customRender(foo)
${queryMethod}()`,
- errors: [
- {
- line: 4,
- column: 9,
- messageId: 'preferScreenQueries',
- data: {
- name: queryMethod,
- },
- },
- ],
- } as const)
- ),
- ...ALL_QUERIES_COMBINATIONS.map(
- (queryMethod) =>
- ({
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { render as testingLibraryRender} from '@testing-library/react'
+ errors: [
+ {
+ line: 4,
+ column: 9,
+ messageId: 'preferScreenQueries',
+ data: {
+ name: queryMethod,
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) =>
+ ALL_QUERIES_COMBINATIONS.map(
+ (queryMethod) =>
+ ({
+ settings: { 'testing-library/utils-module': 'test-utils' },
+ code: `
+ import { render as testingLibraryRender} from '${testingFramework}'
const { ${queryMethod} } = testingLibraryRender(foo)
${queryMethod}()`,
- errors: [
- {
- line: 4,
- column: 9,
- messageId: 'preferScreenQueries',
- data: {
- name: queryMethod,
- },
- },
- ],
- } as const)
- ),
- ...ALL_QUERIES_COMBINATIONS.map(
- (queryMethod) =>
- ({
- settings: { 'testing-library/utils-module': 'test-utils' },
- code: `
- import { render } from 'test-utils'
- const { ${queryMethod} } = render(foo)
- ${queryMethod}()`,
- errors: [
- {
- line: 4,
- column: 9,
- messageId: 'preferScreenQueries',
- data: {
- name: queryMethod,
- },
- },
- ],
- } as const)
- ),
- ...ALL_QUERIES_COMBINATIONS.map(
- (queryMethod) =>
- ({
- code: `render().${queryMethod}()`,
- errors: [
- {
- messageId: 'preferScreenQueries',
- data: {
- name: queryMethod,
- },
- },
- ],
- } as const)
- ),
- ...ALL_QUERIES_COMBINATIONS.map(
- (queryMethod) =>
- ({
- code: `render(foo, { hydrate: true }).${queryMethod}()`,
- errors: [
- {
- messageId: 'preferScreenQueries',
- data: {
- name: queryMethod,
- },
- },
- ],
- } as const)
- ),
- ...ALL_QUERIES_COMBINATIONS.map(
- (queryMethod) =>
- ({
- code: `component.${queryMethod}()`,
- errors: [
- {
- messageId: 'preferScreenQueries',
- data: {
- name: queryMethod,
- },
- },
- ],
- } as const)
- ),
- ...ALL_QUERIES_COMBINATIONS.map(
- (queryMethod) =>
- ({
- code: `
+ errors: [
+ {
+ line: 4,
+ column: 9,
+ messageId: 'preferScreenQueries',
+ data: {
+ name: queryMethod,
+ },
+ },
+ ],
+ }) as const
+ )
+ ),
+ ...ALL_QUERIES_COMBINATIONS.map(
+ (queryMethod) =>
+ ({
+ code: `render().${queryMethod}()`,
+ errors: [
+ {
+ messageId: 'preferScreenQueries',
+ data: {
+ name: queryMethod,
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...ALL_QUERIES_COMBINATIONS.map(
+ (queryMethod) =>
+ ({
+ code: `render(foo, { hydrate: true }).${queryMethod}()`,
+ errors: [
+ {
+ messageId: 'preferScreenQueries',
+ data: {
+ name: queryMethod,
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...ALL_QUERIES_COMBINATIONS.map(
+ (queryMethod) =>
+ ({
+ code: `component.${queryMethod}()`,
+ errors: [
+ {
+ messageId: 'preferScreenQueries',
+ data: {
+ name: queryMethod,
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...ALL_QUERIES_COMBINATIONS.map(
+ (queryMethod) =>
+ ({
+ code: `
const { ${queryMethod} } = render()
${queryMethod}(baz)
`,
- errors: [
- {
- messageId: 'preferScreenQueries',
- data: {
- name: queryMethod,
- },
- },
- ],
- } as const)
- ),
- ...ALL_QUERIES_COMBINATIONS.map(
- (queryMethod) =>
- ({
- code: `
+ errors: [
+ {
+ messageId: 'preferScreenQueries',
+ data: {
+ name: queryMethod,
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...ALL_QUERIES_COMBINATIONS.map(
+ (queryMethod) =>
+ ({
+ code: `
const myRenderVariable = render()
myRenderVariable.${queryMethod}(baz)
`,
- errors: [
- {
- messageId: 'preferScreenQueries',
- data: {
- name: queryMethod,
- },
- },
- ],
- } as const)
- ),
- ...ALL_QUERIES_COMBINATIONS.map(
- (queryMethod) =>
- ({
- code: `
+ errors: [
+ {
+ messageId: 'preferScreenQueries',
+ data: {
+ name: queryMethod,
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...ALL_QUERIES_COMBINATIONS.map(
+ (queryMethod) =>
+ ({
+ code: `
const [myVariable] = render()
myVariable.${queryMethod}(baz)
`,
- errors: [
- {
- messageId: 'preferScreenQueries',
- data: {
- name: queryMethod,
- },
- },
- ],
- } as const)
- ),
- ...ALL_QUERIES_COMBINATIONS.map(
- (queryMethod) =>
- ({
- code: `
+ errors: [
+ {
+ messageId: 'preferScreenQueries',
+ data: {
+ name: queryMethod,
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...ALL_QUERIES_COMBINATIONS.map(
+ (queryMethod) =>
+ ({
+ code: `
const { ${queryMethod} } = render(baz, { hydrate: true })
${queryMethod}(baz)
`,
- errors: [
- {
- messageId: 'preferScreenQueries',
- data: {
- name: queryMethod,
- },
- },
- ],
- } as const)
- ),
- ...ALL_QUERIES_COMBINATIONS.map(
- (queryMethod) =>
- ({
- code: `
+ errors: [
+ {
+ messageId: 'preferScreenQueries',
+ data: {
+ name: queryMethod,
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...ALL_QUERIES_COMBINATIONS.map(
+ (queryMethod) =>
+ ({
+ code: `
const [myVariable] = within()
myVariable.${queryMethod}(baz)
`,
- errors: [
- {
- messageId: 'preferScreenQueries',
- data: {
- name: queryMethod,
- },
- },
- ],
- } as const)
- ),
- {
- code: ` // issue #367 - example A
- import { render } from '@testing-library/react';
-
+ errors: [
+ {
+ messageId: 'preferScreenQueries',
+ data: {
+ name: queryMethod,
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
+ {
+ code: ` // issue #367 - example A
+ import { render } from '${testingFramework}';
+
function setup() {
return render();
}
-
+
it('foo', async () => {
const { getByText } = await setup();
expect(getByText('foo')).toBeInTheDocument();
});
-
+
it('bar', () => {
const { getByText } = setup();
expect(getByText('foo')).toBeInTheDocument();
});
`,
- errors: [
- {
- messageId: 'preferScreenQueries',
- line: 10,
- column: 16,
- data: {
- name: 'getByText',
- },
- },
- {
- messageId: 'preferScreenQueries',
- line: 15,
- column: 16,
- data: {
- name: 'getByText',
- },
- },
- ],
- },
- {
- code: ` // issue #367 - example B
- import { render } from '@testing-library/react';
-
+ errors: [
+ {
+ messageId: 'preferScreenQueries',
+ line: 10,
+ column: 16,
+ data: {
+ name: 'getByText',
+ },
+ },
+ {
+ messageId: 'preferScreenQueries',
+ line: 15,
+ column: 16,
+ data: {
+ name: 'getByText',
+ },
+ },
+ ],
+ } as const,
+ {
+ code: ` // issue #367 - example B
+ import { render } from '${testingFramework}';
+
function setup() {
return render();
}
-
+
it('foo', () => {
const { getByText } = setup();
expect(getByText('foo')).toBeInTheDocument();
});
-
+
it('bar', () => {
const results = setup();
const { getByText } = results;
expect(getByText('foo')).toBe('foo');
});
`,
- errors: [
- {
- messageId: 'preferScreenQueries',
- line: 10,
- column: 16,
- data: {
- name: 'getByText',
- },
- },
- {
- messageId: 'preferScreenQueries',
- line: 16,
- column: 16,
- data: {
- name: 'getByText',
- },
- },
- ],
- },
- ],
+ errors: [
+ {
+ messageId: 'preferScreenQueries',
+ line: 10,
+ column: 16,
+ data: {
+ name: 'getByText',
+ },
+ },
+ {
+ messageId: 'preferScreenQueries',
+ line: 16,
+ column: 16,
+ data: {
+ name: 'getByText',
+ },
+ },
+ ],
+ } as const,
+ ]),
+ ],
});
diff --git a/tests/lib/rules/prefer-user-event.test.ts b/tests/lib/rules/prefer-user-event.test.ts
index 4b7de9a2..299a65dc 100644
--- a/tests/lib/rules/prefer-user-event.test.ts
+++ b/tests/lib/rules/prefer-user-event.test.ts
@@ -1,614 +1,615 @@
-import { TSESLint } from '@typescript-eslint/experimental-utils';
+import {
+ type InvalidTestCase,
+ type ValidTestCase,
+} from '@typescript-eslint/rule-tester';
import rule, {
- MAPPING_TO_USER_EVENT,
- MessageIds,
- Options,
- RULE_NAME,
- UserEventMethods,
+ MAPPING_TO_USER_EVENT,
+ MessageIds,
+ Options,
+ RULE_NAME,
+ UserEventMethods,
} from '../../../lib/rules/prefer-user-event';
import { LIBRARY_MODULES } from '../../../lib/utils';
import { createRuleTester } from '../test-utils';
function createScenarioWithImport<
- T extends
- | TSESLint.InvalidTestCase
- | TSESLint.ValidTestCase
+ T extends InvalidTestCase | ValidTestCase,
>(callback: (libraryModule: string, fireEventMethod: string) => T) {
- return LIBRARY_MODULES.reduce(
- (acc: Array, libraryModule) =>
- acc.concat(
- Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) =>
- callback(libraryModule, fireEventMethod)
- )
- ),
- []
- );
+ return LIBRARY_MODULES.reduce(
+ (acc: Array, libraryModule) =>
+ acc.concat(
+ Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) =>
+ callback(libraryModule, fireEventMethod)
+ )
+ ),
+ []
+ );
}
const ruleTester = createRuleTester();
function formatUserEventMethodsMessage(fireEventMethod: string): string {
- const userEventMethods = MAPPING_TO_USER_EVENT[fireEventMethod].map(
- (methodName) => `userEvent.${methodName}`
- );
- let joinedList = '';
+ const userEventMethods = MAPPING_TO_USER_EVENT[fireEventMethod].map(
+ (methodName) => `userEvent.${methodName}`
+ );
+ let joinedList = '';
- for (let i = 0; i < userEventMethods.length; i++) {
- const item = userEventMethods[i];
- if (i === 0) {
- joinedList += item;
- } else if (i + 1 === userEventMethods.length) {
- joinedList += `, or ${item}`;
- } else {
- joinedList += `, ${item}`;
- }
- }
+ for (let i = 0; i < userEventMethods.length; i++) {
+ const item = userEventMethods[i];
+ if (i === 0) {
+ joinedList += item;
+ } else if (i + 1 === userEventMethods.length) {
+ joinedList += `, or ${item}`;
+ } else {
+ joinedList += `, ${item}`;
+ }
+ }
- return joinedList;
+ return joinedList;
}
ruleTester.run(RULE_NAME, rule, {
- valid: [
- {
- code: `
+ valid: [
+ {
+ code: `
import { screen } from '@testing-library/user-event'
const element = screen.getByText(foo)
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const utils = render(baz)
const element = utils.getByText(foo)
`,
- },
- ...UserEventMethods.map((userEventMethod) => ({
- code: `
+ },
+ ...UserEventMethods.map((userEventMethod) => ({
+ code: `
import userEvent from '@testing-library/user-event'
const node = document.createElement(elementType)
userEvent.${userEventMethod}(foo)
`,
- })),
- ...createScenarioWithImport>(
- (libraryModule: string, fireEventMethod: string) => ({
- code: `
+ })),
+ ...createScenarioWithImport>(
+ (libraryModule: string, fireEventMethod: string) => ({
+ code: `
import { fireEvent } from '${libraryModule}'
const node = document.createElement(elementType)
fireEvent.${fireEventMethod}(foo)
`,
- options: [{ allowedMethods: [fireEventMethod] }],
- })
- ),
- ...createScenarioWithImport>(
- (libraryModule: string, fireEventMethod: string) => ({
- code: `
+ options: [{ allowedMethods: [fireEventMethod] }],
+ })
+ ),
+ ...createScenarioWithImport>(
+ (libraryModule: string, fireEventMethod: string) => ({
+ code: `
import { fireEvent as fireEventAliased } from '${libraryModule}'
const node = document.createElement(elementType)
fireEventAliased.${fireEventMethod}(foo)
`,
- options: [{ allowedMethods: [fireEventMethod] }],
- })
- ),
- ...createScenarioWithImport>(
- (libraryModule: string, fireEventMethod: string) => ({
- code: `
+ options: [{ allowedMethods: [fireEventMethod] }],
+ })
+ ),
+ ...createScenarioWithImport>(
+ (libraryModule: string, fireEventMethod: string) => ({
+ code: `
import * as dom from '${libraryModule}'
dom.fireEvent.${fireEventMethod}(foo)
`,
- options: [{ allowedMethods: [fireEventMethod] }],
- })
- ),
- ...LIBRARY_MODULES.map((libraryModule) => ({
- // imported fireEvent and not used,
- code: `
+ options: [{ allowedMethods: [fireEventMethod] }],
+ })
+ ),
+ ...LIBRARY_MODULES.map((libraryModule) => ({
+ // imported fireEvent and not used,
+ code: `
import { fireEvent } from '${libraryModule}'
import * as foo from 'someModule'
foo.baz()
`,
- })),
- ...LIBRARY_MODULES.map((libraryModule) => ({
- // imported dom, but not using fireEvent
- code: `
+ })),
+ ...LIBRARY_MODULES.map((libraryModule) => ({
+ // imported dom, but not using fireEvent
+ code: `
import * as dom from '${libraryModule}'
const button = dom.screen.getByRole('button')
const foo = dom.screen.container.querySelector('baz')
`,
- })),
- ...LIBRARY_MODULES.map((libraryModule) => ({
- code: `
+ })),
+ ...LIBRARY_MODULES.map((libraryModule) => ({
+ code: `
import { fireEvent as aliasedFireEvent } from '${libraryModule}'
function fireEvent() {
console.log('foo')
}
fireEvent()
`,
- })),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ })),
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { screen } from 'test-utils'
const element = screen.getByText(foo)
`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { render } from 'test-utils'
const utils = render(baz)
const element = utils.getByText(foo)
`,
- },
- ...UserEventMethods.map((userEventMethod) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ },
+ ...UserEventMethods.map((userEventMethod) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import userEvent from 'test-utils'
const node = document.createElement(elementType)
userEvent.${userEventMethod}(foo)
`,
- })),
- ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod: string) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ })),
+ ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod: string) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
// fireEvent method used but not imported from TL related module
// (aggressive reporting opted out)
import { fireEvent } from 'somewhere-else'
fireEvent.${fireEventMethod}(foo)
`,
- })),
- ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod: string) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ })),
+ ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod: string) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { fireEvent } from 'test-utils'
const node = document.createElement(elementType)
fireEvent.${fireEventMethod}(foo)
`,
- options: [{ allowedMethods: [fireEventMethod] }],
- })),
- ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod: string) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ options: [{ allowedMethods: [fireEventMethod] }],
+ })),
+ ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod: string) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { fireEvent as fireEventAliased } from 'test-utils'
const node = document.createElement(elementType)
fireEventAliased.${fireEventMethod}(foo)
`,
- options: [{ allowedMethods: [fireEventMethod] }],
- })),
- ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod: string) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ options: [{ allowedMethods: [fireEventMethod] }],
+ })),
+ ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod: string) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import * as dom from 'test-utils'
dom.fireEvent.${fireEventMethod}(foo)
`,
- options: [{ allowedMethods: [fireEventMethod] }],
- })),
- // edge case for coverage:
- // valid use case without call expression
- // so there is no innermost function scope found
- `
+ options: [{ allowedMethods: [fireEventMethod] }],
+ })),
+ // edge case for coverage:
+ // valid use case without call expression
+ // so there is no innermost function scope found
+ `
import { fireEvent } from '@testing-library/react';
test('edge case for no innermost function scope', () => {
const click = fireEvent.click
})
`,
- ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { fireEvent, createEvent } from 'test-utils'
const event = createEvent.${fireEventMethod}(node)
fireEvent(node, event)
`,
- options: [{ allowedMethods: [fireEventMethod] }],
- })),
- ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ options: [{ allowedMethods: [fireEventMethod] }],
+ })),
+ ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { fireEvent as fireEventAliased, createEvent } from 'test-utils'
fireEventAliased(node, createEvent.${fireEventMethod}(node))
`,
- options: [{ allowedMethods: [fireEventMethod] }],
- })),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ options: [{ allowedMethods: [fireEventMethod] }],
+ })),
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { fireEvent, createEvent } from 'test-utils'
const event = createEvent.drop(node)
fireEvent(node, event)
`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { fireEvent, createEvent } from 'test-utils'
const event = createEvent('drop', node)
fireEvent(node, event)
`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { fireEvent as fireEventAliased, createEvent as createEventAliased } from 'test-utils'
const event = createEventAliased.drop(node)
fireEventAliased(node, event)
`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ },
+ {
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { fireEvent as fireEventAliased, createEvent as createEventAliased } from 'test-utils'
const event = createEventAliased('drop', node)
fireEventAliased(node, event)
`,
- },
- {
- code: `
+ },
+ {
+ code: `
const createEvent = () => 'Event';
const event = createEvent();
`,
- },
- ],
- invalid: [
- ...createScenarioWithImport>(
- (libraryModule: string, fireEventMethod: string) => ({
- code: `
+ },
+ ],
+ invalid: [
+ ...createScenarioWithImport>(
+ (libraryModule: string, fireEventMethod: string) => ({
+ code: `
import { fireEvent } from '${libraryModule}'
const node = document.createElement(elementType)
fireEvent.${fireEventMethod}(foo)
`,
- errors: [
- {
- messageId: 'preferUserEvent',
- line: 4,
- column: 9,
- data: {
- userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
- fireEventMethod,
- },
- },
- ],
- })
- ),
- ...createScenarioWithImport>(
- (libraryModule: string, fireEventMethod: string) => ({
- code: `
+ errors: [
+ {
+ messageId: 'preferUserEvent',
+ line: 4,
+ column: 9,
+ data: {
+ userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
+ fireEventMethod,
+ },
+ },
+ ],
+ })
+ ),
+ ...createScenarioWithImport>(
+ (libraryModule: string, fireEventMethod: string) => ({
+ code: `
import * as dom from '${libraryModule}'
dom.fireEvent.${fireEventMethod}(foo)
`,
- errors: [
- {
- messageId: 'preferUserEvent',
- line: 3,
- column: 9,
- data: {
- userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
- fireEventMethod,
- },
- },
- ],
- })
- ),
- ...createScenarioWithImport>(
- (libraryModule: string, fireEventMethod: string) => ({
- code: `
+ errors: [
+ {
+ messageId: 'preferUserEvent',
+ line: 3,
+ column: 9,
+ data: {
+ userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
+ fireEventMethod,
+ },
+ },
+ ],
+ })
+ ),
+ ...createScenarioWithImport>(
+ (libraryModule: string, fireEventMethod: string) => ({
+ code: `
const { fireEvent } = require('${libraryModule}')
fireEvent.${fireEventMethod}(foo)
`,
- errors: [
- {
- messageId: 'preferUserEvent',
- line: 3,
- column: 9,
- data: {
- userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
- fireEventMethod,
- },
- },
- ],
- })
- ),
- ...createScenarioWithImport>(
- (libraryModule: string, fireEventMethod: string) => ({
- code: `
+ errors: [
+ {
+ messageId: 'preferUserEvent',
+ line: 3,
+ column: 9,
+ data: {
+ userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
+ fireEventMethod,
+ },
+ },
+ ],
+ })
+ ),
+ ...createScenarioWithImport>(
+ (libraryModule: string, fireEventMethod: string) => ({
+ code: `
const rtl = require('${libraryModule}')
rtl.fireEvent.${fireEventMethod}(foo)
`,
- errors: [
- {
- messageId: 'preferUserEvent',
- line: 3,
- column: 9,
- data: {
- userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
- fireEventMethod,
- },
- },
- ],
- })
- ),
- ...Object.keys(MAPPING_TO_USER_EVENT).map(
- (fireEventMethod: string) =>
- ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [
+ {
+ messageId: 'preferUserEvent',
+ line: 3,
+ column: 9,
+ data: {
+ userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
+ fireEventMethod,
+ },
+ },
+ ],
+ })
+ ),
+ ...Object.keys(MAPPING_TO_USER_EVENT).map(
+ (fireEventMethod: string) =>
+ ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import * as dom from 'test-utils'
dom.fireEvent.${fireEventMethod}(foo)
`,
- errors: [
- {
- messageId: 'preferUserEvent',
- line: 3,
- column: 9,
- data: {
- userEventMethods:
- formatUserEventMethodsMessage(fireEventMethod),
- fireEventMethod,
- },
- },
- ],
- } as const)
- ),
- ...Object.keys(MAPPING_TO_USER_EVENT).map(
- (fireEventMethod: string) =>
- ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [
+ {
+ messageId: 'preferUserEvent',
+ line: 3,
+ column: 9,
+ data: {
+ userEventMethods:
+ formatUserEventMethodsMessage(fireEventMethod),
+ fireEventMethod,
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...Object.keys(MAPPING_TO_USER_EVENT).map(
+ (fireEventMethod: string) =>
+ ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { fireEvent } from 'test-utils'
fireEvent.${fireEventMethod}(foo)
`,
- errors: [
- {
- messageId: 'preferUserEvent',
- line: 3,
- column: 9,
- data: {
- userEventMethods:
- formatUserEventMethodsMessage(fireEventMethod),
- fireEventMethod,
- },
- },
- ],
- } as const)
- ),
- ...Object.keys(MAPPING_TO_USER_EVENT).map(
- (fireEventMethod: string) =>
- ({
- code: `
+ errors: [
+ {
+ messageId: 'preferUserEvent',
+ line: 3,
+ column: 9,
+ data: {
+ userEventMethods:
+ formatUserEventMethodsMessage(fireEventMethod),
+ fireEventMethod,
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...Object.keys(MAPPING_TO_USER_EVENT).map(
+ (fireEventMethod: string) =>
+ ({
+ code: `
// same as previous group of test cases but without custom module set
// (aggressive reporting)
import { fireEvent } from 'test-utils'
fireEvent.${fireEventMethod}(foo)
`,
- errors: [
- {
- messageId: 'preferUserEvent',
- line: 5,
- column: 9,
- data: {
- userEventMethods:
- formatUserEventMethodsMessage(fireEventMethod),
- fireEventMethod,
- },
- },
- ],
- } as const)
- ),
- ...Object.keys(MAPPING_TO_USER_EVENT).map(
- (fireEventMethod: string) =>
- ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [
+ {
+ messageId: 'preferUserEvent',
+ line: 5,
+ column: 9,
+ data: {
+ userEventMethods:
+ formatUserEventMethodsMessage(fireEventMethod),
+ fireEventMethod,
+ },
+ },
+ ],
+ }) as const
+ ),
+ ...Object.keys(MAPPING_TO_USER_EVENT).map(
+ (fireEventMethod: string) =>
+ ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { fireEvent as fireEventAliased } from 'test-utils'
fireEventAliased.${fireEventMethod}(foo)
`,
- errors: [
- {
- messageId: 'preferUserEvent',
- line: 3,
- column: 9,
- data: {
- userEventMethods:
- formatUserEventMethodsMessage(fireEventMethod),
- fireEventMethod,
- },
- },
- ],
- } as const)
- ),
- {
- code: ` // simple test to check error in detail
+ errors: [
+ {
+ messageId: 'preferUserEvent',
+ line: 3,
+ column: 9,
+ data: {
+ userEventMethods:
+ formatUserEventMethodsMessage(fireEventMethod),
+ fireEventMethod,
+ },
+ },
+ ],
+ }) as const
+ ),
+ {
+ code: ` // simple test to check error in detail
import { fireEvent } from '@testing-library/react'
fireEvent.click(element)
fireEvent.mouseOut(element)
`,
- errors: [
- {
- messageId: 'preferUserEvent',
- line: 4,
- endLine: 4,
- column: 7,
- endColumn: 22,
- data: {
- userEventMethods:
- 'userEvent.click, userEvent.type, userEvent.selectOptions, or userEvent.deselectOptions',
- fireEventMethod: 'click',
- },
- },
- {
- messageId: 'preferUserEvent',
- line: 5,
- endLine: 5,
- column: 7,
- endColumn: 25,
- data: {
- userEventMethods: 'userEvent.unhover',
- fireEventMethod: 'mouseOut',
- },
- },
- ],
- },
- ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [
+ {
+ messageId: 'preferUserEvent',
+ line: 4,
+ endLine: 4,
+ column: 7,
+ endColumn: 22,
+ data: {
+ userEventMethods:
+ 'userEvent.click, userEvent.type, userEvent.selectOptions, or userEvent.deselectOptions',
+ fireEventMethod: 'click',
+ },
+ },
+ {
+ messageId: 'preferUserEvent',
+ line: 5,
+ endLine: 5,
+ column: 7,
+ endColumn: 25,
+ data: {
+ userEventMethods: 'userEvent.unhover',
+ fireEventMethod: 'mouseOut',
+ },
+ },
+ ],
+ },
+ ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { fireEvent, createEvent } from 'test-utils'
-
+
fireEvent(node, createEvent('${fireEventMethod}', node))
`,
- errors: [
- {
- messageId: 'preferUserEvent',
- line: 4,
- column: 9,
- data: {
- userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
- fireEventMethod,
- },
- } as const,
- ],
- })),
- ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [
+ {
+ messageId: 'preferUserEvent',
+ line: 4,
+ column: 9,
+ data: {
+ userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
+ fireEventMethod,
+ },
+ } as const,
+ ],
+ })),
+ ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { fireEvent, createEvent } from 'test-utils'
fireEvent(node, createEvent.${fireEventMethod}(node))
`,
- errors: [
- {
- messageId: 'preferUserEvent',
- line: 4,
- column: 9,
- data: {
- userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
- fireEventMethod,
- },
- } as const,
- ],
- })),
- ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [
+ {
+ messageId: 'preferUserEvent',
+ line: 4,
+ column: 9,
+ data: {
+ userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
+ fireEventMethod,
+ },
+ } as const,
+ ],
+ })),
+ ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { fireEvent, createEvent } from 'test-utils'
const event = createEvent.${fireEventMethod}(node)
fireEvent(node, event)
`,
- errors: [
- {
- messageId: 'preferUserEvent',
- line: 4,
- column: 9,
- data: {
- userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
- fireEventMethod,
- },
- } as const,
- ],
- })),
- ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [
+ {
+ messageId: 'preferUserEvent',
+ line: 4,
+ column: 9,
+ data: {
+ userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
+ fireEventMethod,
+ },
+ } as const,
+ ],
+ })),
+ ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import { fireEvent as fireEventAliased, createEvent as createEventAliased } from 'test-utils'
const eventValid = createEventAliased.drop(node)
fireEventAliased(node, eventValid)
const eventInvalid = createEventAliased.${fireEventMethod}(node)
fireEventAliased(node, eventInvalid)
`,
- errors: [
- {
- messageId: 'preferUserEvent',
- line: 6,
- column: 9,
- data: {
- userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
- fireEventMethod,
- },
- } as const,
- ],
- })),
- ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [
+ {
+ messageId: 'preferUserEvent',
+ line: 6,
+ column: 9,
+ data: {
+ userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
+ fireEventMethod,
+ },
+ } as const,
+ ],
+ })),
+ ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import * as dom from 'test-utils'
const eventValid = dom.createEvent.drop(node)
dom.fireEvent(node, eventValid)
const eventInvalid = dom.createEvent.${fireEventMethod}(node)
dom.fireEvent(node, eventInvalid)
`,
- errors: [
- {
- messageId: 'preferUserEvent',
- line: 6,
- column: 9,
- data: {
- userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
- fireEventMethod,
- },
- } as const,
- ],
- })),
- ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
+ errors: [
+ {
+ messageId: 'preferUserEvent',
+ line: 6,
+ column: 9,
+ data: {
+ userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
+ fireEventMethod,
+ },
+ } as const,
+ ],
+ })),
+ ...Object.keys(MAPPING_TO_USER_EVENT).map((fireEventMethod) => ({
+ settings: {
+ 'testing-library/utils-module': 'test-utils',
+ },
+ code: `
import * as dom from 'test-utils'
// valid event
dom.fireEvent(node, dom.createEvent.drop(node))
// invalid event
dom.fireEvent(node, dom.createEvent.${fireEventMethod}(node))
`,
- errors: [
- {
- messageId: 'preferUserEvent',
- line: 6,
- column: 9,
- data: {
- userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
- fireEventMethod,
- },
- } as const,
- ],
- })),
- ],
+ errors: [
+ {
+ messageId: 'preferUserEvent',
+ line: 6,
+ column: 9,
+ data: {
+ userEventMethods: formatUserEventMethodsMessage(fireEventMethod),
+ fireEventMethod,
+ },
+ } as const,
+ ],
+ })),
+ ],
});
diff --git a/tests/lib/rules/prefer-wait-for.test.ts b/tests/lib/rules/prefer-wait-for.test.ts
deleted file mode 100644
index ae6a85c4..00000000
--- a/tests/lib/rules/prefer-wait-for.test.ts
+++ /dev/null
@@ -1,2258 +0,0 @@
-import rule, { RULE_NAME } from '../../../lib/rules/prefer-wait-for';
-import { LIBRARY_MODULES } from '../../../lib/utils';
-import { createRuleTester } from '../test-utils';
-
-const ruleTester = createRuleTester();
-
-ruleTester.run(RULE_NAME, rule, {
- valid: [
- ...LIBRARY_MODULES.map((libraryModule) => ({
- code: `import { waitFor, render } from '${libraryModule}';
-
- async () => {
- await waitFor(() => {});
- }`,
- })),
- ...LIBRARY_MODULES.map((libraryModule) => ({
- code: `const { waitFor, render } = require('${libraryModule}');
-
- async () => {
- await waitFor(() => {});
- }`,
- })),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import { waitFor, render } from 'test-utils';
-
- async () => {
- await waitFor(() => {});
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const { waitFor, render } = require('test-utils');
-
- async () => {
- await waitFor(() => {});
- }`,
- },
- ...LIBRARY_MODULES.map((libraryModule) => ({
- code: `import { waitForElementToBeRemoved, render } from '${libraryModule}';
-
- async () => {
- await waitForElementToBeRemoved(() => {});
- }`,
- })),
- ...LIBRARY_MODULES.map((libraryModule) => ({
- code: `const { waitForElementToBeRemoved, render } = require('${libraryModule}');
-
- async () => {
- await waitForElementToBeRemoved(() => {});
- }`,
- })),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import { waitForElementToBeRemoved, render } from 'test-utils';
-
- async () => {
- await waitForElementToBeRemoved(() => {});
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const { waitForElementToBeRemoved, render } = require('test-utils');
-
- async () => {
- await waitForElementToBeRemoved(() => {});
- }`,
- },
- ...LIBRARY_MODULES.map((libraryModule) => ({
- code: `import * as testingLibrary from '${libraryModule}';
-
- async () => {
- await testingLibrary.waitForElementToBeRemoved(() => {});
- }`,
- })),
- ...LIBRARY_MODULES.map((libraryModule) => ({
- code: `const testingLibrary = require('${libraryModule}');
-
- async () => {
- await testingLibrary.waitForElementToBeRemoved(() => {});
- }`,
- })),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import * as testingLibrary from 'test-utils';
-
- async () => {
- await testingLibrary.waitForElementToBeRemoved(() => {});
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const testingLibrary = require('test-utils');
-
- async () => {
- await testingLibrary.waitForElementToBeRemoved(() => {});
- }`,
- },
- ...LIBRARY_MODULES.map((libraryModule) => ({
- code: `import { render } from '${libraryModule}';
- import { waitForSomethingElse } from 'other-module';
-
- async () => {
- await waitForSomethingElse(() => {});
- }`,
- })),
- ...LIBRARY_MODULES.map((libraryModule) => ({
- code: `const { render } = require('${libraryModule}');
- const { waitForSomethingElse } = require('other-module');
-
- async () => {
- await waitForSomethingElse(() => {});
- }`,
- })),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import { render } from 'test-utils';
- import { waitForSomethingElse } from 'other-module';
-
- async () => {
- await waitForSomethingElse(() => {});
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const { render } = require('test-utils');
- const { waitForSomethingElse } = require('other-module');
-
- async () => {
- await waitForSomethingElse(() => {});
- }`,
- },
- ...LIBRARY_MODULES.map((libraryModule) => ({
- code: `import * as testingLibrary from '${libraryModule}';
-
- async () => {
- await testingLibrary.waitFor(() => {}, { timeout: 500 });
- }`,
- })),
- ...LIBRARY_MODULES.map((libraryModule) => ({
- code: `const testingLibrary = require('${libraryModule}');
-
- async () => {
- await testingLibrary.waitFor(() => {}, { timeout: 500 });
- }`,
- })),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import * as testingLibrary from 'test-utils';
-
- async () => {
- await testingLibrary.waitFor(() => {}, { timeout: 500 });
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const testingLibrary = require('test-utils');
-
- async () => {
- await testingLibrary.waitFor(() => {}, { timeout: 500 });
- }`,
- },
- {
- code: `import { wait } from 'imNoTestingLibrary';
-
- async () => {
- await wait();
- }`,
- },
- {
- code: `const { wait } = require('imNoTestingLibrary');
-
- async () => {
- await wait();
- }`,
- },
- {
- code: `import * as foo from 'imNoTestingLibrary';
-
- async () => {
- await foo.wait();
- }`,
- },
- {
- code: `const foo = require('imNoTestingLibrary');
-
- async () => {
- await foo.wait();
- }`,
- },
- {
- code: `import * as foo from 'imNoTestingLibrary';
- cy.wait();
- `,
- },
- {
- code: `const foo = require('imNoTestingLibrary');
- cy.wait();
- `,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `
- // case: aggressive reporting disabled - method named same as invalid method
- // but not coming from Testing Library is valid
- import { wait as testingLibraryWait } from 'test-utils'
- import { wait } from 'somewhere-else'
-
- async () => {
- await wait();
- }
- `,
- },
- {
- // https://github.com/testing-library/eslint-plugin-testing-library/issues/145
- code: `import * as foo from 'imNoTestingLibrary';
- async function wait(): Promise {
- // doesn't matter
- }
-
- function callsWait(): void {
- await wait();
- }
- `,
- },
- {
- // https://github.com/testing-library/eslint-plugin-testing-library/issues/145
- code: `const foo = require('imNoTestingLibrary');
- async function wait(): Promise {
- // doesn't matter
- }
-
- function callsWait(): void {
- await wait();
- }
- `,
- },
- ],
-
- invalid: [
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `import { wait, render } from '${libraryModule}';
-
- async () => {
- await wait();
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { render,waitFor } from '${libraryModule}';
-
- async () => {
- await waitFor(() => {});
- }`,
- } as const)
- ),
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `const { wait, render } = require('${libraryModule}');
-
- async () => {
- await wait();
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { render,waitFor } = require('${libraryModule}');
-
- async () => {
- await waitFor(() => {});
- }`,
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import { wait, render } from 'test-utils';
-
- async () => {
- await wait();
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { render,waitFor } from 'test-utils';
-
- async () => {
- await waitFor(() => {});
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const { wait, render } = require('test-utils');
-
- async () => {
- await wait();
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { render,waitFor } = require('test-utils');
-
- async () => {
- await waitFor(() => {});
- }`,
- },
- // namespaced wait should be fixed but not its import
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `import * as testingLibrary from '${libraryModule}';
-
- async () => {
- await testingLibrary.wait();
- }`,
- errors: [
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 30,
- },
- ],
- output: `import * as testingLibrary from '${libraryModule}';
-
- async () => {
- await testingLibrary.waitFor(() => {});
- }`,
- } as const)
- ),
- // namespaced wait should be fixed but not its import
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `const testingLibrary = require('${libraryModule}');
-
- async () => {
- await testingLibrary.wait();
- }`,
- errors: [
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 30,
- },
- ],
- output: `const testingLibrary = require('${libraryModule}');
-
- async () => {
- await testingLibrary.waitFor(() => {});
- }`,
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import * as testingLibrary from 'test-utils';
-
- async () => {
- await testingLibrary.wait();
- }`,
- errors: [
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 30,
- },
- ],
- output: `import * as testingLibrary from 'test-utils';
-
- async () => {
- await testingLibrary.waitFor(() => {});
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const testingLibrary = require('test-utils');
-
- async () => {
- await testingLibrary.wait();
- }`,
- errors: [
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 30,
- },
- ],
- output: `const testingLibrary = require('test-utils');
-
- async () => {
- await testingLibrary.waitFor(() => {});
- }`,
- },
- // namespaced waitForDomChange should be fixed but not its import
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `import * as testingLibrary from '${libraryModule}';
-
- async () => {
- await testingLibrary.waitForDomChange({ timeout: 500 });
- }`,
- errors: [
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 30,
- },
- ],
- output: `import * as testingLibrary from '${libraryModule}';
-
- async () => {
- await testingLibrary.waitFor(() => {}, { timeout: 500 });
- }`,
- } as const)
- ),
- // namespaced waitForDomChange should be fixed but not its import
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `const testingLibrary = require('${libraryModule}');
-
- async () => {
- await testingLibrary.waitForDomChange({ timeout: 500 });
- }`,
- errors: [
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 30,
- },
- ],
- output: `const testingLibrary = require('${libraryModule}');
-
- async () => {
- await testingLibrary.waitFor(() => {}, { timeout: 500 });
- }`,
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import * as testingLibrary from 'test-utils';
-
- async () => {
- await testingLibrary.waitForDomChange({ timeout: 500 });
- }`,
- errors: [
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 30,
- },
- ],
- output: `import * as testingLibrary from 'test-utils';
-
- async () => {
- await testingLibrary.waitFor(() => {}, { timeout: 500 });
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const testingLibrary = require('test-utils');
-
- async () => {
- await testingLibrary.waitForDomChange({ timeout: 500 });
- }`,
- errors: [
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 30,
- },
- ],
- output: `const testingLibrary = require('test-utils');
-
- async () => {
- await testingLibrary.waitFor(() => {}, { timeout: 500 });
- }`,
- },
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `import { render, wait } from '${libraryModule}'
-
- async () => {
- await wait(() => {});
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { render,waitFor } from '${libraryModule}';
-
- async () => {
- await waitFor(() => {});
- }`,
- } as const)
- ),
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `const { render, wait } = require('${libraryModule}');
-
- async () => {
- await wait(() => {});
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { render,waitFor } = require('${libraryModule}');
-
- async () => {
- await waitFor(() => {});
- }`,
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import { render, wait } from 'test-utils'
-
- async () => {
- await wait(() => {});
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { render,waitFor } from 'test-utils';
-
- async () => {
- await waitFor(() => {});
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const { render, wait } = require('test-utils');
-
- async () => {
- await wait(() => {});
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { render,waitFor } = require('test-utils');
-
- async () => {
- await waitFor(() => {});
- }`,
- },
- // this import doesn't have trailing semicolon but fixer adds it
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `import { render, wait, screen } from "${libraryModule}";
-
- async () => {
- await wait(function cb() {
- doSomething();
- });
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { render,screen,waitFor } from '${libraryModule}';
-
- async () => {
- await waitFor(function cb() {
- doSomething();
- });
- }`,
- } as const)
- ),
- // this import doesn't have trailing semicolon but fixer adds it
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `import { render, wait, screen } from "${libraryModule}";
-
- async () => {
- await wait(function cb() {
- doSomething();
- });
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { render,screen,waitFor } from '${libraryModule}';
-
- async () => {
- await waitFor(function cb() {
- doSomething();
- });
- }`,
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import { render, wait, screen } from "test-utils";
-
- async () => {
- await wait(function cb() {
- doSomething();
- });
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { render,screen,waitFor } from 'test-utils';
-
- async () => {
- await waitFor(function cb() {
- doSomething();
- });
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const { render, wait, screen } = require('test-utils');
-
- async () => {
- await wait(function cb() {
- doSomething();
- });
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { render,screen,waitFor } = require('test-utils');
-
- async () => {
- await waitFor(function cb() {
- doSomething();
- });
- }`,
- },
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `import { render, waitForElement, screen } from '${libraryModule}'
-
- async () => {
- await waitForElement(() => {});
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { render,screen,waitFor } from '${libraryModule}';
-
- async () => {
- await waitFor(() => {});
- }`,
- } as const)
- ),
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `const { render, waitForElement, screen } = require('${libraryModule}');
-
- async () => {
- await waitForElement(() => {});
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { render,screen,waitFor } = require('${libraryModule}');
-
- async () => {
- await waitFor(() => {});
- }`,
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import { render, waitForElement, screen } from 'test-utils'
-
- async () => {
- await waitForElement(() => {});
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { render,screen,waitFor } from 'test-utils';
-
- async () => {
- await waitFor(() => {});
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const { render, waitForElement, screen } = require('test-utils');
-
- async () => {
- await waitForElement(() => {});
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { render,screen,waitFor } = require('test-utils');
-
- async () => {
- await waitFor(() => {});
- }`,
- },
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `import { waitForElement } from '${libraryModule}';
-
- async () => {
- await waitForElement(function cb() {
- doSomething();
- });
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { waitFor } from '${libraryModule}';
-
- async () => {
- await waitFor(function cb() {
- doSomething();
- });
- }`,
- } as const)
- ),
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `const { waitForElement } = require('${libraryModule}');
-
- async () => {
- await waitForElement(function cb() {
- doSomething();
- });
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { waitFor } = require('${libraryModule}');
-
- async () => {
- await waitFor(function cb() {
- doSomething();
- });
- }`,
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import { waitForElement } from 'test-utils';
-
- async () => {
- await waitForElement(function cb() {
- doSomething();
- });
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { waitFor } from 'test-utils';
-
- async () => {
- await waitFor(function cb() {
- doSomething();
- });
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const { waitForElement } = require('test-utils');
-
- async () => {
- await waitForElement(function cb() {
- doSomething();
- });
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { waitFor } = require('test-utils');
-
- async () => {
- await waitFor(function cb() {
- doSomething();
- });
- }`,
- },
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `import { waitForDomChange } from '${libraryModule}';
-
- async () => {
- await waitForDomChange();
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { waitFor } from '${libraryModule}';
-
- async () => {
- await waitFor(() => {});
- }`,
- } as const)
- ),
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `const { waitForDomChange } = require('${libraryModule}');
-
- async () => {
- await waitForDomChange();
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { waitFor } = require('${libraryModule}');
-
- async () => {
- await waitFor(() => {});
- }`,
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import { waitForDomChange } from 'test-utils';
-
- async () => {
- await waitForDomChange();
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { waitFor } from 'test-utils';
-
- async () => {
- await waitFor(() => {});
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const { waitForDomChange } = require('test-utils');
-
- async () => {
- await waitForDomChange();
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { waitFor } = require('test-utils');
-
- async () => {
- await waitFor(() => {});
- }`,
- },
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `import { waitForDomChange } from '${libraryModule}';
-
- async () => {
- await waitForDomChange(mutationObserverOptions);
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { waitFor } from '${libraryModule}';
-
- async () => {
- await waitFor(() => {}, mutationObserverOptions);
- }`,
- } as const)
- ),
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `const { waitForDomChange } = require('${libraryModule}');
-
- async () => {
- await waitForDomChange(mutationObserverOptions);
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { waitFor } = require('${libraryModule}');
-
- async () => {
- await waitFor(() => {}, mutationObserverOptions);
- }`,
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import { waitForDomChange } from 'test-utils';
-
- async () => {
- await waitForDomChange(mutationObserverOptions);
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { waitFor } from 'test-utils';
-
- async () => {
- await waitFor(() => {}, mutationObserverOptions);
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const { waitForDomChange } = require('test-utils');
-
- async () => {
- await waitForDomChange(mutationObserverOptions);
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { waitFor } = require('test-utils');
-
- async () => {
- await waitFor(() => {}, mutationObserverOptions);
- }`,
- },
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `import { waitForDomChange } from '${libraryModule}';
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { waitFor } from '${libraryModule}';
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- }`,
- } as const)
- ),
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `const { waitForDomChange } = require('${libraryModule}');
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { waitFor } = require('${libraryModule}');
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- }`,
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import { waitForDomChange } from 'test-utils';
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { waitFor } from 'test-utils';
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const { waitForDomChange } = require('test-utils');
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { waitFor } = require('test-utils');
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- }`,
- },
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `import { waitForDomChange, wait, waitForElement } from '${libraryModule}';
- import userEvent from '@testing-library/user-event';
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 5,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 6,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 7,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 8,
- column: 15,
- },
- ],
- output: `import { waitFor } from '${libraryModule}';
- import userEvent from '@testing-library/user-event';
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- } as const)
- ),
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `const { waitForDomChange, wait, waitForElement } = require('${libraryModule}');
- const userEvent = require('@testing-library/user-event');
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 5,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 6,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 7,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 8,
- column: 15,
- },
- ],
- output: `const { waitFor } = require('${libraryModule}');
- const userEvent = require('@testing-library/user-event');
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import { waitForDomChange, wait, waitForElement } from 'test-utils';
- import userEvent from '@testing-library/user-event';
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 5,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 6,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 7,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 8,
- column: 15,
- },
- ],
- output: `import { waitFor } from 'test-utils';
- import userEvent from '@testing-library/user-event';
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const { waitForDomChange, wait, waitForElement } = require('test-utils');
- const userEvent = require('@testing-library/user-event');
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 5,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 6,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 7,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 8,
- column: 15,
- },
- ],
- output: `const { waitFor } = require('test-utils');
- const userEvent = require('@testing-library/user-event');
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- },
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `import { render, waitForDomChange, wait, waitForElement } from '${libraryModule}';
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 5,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 6,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 7,
- column: 15,
- },
- ],
- output: `import { render,waitFor } from '${libraryModule}';
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- } as const)
- ),
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `const { render, waitForDomChange, wait, waitForElement } = require('${libraryModule}');
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 5,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 6,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 7,
- column: 15,
- },
- ],
- output: `const { render,waitFor } = require('${libraryModule}');
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import { render, waitForDomChange, wait, waitForElement } from 'test-utils';
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 5,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 6,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 7,
- column: 15,
- },
- ],
- output: `import { render,waitFor } from 'test-utils';
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const { render, waitForDomChange, wait, waitForElement } = require('test-utils');
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 5,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 6,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 7,
- column: 15,
- },
- ],
- output: `const { render,waitFor } = require('test-utils');
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- },
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `import { waitForDomChange, wait, render, waitForElement } from '${libraryModule}';
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 5,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 6,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 7,
- column: 15,
- },
- ],
- output: `import { render,waitFor } from '${libraryModule}';
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- } as const)
- ),
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `const { waitForDomChange, wait, render, waitForElement } = require('${libraryModule}');
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 5,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 6,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 7,
- column: 15,
- },
- ],
- output: `const { render,waitFor } = require('${libraryModule}');
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import { waitForDomChange, wait, render, waitForElement } from 'test-utils';
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 5,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 6,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 7,
- column: 15,
- },
- ],
- output: `import { render,waitFor } from 'test-utils';
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const { waitForDomChange, wait, render, waitForElement } = require('test-utils');
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 5,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 6,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 7,
- column: 15,
- },
- ],
- output: `const { render,waitFor } = require('test-utils');
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- },
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `import {
- waitForDomChange,
- wait,
- render,
- waitForElement,
- } from '${libraryModule}';
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 9,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 10,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 11,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 12,
- column: 15,
- },
- ],
- output: `import { render,waitFor } from '${libraryModule}';
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- } as const)
- ),
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- code: `const {
- waitForDomChange,
- wait,
- render,
- waitForElement,
- } = require('${libraryModule}');
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 9,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 10,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 11,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 12,
- column: 15,
- },
- ],
- output: `const { render,waitFor } = require('${libraryModule}');
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `import {
- waitForDomChange,
- wait,
- render,
- waitForElement,
- } from 'test-utils';
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 9,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 10,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 11,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 12,
- column: 15,
- },
- ],
- output: `import { render,waitFor } from 'test-utils';
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- code: `const {
- waitForDomChange,
- wait,
- render,
- waitForElement,
- } = require('test-utils');
-
- async () => {
- await waitForDomChange({ timeout: 5000 });
- await waitForElement();
- await wait();
- await wait(() => { doSomething() });
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 9,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 10,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 11,
- column: 15,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 12,
- column: 15,
- },
- ],
- output: `const { render,waitFor } = require('test-utils');
-
- async () => {
- await waitFor(() => {}, { timeout: 5000 });
- await waitFor(() => {});
- await waitFor(() => {});
- await waitFor(() => { doSomething() });
- }`,
- },
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- // if already importing waitFor then it's not imported twice
- code: `import { wait, waitFor, render } from '${libraryModule}';
-
- async () => {
- await wait();
- await waitFor(someCallback);
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { render,waitFor } from '${libraryModule}';
-
- async () => {
- await waitFor(() => {});
- await waitFor(someCallback);
- }`,
- } as const)
- ),
- ...LIBRARY_MODULES.map(
- (libraryModule) =>
- ({
- // if already importing waitFor then it's not imported twice
- code: `const { wait, waitFor, render } = require('${libraryModule}');
-
- async () => {
- await wait();
- await waitFor(someCallback);
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { render,waitFor } = require('${libraryModule}');
-
- async () => {
- await waitFor(() => {});
- await waitFor(someCallback);
- }`,
- } as const)
- ),
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- // if already importing waitFor then it's not imported twice
- code: `import { wait, waitFor, render } from 'test-utils';
-
- async () => {
- await wait();
- await waitFor(someCallback);
- }`,
- errors: [
- {
- messageId: 'preferWaitForImport',
- line: 1,
- column: 1,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `import { render,waitFor } from 'test-utils';
-
- async () => {
- await waitFor(() => {});
- await waitFor(someCallback);
- }`,
- },
- {
- settings: {
- 'testing-library/utils-module': 'test-utils',
- },
- // if already importing waitFor then it's not imported twice
- code: `const { wait, waitFor, render } = require('test-utils');
-
- async () => {
- await wait();
- await waitFor(someCallback);
- }`,
- errors: [
- {
- messageId: 'preferWaitForRequire',
- line: 1,
- column: 7,
- },
- {
- messageId: 'preferWaitForMethod',
- line: 4,
- column: 15,
- },
- ],
- output: `const { render,waitFor } = require('test-utils');
-
- async () => {
- await waitFor(() => {});
- await waitFor(someCallback);
- }`,
- },
- ],
-});
diff --git a/tests/lib/rules/render-result-naming-convention.test.ts b/tests/lib/rules/render-result-naming-convention.test.ts
index 51b9e023..b2715300 100644
--- a/tests/lib/rules/render-result-naming-convention.test.ts
+++ b/tests/lib/rules/render-result-naming-convention.test.ts
@@ -1,126 +1,136 @@
import rule, {
- RULE_NAME,
+ RULE_NAME,
} from '../../../lib/rules/render-result-naming-convention';
import { createRuleTester } from '../test-utils';
const ruleTester = createRuleTester();
+const SUPPORTED_TESTING_FRAMEWORKS = [
+ '@testing-library/angular',
+ '@testing-library/react',
+ '@testing-library/vue',
+ '@marko/testing-library',
+];
+
ruleTester.run(RULE_NAME, rule, {
- valid: [
- {
- code: `
- import { render } from '@testing-library/react';
-
- test('should not report straight destructured render result', () => {
- const { rerender, getByText } = render(