From da194524eedf71fbd586430925e0b375c1acb1cb Mon Sep 17 00:00:00 2001 From: Marco Pasqualetti <24919330+marcalexiei@users.noreply.github.com> Date: Sat, 8 Mar 2025 21:11:28 +0100 Subject: [PATCH] chore: add `@vitest/eslint-plugin` (#630) --- eslint.config.js | 32 +++++++++++++++++-- package.json | 1 + .../src/build-route-config.spec.ts | 2 +- .../src/has-parent-route.spec.ts | 6 ++-- .../src/sort-route-nodes.spec.ts | 3 +- .../tuono-router/src/components/Link.spec.tsx | 2 +- .../src/components/RouteMatch.spec.tsx | 4 +-- .../tuono-router/src/hooks/useRoute.spec.ts | 6 ++-- pnpm-lock.yaml | 24 ++++++++++++++ 9 files changed, 69 insertions(+), 11 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index da71a5e8..1cdc2443 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -5,9 +5,19 @@ import eslintPluginImport from 'eslint-plugin-import' import eslintPluginReact from 'eslint-plugin-react' // @ts-expect-error no types are available for this plugin import eslintPluginReactHooks from 'eslint-plugin-react-hooks' +import eslintPluginVitest from '@vitest/eslint-plugin' -const REACT_FILES_MATCH = - 'packages/{tuono,tuono-router}/**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}' +const JS_EXTENSIONS_MATCH = [ + 'js', + 'mjs', + 'cjs', + 'jsx', + 'mjsx', + 'ts', + 'tsx', + 'mtsx', +].join(',') +const REACT_FILES_MATCH = `packages/{tuono,tuono-router}/**/*.{${JS_EXTENSIONS_MATCH}}` /** @type import('typescript-eslint').ConfigArray */ const tuonoEslintConfig = tseslint.config( @@ -172,6 +182,24 @@ const tuonoEslintConfig = tseslint.config( // #endregion misc }, }, + + { + files: [`packages/**/*.spec.{${JS_EXTENSIONS_MATCH}}`], + plugins: { vitest: eslintPluginVitest }, + rules: { + ...eslintPluginVitest.configs.recommended.rules, + 'vitest/consistent-test-filename': [ + 'error', + { + pattern: '.*\\.spec\\.[tj]sx?$', + allTestPattern: '.*\\.(test|spec)\\.[tj]sx?$', + }, + ], + 'vitest/consistent-test-it': 'error', + 'vitest/max-expects': 'error', + 'vitest/max-nested-describe': ['error', { max: 3 }], + }, + }, ) export default tuonoEslintConfig diff --git a/package.json b/package.json index 6d01bff4..eba35856 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@eslint/js": "9.20.0", "@playwright/test": "1.50.1", "@types/node": "22.13.9", + "@vitest/eslint-plugin": "1.1.36", "eslint": "9.20.0", "eslint-import-resolver-typescript": "3.7.0", "eslint-plugin-import": "2.31.0", diff --git a/packages/tuono-fs-router-vite-plugin/src/build-route-config.spec.ts b/packages/tuono-fs-router-vite-plugin/src/build-route-config.spec.ts index 9b50a422..648a72f8 100644 --- a/packages/tuono-fs-router-vite-plugin/src/build-route-config.spec.ts +++ b/packages/tuono-fs-router-vite-plugin/src/build-route-config.spec.ts @@ -63,7 +63,7 @@ const routes = [ ] describe('buildRouteConfig works', () => { - it('Should build the correct config', () => { + it('should build the correct config', () => { const config = buildRouteConfig(routes) expect(config).toStrictEqual( 'PostsMyPostRoute,PostsIndexRoute,PostspostIndexRoute', diff --git a/packages/tuono-fs-router-vite-plugin/src/has-parent-route.spec.ts b/packages/tuono-fs-router-vite-plugin/src/has-parent-route.spec.ts index e8831e79..53cc470a 100644 --- a/packages/tuono-fs-router-vite-plugin/src/has-parent-route.spec.ts +++ b/packages/tuono-fs-router-vite-plugin/src/has-parent-route.spec.ts @@ -72,13 +72,15 @@ const dynamicRoute = { } describe('hasParentRoute works', () => { - it('Should detect parent route', () => { + it('should detect parent route', () => { const parentRoute = hasParentRoute(routes, myPost, myPost.path) + expect(parentRoute).toStrictEqual(parent) }) - it('Should detect parent route for dynamic routes', () => { + it('should detect parent route for dynamic routes', () => { const parentRoute = hasParentRoute(routes, dynamicRoute, dynamicRoute.path) + expect(parentRoute).toStrictEqual(parent) }) }) diff --git a/packages/tuono-fs-router-vite-plugin/src/sort-route-nodes.spec.ts b/packages/tuono-fs-router-vite-plugin/src/sort-route-nodes.spec.ts index 8e194220..8d97ca03 100644 --- a/packages/tuono-fs-router-vite-plugin/src/sort-route-nodes.spec.ts +++ b/packages/tuono-fs-router-vite-plugin/src/sort-route-nodes.spec.ts @@ -108,8 +108,9 @@ const expectedSorting = [ ] describe('sortRouteNodes works', () => { - it('Should correctly sort the nodes', () => { + it('should correctly sort the nodes', () => { const sorted = sortRouteNodes(routes) + expect(sorted).toStrictEqual(expectedSorting) }) }) diff --git a/packages/tuono-router/src/components/Link.spec.tsx b/packages/tuono-router/src/components/Link.spec.tsx index f9ae4cb4..5898d17e 100644 --- a/packages/tuono-router/src/components/Link.spec.tsx +++ b/packages/tuono-router/src/components/Link.spec.tsx @@ -33,7 +33,7 @@ vi.mock('react-intersection-observer', () => ({ }, })) -describe('Link Component', () => { +describe('Link component', () => { beforeEach(() => { pushMock.mockReset() preloadMock.mockReset() diff --git a/packages/tuono-router/src/components/RouteMatch.spec.tsx b/packages/tuono-router/src/components/RouteMatch.spec.tsx index 516a88df..ec09869b 100644 --- a/packages/tuono-router/src/components/RouteMatch.spec.tsx +++ b/packages/tuono-router/src/components/RouteMatch.spec.tsx @@ -1,5 +1,5 @@ import type { JSX } from 'react' -import { afterEach, describe, expect, test, vi } from 'vitest' +import { afterEach, describe, expect, it, vi } from 'vitest' import { cleanup, render, screen } from '@testing-library/react' @@ -51,7 +51,7 @@ vi.mock('../hooks/useServerPayloadData.ts', () => ({ describe('Test RouteMatch component', () => { afterEach(cleanup) - test('It should correctly render nested routes', () => { + it('should correctly render nested routes', () => { render() expect(screen.getByTestId('root')).toMatchInlineSnapshot( diff --git a/packages/tuono-router/src/hooks/useRoute.spec.ts b/packages/tuono-router/src/hooks/useRoute.spec.ts index bdbf764d..f5f3679e 100644 --- a/packages/tuono-router/src/hooks/useRoute.spec.ts +++ b/packages/tuono-router/src/hooks/useRoute.spec.ts @@ -1,4 +1,4 @@ -import { afterEach, describe, expect, test, vi } from 'vitest' +import { afterEach, describe, expect, it, vi } from 'vitest' import { cleanup } from '@testing-library/react' import { useRoute } from './useRoute' @@ -21,7 +21,7 @@ describe('useRoute', () => { useRouterContextMock.mockReset() }) - test('match routes by ids', () => { + it('should match routes by ids', () => { useRouterContextMock.mockReturnValue({ router: { routesById: { @@ -36,6 +36,7 @@ describe('useRoute', () => { }, }) + /* eslint-disable vitest/max-expects */ expect(useRoute('/')?.id).toBe('/') expect(useRoute('/not-found')?.id).toBe(undefined) expect(useRoute('/about')?.id).toBe('/about') @@ -50,5 +51,6 @@ describe('useRoute', () => { expect(useRoute('/blog/catch_all/catch_all')?.id).toBe( '/blog/[...catch_all]', ) + /* eslint-enable vitest/max-expects */ }) }) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 17808653..e246d02b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,6 +17,9 @@ importers: '@types/node': specifier: 22.13.9 version: 22.13.9 + '@vitest/eslint-plugin': + specifier: 1.1.36 + version: 1.1.36(@typescript-eslint/utils@8.24.0(eslint@9.20.0(jiti@2.4.2))(typescript@5.7.3))(eslint@9.20.0(jiti@2.4.2))(typescript@5.7.3)(vitest@3.0.7(@types/debug@4.1.12)(@types/node@22.13.9)(happy-dom@17.2.2)(jiti@2.4.2)(lightningcss@1.29.1)(sugarss@4.0.1(postcss@8.5.3))) eslint: specifier: 9.20.0 version: 9.20.0(jiti@2.4.2) @@ -1143,6 +1146,19 @@ packages: peerDependencies: vite: ^4 || ^5 || ^6 + '@vitest/eslint-plugin@1.1.36': + resolution: {integrity: sha512-IjBV/fcL9NJRxGw221ieaDsqKqj8qUo7rvSupDxMjTXyhsCusHC6M+jFUNqBp4PCkYFcf5bjrKxeZoCEWoPxig==} + peerDependencies: + '@typescript-eslint/utils': ^8.24.0 + eslint: '>= 8.57.0' + typescript: '>= 5.0.0' + vitest: '*' + peerDependenciesMeta: + typescript: + optional: true + vitest: + optional: true + '@vitest/expect@3.0.7': resolution: {integrity: sha512-QP25f+YJhzPfHrHfYHtvRn+uvkCFCqFtW9CktfBxmB+25QqWsx7VB2As6f4GmwllHLDhXNHvqedwhvMmSnNmjw==} @@ -3594,6 +3610,14 @@ snapshots: transitivePeerDependencies: - '@swc/helpers' + '@vitest/eslint-plugin@1.1.36(@typescript-eslint/utils@8.24.0(eslint@9.20.0(jiti@2.4.2))(typescript@5.7.3))(eslint@9.20.0(jiti@2.4.2))(typescript@5.7.3)(vitest@3.0.7(@types/debug@4.1.12)(@types/node@22.13.9)(happy-dom@17.2.2)(jiti@2.4.2)(lightningcss@1.29.1)(sugarss@4.0.1(postcss@8.5.3)))': + dependencies: + '@typescript-eslint/utils': 8.24.0(eslint@9.20.0(jiti@2.4.2))(typescript@5.7.3) + eslint: 9.20.0(jiti@2.4.2) + optionalDependencies: + typescript: 5.7.3 + vitest: 3.0.7(@types/debug@4.1.12)(@types/node@22.13.9)(happy-dom@17.2.2)(jiti@2.4.2)(lightningcss@1.29.1)(sugarss@4.0.1(postcss@8.5.3)) + '@vitest/expect@3.0.7': dependencies: '@vitest/spy': 3.0.7