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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @ts-check

import js from '@eslint/js';
import vitest from '@vitest/eslint-plugin';
import { defineConfig, globalIgnores } from 'eslint/config';
import prettierConfig from 'eslint-config-prettier/flat';
import { importX } from 'eslint-plugin-import-x';
import jest from 'eslint-plugin-jest';
import globals from 'globals';
import tseslint from 'typescript-eslint';

Expand Down Expand Up @@ -88,12 +88,12 @@ const config = defineConfig(
},
},
{
name: 'Jest config',
name: 'Vitest config',
files: ['**/*.test.ts', '**/*.test.js'],
...jest.configs['flat/recommended'],
...vitest.configs.recommended,
rules: {
...jest.configs['flat/recommended'].rules,
'jest/padding-around-all': 'error',
...vitest.configs.recommended.rules,
'vitest/padding-around-all': 'warn',
},
},
{
Expand Down
14 changes: 0 additions & 14 deletions jest.config.js

This file was deleted.

4 changes: 3 additions & 1 deletion lib/configs/angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// DO NOT EDIT THIS CODE BY HAND
// YOU CAN REGENERATE IT USING pnpm run generate:configs

import type { Linter } from 'eslint';

export = {
plugins: ['testing-library'],
rules: {
Expand Down Expand Up @@ -32,4 +34,4 @@ export = {
'testing-library/prefer-screen-queries': 'error',
'testing-library/render-result-naming-convention': 'error',
},
};
} satisfies Linter.LegacyConfig;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this for type validation purposes.

4 changes: 3 additions & 1 deletion lib/configs/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// DO NOT EDIT THIS CODE BY HAND
// YOU CAN REGENERATE IT USING pnpm run generate:configs

import type { Linter } from 'eslint';

export = {
plugins: ['testing-library'],
rules: {
Expand All @@ -27,4 +29,4 @@ export = {
'testing-library/prefer-query-by-disappearance': 'error',
'testing-library/prefer-screen-queries': 'error',
},
};
} satisfies Linter.LegacyConfig;
31 changes: 16 additions & 15 deletions lib/configs/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { join } from 'path';

import { importDefault, SUPPORTED_TESTING_FRAMEWORKS } from '../utils';
import angular from './angular';
import dom from './dom';
import marko from './marko';
import react from './react';
import svelte from './svelte';
import vue from './vue';

import type { SupportedTestingFramework } from '../utils';
import type { TSESLint } from '@typescript-eslint/utils';

const configsDir = __dirname;
import type { Linter } from 'eslint';

const getConfigForFramework = (framework: SupportedTestingFramework) =>
importDefault<TSESLint.SharedConfig.RulesRecord>(join(configsDir, framework));
const legacyConfigs: Record<SupportedTestingFramework, Linter.LegacyConfig> = {
dom,
angular,
react,
vue,
svelte,
marko,
};

export default SUPPORTED_TESTING_FRAMEWORKS.reduce(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove dynamic generation in favor of static generation.

(allConfigs, framework) => ({
...allConfigs,
[framework]: getConfigForFramework(framework),
}),
{}
) as Record<SupportedTestingFramework, TSESLint.SharedConfig.RulesRecord>;
export { legacyConfigs };
4 changes: 3 additions & 1 deletion lib/configs/marko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// DO NOT EDIT THIS CODE BY HAND
// YOU CAN REGENERATE IT USING pnpm run generate:configs

import type { Linter } from 'eslint';

export = {
plugins: ['testing-library'],
rules: {
Expand Down Expand Up @@ -29,4 +31,4 @@ export = {
'testing-library/prefer-screen-queries': 'error',
'testing-library/render-result-naming-convention': 'error',
},
};
} satisfies Linter.LegacyConfig;
4 changes: 3 additions & 1 deletion lib/configs/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// DO NOT EDIT THIS CODE BY HAND
// YOU CAN REGENERATE IT USING pnpm run generate:configs

import type { Linter } from 'eslint';

export = {
plugins: ['testing-library'],
rules: {
Expand Down Expand Up @@ -34,4 +36,4 @@ export = {
'testing-library/prefer-screen-queries': 'error',
'testing-library/render-result-naming-convention': 'error',
},
};
} satisfies Linter.LegacyConfig;
4 changes: 3 additions & 1 deletion lib/configs/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// DO NOT EDIT THIS CODE BY HAND
// YOU CAN REGENERATE IT USING pnpm run generate:configs

import type { Linter } from 'eslint';

export = {
plugins: ['testing-library'],
rules: {
Expand Down Expand Up @@ -29,4 +31,4 @@ export = {
'testing-library/prefer-screen-queries': 'error',
'testing-library/render-result-naming-convention': 'error',
},
};
} satisfies Linter.LegacyConfig;
4 changes: 3 additions & 1 deletion lib/configs/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// DO NOT EDIT THIS CODE BY HAND
// YOU CAN REGENERATE IT USING pnpm run generate:configs

import type { Linter } from 'eslint';

export = {
plugins: ['testing-library'],
rules: {
Expand Down Expand Up @@ -29,4 +31,4 @@ export = {
'testing-library/prefer-screen-queries': 'error',
'testing-library/render-result-naming-convention': 'error',
},
};
} satisfies Linter.LegacyConfig;
56 changes: 34 additions & 22 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,56 @@
import configs from './configs';
import { legacyConfigs } from './configs';
import rules from './rules';

import type { SupportedTestingFramework } from './utils';
import type { TSESLint } from '@typescript-eslint/utils';

// we can't natively import package.json as tsc will copy it into dist/
const {
name: packageName,
version: packageVersion,
// we can't natively import package.json as tsc will copy it into dist/
// eslint-disable-next-line @typescript-eslint/no-require-imports
} = require('../package.json') as { name: string; version: string };

type FinalConfigs = Record<
SupportedTestingFramework | `flat/${SupportedTestingFramework}`,
TSESLint.ClassicConfig.Config | TSESLint.FlatConfig.Config
>;

const plugin = {
meta: {
name: packageName,
version: packageVersion,
},
// ugly cast for now to keep TypeScript happy since
// we don't have types for flat config yet
configs: {} as Record<
SupportedTestingFramework | `flat/${SupportedTestingFramework}`,
TSESLint.SharedConfig.RulesRecord
>,
configs: {} as FinalConfigs,
rules,
};

plugin.configs = {
...configs,
...(Object.fromEntries(
Object.entries(configs).map(([framework, config]) => [
`flat/${framework}`,
{
plugins: { 'testing-library': plugin },
rules: config.rules,
},
])
) as unknown as Record<
`flat/${SupportedTestingFramework}`,
TSESLint.SharedConfig.RulesRecord & { plugins: unknown }
>),
};
...legacyConfigs,
'flat/dom': {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove dynamic generation in favor of static generation.

plugins: { 'testing-library': plugin },
rules: legacyConfigs.dom.rules,
},
'flat/angular': {
plugins: { 'testing-library': plugin },
rules: legacyConfigs.angular.rules,
},
'flat/react': {
plugins: { 'testing-library': plugin },
rules: legacyConfigs.react.rules,
},
'flat/vue': {
plugins: { 'testing-library': plugin },
rules: legacyConfigs.vue.rules,
},
'flat/svelte': {
plugins: { 'testing-library': plugin },
rules: legacyConfigs.svelte.rules,
},
'flat/marko': {
plugins: { 'testing-library': plugin },
rules: legacyConfigs.marko.rules,
},
} satisfies FinalConfigs;

