|
| 1 | +import { describe, it, beforeEach, expect } from 'vitest' |
| 2 | +import { RuleTester } from 'eslint' |
| 3 | +import plugin from '../src/index.js' |
| 4 | + |
| 5 | +describe('Test files handling', () => { |
| 6 | + let ruleTester |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + ruleTester = new RuleTester({ |
| 10 | + languageOptions: { |
| 11 | + ecmaVersion: 2022, |
| 12 | + sourceType: 'module', |
| 13 | + }, |
| 14 | + plugins: { |
| 15 | + 'vue-modular': plugin, |
| 16 | + }, |
| 17 | + }) |
| 18 | + |
| 19 | + if (global.__eslintVueModularState) delete global.__eslintVueModularState |
| 20 | + }) |
| 21 | + |
| 22 | + describe('enforce-import-boundaries', () => { |
| 23 | + it('should allow test files to import from anywhere', () => { |
| 24 | + ruleTester.run('enforce-import-boundaries', plugin.rules['enforce-import-boundaries'], { |
| 25 | + valid: [ |
| 26 | + // ✅ Test files in tests/ directory can import module internals |
| 27 | + { |
| 28 | + code: "import userService from '@/modules/user/services/userService'", |
| 29 | + filename: '/project/tests/user.test.js', |
| 30 | + }, |
| 31 | + { |
| 32 | + code: "import { UserComponent } from '@/modules/user/components/UserProfile'", |
| 33 | + filename: '/project/tests/unit/user-module.spec.js', |
| 34 | + }, |
| 35 | + // ✅ Test files with .test. pattern can import feature internals |
| 36 | + { |
| 37 | + code: "import loginValidator from '@/features/auth/validators/loginValidator'", |
| 38 | + filename: '/project/src/features/auth/login.test.js', |
| 39 | + }, |
| 40 | + { |
| 41 | + code: "import { searchHelpers } from '@/features/search/utils/helpers'", |
| 42 | + filename: '/project/src/utils/search.spec.ts', |
| 43 | + }, |
| 44 | + // ✅ Test files with .spec. pattern can import app internals |
| 45 | + { |
| 46 | + code: "import appConfig from '@/app/config/settings'", |
| 47 | + filename: '/project/src/app/app.spec.js', |
| 48 | + }, |
| 49 | + // ✅ Test files in __tests__ directory can import anything |
| 50 | + { |
| 51 | + code: "import { StoreModule } from '@/stores/user'", |
| 52 | + filename: '/project/src/modules/auth/__tests__/auth-store.test.js', |
| 53 | + }, |
| 54 | + { |
| 55 | + code: "import componentHelpers from '@/components/Button/helpers'", |
| 56 | + filename: '/project/src/__tests__/components.spec.js', |
| 57 | + }, |
| 58 | + // ✅ Top-level tests folder (sibling to src) can import anything |
| 59 | + { |
| 60 | + code: "import { getAllModules } from '@/modules/admin/internal/utils'", |
| 61 | + filename: '/project/tests/integration/admin.test.js', |
| 62 | + }, |
| 63 | + ], |
| 64 | + invalid: [], |
| 65 | + }) |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + describe('no-cross-module-imports', () => { |
| 70 | + it('should allow test files to import across modules', () => { |
| 71 | + ruleTester.run('no-cross-module-imports', plugin.rules['no-cross-module-imports'], { |
| 72 | + valid: [ |
| 73 | + // ✅ Test files can import deep into other modules |
| 74 | + { |
| 75 | + code: "import userService from '@/modules/user/services/userService'", |
| 76 | + filename: '/project/tests/admin.test.js', |
| 77 | + }, |
| 78 | + { |
| 79 | + code: "import { AdminComponent } from '../../modules/admin/components/AdminPanel'", |
| 80 | + filename: '/project/src/modules/user/user.spec.js', |
| 81 | + }, |
| 82 | + // ✅ Files with test patterns can cross module boundaries |
| 83 | + { |
| 84 | + code: "import billingUtils from '@/modules/billing/utils/calculator'", |
| 85 | + filename: '/project/src/modules/user/billing.test.ts', |
| 86 | + }, |
| 87 | + ], |
| 88 | + invalid: [], |
| 89 | + }) |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + describe('no-cross-feature-imports', () => { |
| 94 | + it('should allow test files to import across features', () => { |
| 95 | + ruleTester.run('no-cross-feature-imports', plugin.rules['no-cross-feature-imports'], { |
| 96 | + valid: [ |
| 97 | + // ✅ Test files can import deep into other features |
| 98 | + { |
| 99 | + code: "import authValidator from '@/features/auth/validators/loginValidator'", |
| 100 | + filename: '/project/tests/search.test.js', |
| 101 | + }, |
| 102 | + { |
| 103 | + code: "import { SearchComponent } from '../../features/search/components/SearchBar'", |
| 104 | + filename: '/project/src/features/auth/auth.spec.js', |
| 105 | + }, |
| 106 | + // ✅ Files with test patterns can cross feature boundaries |
| 107 | + { |
| 108 | + code: "import notificationHelpers from '@/features/notifications/utils/helpers'", |
| 109 | + filename: '/project/src/features/user/notifications.test.ts', |
| 110 | + }, |
| 111 | + ], |
| 112 | + invalid: [], |
| 113 | + }) |
| 114 | + }) |
| 115 | + }) |
| 116 | + |
| 117 | + describe('isTestFile utility', () => { |
| 118 | + it('should correctly identify test files', () => { |
| 119 | + const { isTestFile } = plugin.utils || {} |
| 120 | + |
| 121 | + if (isTestFile) { |
| 122 | + // Files in tests/ directory |
| 123 | + expect(isTestFile('/project/tests/user.js')).toBe(true) |
| 124 | + expect(isTestFile('/project/tests/unit/auth.js')).toBe(true) |
| 125 | + expect(isTestFile('/project/src/tests/helpers.js')).toBe(true) |
| 126 | + |
| 127 | + // Files with .test. pattern |
| 128 | + expect(isTestFile('/project/src/components/Button.test.js')).toBe(true) |
| 129 | + expect(isTestFile('/project/src/utils/helpers.test.ts')).toBe(true) |
| 130 | + |
| 131 | + // Files with .spec. pattern |
| 132 | + expect(isTestFile('/project/src/components/Button.spec.js')).toBe(true) |
| 133 | + expect(isTestFile('/project/src/utils/helpers.spec.ts')).toBe(true) |
| 134 | + |
| 135 | + // Files in __tests__ directory |
| 136 | + expect(isTestFile('/project/src/__tests__/setup.js')).toBe(true) |
| 137 | + expect(isTestFile('/project/src/components/__tests__/Button.js')).toBe(true) |
| 138 | + |
| 139 | + // Non-test files |
| 140 | + expect(isTestFile('/project/src/components/Button.js')).toBe(false) |
| 141 | + expect(isTestFile('/project/src/utils/helpers.js')).toBe(false) |
| 142 | + expect(isTestFile('/project/src/stores/user.js')).toBe(false) |
| 143 | + expect(isTestFile('/project/src/modules/auth/index.js')).toBe(false) |
| 144 | + } |
| 145 | + }) |
| 146 | + }) |
| 147 | +}) |
0 commit comments