export = plugin;
79 changes: 58 additions & 21 deletions lib/rules/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,59 @@
import { readdirSync } from 'fs';
import { join, parse } from 'path';
import awaitAsyncEvents from './await-async-events';
import awaitAsyncQueries from './await-async-queries';
import awaitAsyncUtils from './await-async-utils';
import consistentDataTestid from './consistent-data-testid';
import noAwaitSyncEvents from './no-await-sync-events';
import noAwaitSyncQueries from './no-await-sync-queries';
import noContainer from './no-container';
import noDebuggingUtils from './no-debugging-utils';
import noDomImport from './no-dom-import';
import noGlobalRegexpFlagInQuery from './no-global-regexp-flag-in-query';
import noManualCleanup from './no-manual-cleanup';
import noNodeAccess from './no-node-access';
import noPromiseInFireEvent from './no-promise-in-fire-event';
import noRenderInLifecycle from './no-render-in-lifecycle';
import noTestIdQueries from './no-test-id-queries';
import noUnnecessaryAct from './no-unnecessary-act';
import noWaitForMultipleAssertions from './no-wait-for-multiple-assertions';
import noWaitForSideEffects from './no-wait-for-side-effects';
import noWaitForSnapshot from './no-wait-for-snapshot';
import preferExplicitAssert from './prefer-explicit-assert';
import preferFindBy from './prefer-find-by';
import preferImplicitAssert from './prefer-implicit-assert';
import preferPresenceQueries from './prefer-presence-queries';
import preferQueryByDisappearance from './prefer-query-by-disappearance';
import preferQueryMatchers from './prefer-query-matchers';
import preferScreenQueries from './prefer-screen-queries';
import preferUserEvent from './prefer-user-event';
import renderResultNamingConvention from './render-result-naming-convention';

import { importDefault } from '../utils';

import type { TestingLibraryPluginRuleModule } from '../utils';

const rulesDir = __dirname;
const excludedFiles = ['index'];

export default readdirSync(rulesDir)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove dynamic generation in favor of static generation.

.map((rule) => parse(rule).name)
.filter((ruleName) => !excludedFiles.includes(ruleName))
.reduce<Record<string, TestingLibraryPluginRuleModule<string, unknown[]>>>(
(allRules, ruleName) => ({
...allRules,
[ruleName]: importDefault<
TestingLibraryPluginRuleModule<string, unknown[]>
>(join(rulesDir, ruleName)),
}),
{}
);
export default {
'await-async-events': awaitAsyncEvents,
'await-async-queries': awaitAsyncQueries,
'await-async-utils': awaitAsyncUtils,
'consistent-data-testid': consistentDataTestid,
'no-await-sync-events': noAwaitSyncEvents,
'no-await-sync-queries': noAwaitSyncQueries,
'no-container': noContainer,
'no-debugging-utils': noDebuggingUtils,
'no-dom-import': noDomImport,
'no-global-regexp-flag-in-query': noGlobalRegexpFlagInQuery,
'no-manual-cleanup': noManualCleanup,
'no-node-access': noNodeAccess,
'no-promise-in-fire-event': noPromiseInFireEvent,
'no-render-in-lifecycle': noRenderInLifecycle,
'no-test-id-queries': noTestIdQueries,
'no-unnecessary-act': noUnnecessaryAct,
'no-wait-for-multiple-assertions': noWaitForMultipleAssertions,
'no-wait-for-side-effects': noWaitForSideEffects,
'no-wait-for-snapshot': noWaitForSnapshot,
'prefer-explicit-assert': preferExplicitAssert,
'prefer-find-by': preferFindBy,
'prefer-implicit-assert': preferImplicitAssert,
'prefer-presence-queries': preferPresenceQueries,
'prefer-query-by-disappearance': preferQueryByDisappearance,
'prefer-query-matchers': preferQueryMatchers,
'prefer-screen-queries': preferScreenQueries,
'prefer-user-event': preferUserEvent,
'render-result-naming-convention': renderResultNamingConvention,
};
9 changes: 0 additions & 9 deletions lib/utils/file-import.ts

This file was deleted.

1 change: 0 additions & 1 deletion lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './compat';
export * from './file-import';
export * from './types';
export * from './resolve-to-testing-library-fn';

Expand Down
16 changes: 7 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
"prettier-base": "prettier . --ignore-unknown --cache --log-level warn",
"rule-doc-generator": "eslint-doc-generator",
"semantic-release": "semantic-release",
"test": "jest",
"test:ci": "pnpm run test --ci --coverage",
"test:watch": "pnpm run test --watch",
"test": "vitest run",
"test:ci": "vitest run --coverage",
"test:watch": "vitest",
"type-check": "tsc --noEmit"
},
"dependencies": {
Expand All @@ -62,32 +62,30 @@
"@eslint/compat": "^1.3.2",
"@eslint/eslintrc": "^3.3.1",
"@eslint/js": "^9.35.0",
"@swc/core": "^1.9.3",
"@swc/jest": "^0.2.37",
"@types/jest": "^29.5.14",
"@types/node": "^22.9.3",
"@typescript-eslint/rule-tester": "^8.15.0",
"@vitest/coverage-v8": "^3.2.4",
"@vitest/eslint-plugin": "^1.5.2",
"del-cli": "^6.0.0",
"eslint": "^9.35.0",
"eslint-config-prettier": "^10.1.8",
"eslint-doc-generator": "^2.2.2",
"eslint-import-resolver-typescript": "^4.4.4",
"eslint-plugin-import-x": "^4.16.1",
"eslint-plugin-jest": "^29.0.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^7.1.0",
"eslint-remote-tester": "^3.0.1",
"eslint-remote-tester-repositories": "^1.0.1",
"globals": "^16.3.0",
"husky": "^9.1.7",
"jest": "^29.7.0",
"lint-staged": "^15.2.10",
"prettier": "3.6.2",
"semantic-release": "^25.0.2",
"semver": "^7.6.3",
"ts-node": "^10.9.2",
"typescript": "^5.7.2",
"typescript-eslint": "^8.15.0"
"typescript-eslint": "^8.15.0",
"vitest": "^3.2.4"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't install vitest v4 since is not compatible with node v18.

},
"peerDependencies": {
"eslint": "^8.57.0 || ^9.0.0"
Expand Down
Loading