Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Refactor tests #158

Merged
merged 3 commits into from
Jan 2, 2025
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
5 changes: 5 additions & 0 deletions .changeset/eighty-steaks-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@feature-sliced/steiger-plugin': patch
---

Refactor test cases to nest FSD roots
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ if (import.meta.vitest) {
const payload: CollectRelatedTsConfigsPayload = {
extended: [
{
tsconfigFile: resolve(joinFromRoot('tsconfig.json')),
tsconfigFile: resolve(joinFromRoot('user', 'projects', 'project-0', 'src', 'tsconfig.json')),
tsconfig: {
extends: './.nuxt/tsconfig.json',
},
},
{
tsconfigFile: resolve(joinFromRoot('.nuxt', 'tsconfig.json')),
tsconfigFile: resolve(joinFromRoot('user', 'projects', 'project-0', 'src', '.nuxt', 'tsconfig.json')),
tsconfig: {
compilerOptions: {
paths: {
Expand All @@ -173,7 +173,7 @@ if (import.meta.vitest) {
},
},
],
tsconfigFile: resolve(joinFromRoot('tsconfig.json')),
tsconfigFile: resolve(joinFromRoot('user', 'projects', 'project-0', 'src', 'tsconfig.json')),
tsconfig: {
extends: './.nuxt/tsconfig.json',
compilerOptions: {
Expand All @@ -196,8 +196,8 @@ if (import.meta.vitest) {
extends: './.nuxt/tsconfig.json',
compilerOptions: {
paths: {
'~': [resolve(joinFromRoot())],
'~/*': [resolve(joinFromRoot('*'))],
'~': [resolve(joinFromRoot('user', 'projects', 'project-0', 'src'))],
'~/*': [resolve(joinFromRoot('user', 'projects', 'project-0', 'src', '*'))],
},
strict: true,
noUncheckedIndexedAccess: false,
Expand Down
59 changes: 31 additions & 28 deletions packages/steiger-plugin-fsd/src/_lib/index-source-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export function indexSourceFiles(root: Folder): Record<string, SourceFile> {
if (import.meta.vitest) {
const { test, expect } = import.meta.vitest
test('indexSourceFiles', () => {
const root = parseIntoFsdRoot(`
const root = parseIntoFsdRoot(
`
📂 shared
📂 ui
📄 styles.ts
Expand All @@ -80,120 +81,122 @@ if (import.meta.vitest) {
📄 index.ts
📄 root.ts
📄 index.ts
`)
`,
joinFromRoot('users', 'user', 'project', 'src'),
)

expect(indexSourceFiles(root)).toEqual({
[joinFromRoot('features', 'comments', 'index.ts')]: {
[joinFromRoot('users', 'user', 'project', 'src', 'features', 'comments', 'index.ts')]: {
file: {
path: joinFromRoot('features', 'comments', 'index.ts'),
path: joinFromRoot('users', 'user', 'project', 'src', 'features', 'comments', 'index.ts'),
type: 'file',
},
layerName: 'features',
segmentName: null,
sliceName: 'comments',
},
[joinFromRoot('features', 'comments', 'ui', 'CommentCard.tsx')]: {
[joinFromRoot('users', 'user', 'project', 'src', 'features', 'comments', 'ui', 'CommentCard.tsx')]: {
file: {
path: joinFromRoot('features', 'comments', 'ui', 'CommentCard.tsx'),
path: joinFromRoot('users', 'user', 'project', 'src', 'features', 'comments', 'ui', 'CommentCard.tsx'),
type: 'file',
},
layerName: 'features',
segmentName: 'ui',
sliceName: 'comments',
},
[joinFromRoot('pages', 'editor', 'index.ts')]: {
[joinFromRoot('users', 'user', 'project', 'src', 'pages', 'editor', 'index.ts')]: {
file: {
path: joinFromRoot('pages', 'editor', 'index.ts'),
path: joinFromRoot('users', 'user', 'project', 'src', 'pages', 'editor', 'index.ts'),
type: 'file',
},
layerName: 'pages',
segmentName: null,
sliceName: 'editor',
},
[joinFromRoot('pages', 'editor', 'ui', 'Editor.tsx')]: {
[joinFromRoot('users', 'user', 'project', 'src', 'pages', 'editor', 'ui', 'Editor.tsx')]: {
file: {
path: joinFromRoot('pages', 'editor', 'ui', 'Editor.tsx'),
path: joinFromRoot('users', 'user', 'project', 'src', 'pages', 'editor', 'ui', 'Editor.tsx'),
type: 'file',
},
layerName: 'pages',
segmentName: 'ui',
sliceName: 'editor',
},
[joinFromRoot('pages', 'editor', 'ui', 'EditorPage.tsx')]: {
[joinFromRoot('users', 'user', 'project', 'src', 'pages', 'editor', 'ui', 'EditorPage.tsx')]: {
file: {
path: joinFromRoot('pages', 'editor', 'ui', 'EditorPage.tsx'),
path: joinFromRoot('users', 'user', 'project', 'src', 'pages', 'editor', 'ui', 'EditorPage.tsx'),
type: 'file',
},
layerName: 'pages',
segmentName: 'ui',
sliceName: 'editor',
},
[joinFromRoot('pages', 'editor', 'ui', 'styles.ts')]: {
[joinFromRoot('users', 'user', 'project', 'src', 'pages', 'editor', 'ui', 'styles.ts')]: {
file: {
path: joinFromRoot('pages', 'editor', 'ui', 'styles.ts'),
path: joinFromRoot('users', 'user', 'project', 'src', 'pages', 'editor', 'ui', 'styles.ts'),
type: 'file',
},
layerName: 'pages',
segmentName: 'ui',
sliceName: 'editor',
},
[joinFromRoot('shared', 'ui', 'Button.tsx')]: {
[joinFromRoot('users', 'user', 'project', 'src', 'shared', 'ui', 'Button.tsx')]: {
file: {
path: joinFromRoot('shared', 'ui', 'Button.tsx'),
path: joinFromRoot('users', 'user', 'project', 'src', 'shared', 'ui', 'Button.tsx'),
type: 'file',
},
layerName: 'shared',
segmentName: 'ui',
sliceName: null,
},
[joinFromRoot('shared', 'ui', 'TextField.tsx')]: {
[joinFromRoot('users', 'user', 'project', 'src', 'shared', 'ui', 'TextField.tsx')]: {
file: {
path: joinFromRoot('shared', 'ui', 'TextField.tsx'),
path: joinFromRoot('users', 'user', 'project', 'src', 'shared', 'ui', 'TextField.tsx'),
type: 'file',
},
layerName: 'shared',
segmentName: 'ui',
sliceName: null,
},
[joinFromRoot('shared', 'ui', 'index.ts')]: {
[joinFromRoot('users', 'user', 'project', 'src', 'shared', 'ui', 'index.ts')]: {
file: {
path: joinFromRoot('shared', 'ui', 'index.ts'),
path: joinFromRoot('users', 'user', 'project', 'src', 'shared', 'ui', 'index.ts'),
type: 'file',
},
layerName: 'shared',
segmentName: 'ui',
sliceName: null,
},
[joinFromRoot('shared', 'ui', 'styles.ts')]: {
[joinFromRoot('users', 'user', 'project', 'src', 'shared', 'ui', 'styles.ts')]: {
file: {
path: joinFromRoot('shared', 'ui', 'styles.ts'),
path: joinFromRoot('users', 'user', 'project', 'src', 'shared', 'ui', 'styles.ts'),
type: 'file',
},
layerName: 'shared',
segmentName: 'ui',
sliceName: null,
},
[joinFromRoot('app', 'ui', 'index.ts')]: {
[joinFromRoot('users', 'user', 'project', 'src', 'app', 'ui', 'index.ts')]: {
file: {
path: joinFromRoot('app', 'ui', 'index.ts'),
path: joinFromRoot('users', 'user', 'project', 'src', 'app', 'ui', 'index.ts'),
type: 'file',
},
layerName: 'app',
segmentName: 'ui',
sliceName: null,
},
[joinFromRoot('app', 'root.ts')]: {
[joinFromRoot('users', 'user', 'project', 'src', 'app', 'root.ts')]: {
file: {
path: joinFromRoot('app', 'root.ts'),
path: joinFromRoot('users', 'user', 'project', 'src', 'app', 'root.ts'),
type: 'file',
},
layerName: 'app',
segmentName: 'root',
sliceName: null,
},
[joinFromRoot('app', 'index.ts')]: {
[joinFromRoot('users', 'user', 'project', 'src', 'app', 'index.ts')]: {
file: {
path: joinFromRoot('app', 'index.ts'),
path: joinFromRoot('users', 'user', 'project', 'src', 'app', 'index.ts'),
type: 'file',
},
layerName: 'app',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import ambiguousSliceNames from './index.js'
import { joinFromRoot, parseIntoFolder as parseIntoFsdRoot } from '@steiger/toolkit'

it('reports no errors on a project without slice names that match some segment name in Shared', () => {
const root = parseIntoFsdRoot(`
const root = parseIntoFsdRoot(
`
📂 shared
📂 ui
📄 index.ts
Expand All @@ -20,13 +21,16 @@ it('reports no errors on a project without slice names that match some segment n
📂 home
📂 ui
📄 index.ts
`)
`,
joinFromRoot('users', 'user', 'project', 'src'),
)

expect(ambiguousSliceNames.check(root)).toEqual({ diagnostics: [] })
})

it('reports errors on a project with slice names that match some segment name in Shared', () => {
const root = parseIntoFsdRoot(`
const root = parseIntoFsdRoot(
`
📂 shared
📂 ui
📄 index.ts
Expand All @@ -45,19 +49,22 @@ it('reports errors on a project with slice names that match some segment name in
📂 home
📂 ui
📄 index.ts
`)
`,
joinFromRoot('users', 'user', 'project', 'src'),
)

const diagnostics = ambiguousSliceNames.check(root).diagnostics
expect(diagnostics).toEqual([
{
message: 'Slice "i18n" could be confused with a segment from Shared with the same name',
location: { path: joinFromRoot('features', 'i18n') },
location: { path: joinFromRoot('users', 'user', 'project', 'src', 'features', 'i18n') },
},
])
})

it('works for slice groups and grouped slices', () => {
const root = parseIntoFsdRoot(`
const root = parseIntoFsdRoot(
`
📂 shared
📂 ui
📄 index.ts
Expand All @@ -78,17 +85,19 @@ it('works for slice groups and grouped slices', () => {
📂 home
📂 ui
📄 index.ts
`)
`,
joinFromRoot('users', 'user', 'project', 'src'),
)

const diagnostics = ambiguousSliceNames.check(root).diagnostics
expect(diagnostics).toEqual([
{
message: 'Slice group "i18n" could be confused with a segment "i18n" from Shared',
location: { path: joinFromRoot('features', 'i18n') },
location: { path: joinFromRoot('users', 'user', 'project', 'src', 'features', 'i18n') },
},
{
message: `Slice "${join('test', 'store')}" could be confused with a segment "store" from Shared`,
location: { path: joinFromRoot('features', 'test', 'store') },
location: { path: joinFromRoot('users', 'user', 'project', 'src', 'features', 'test', 'store') },
},
])
})
25 changes: 17 additions & 8 deletions packages/steiger-plugin-fsd/src/excessive-slicing/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { joinFromRoot, parseIntoFolder as parseIntoFsdRoot } from '@steiger/tool
import excessiveSlicing from './index.js'

it('reports no errors on projects with moderate slicing', () => {
const root = parseIntoFsdRoot(`
const root = parseIntoFsdRoot(
`
📂 entities
📂 users
📂 ui
Expand All @@ -16,15 +17,18 @@ it('reports no errors on projects with moderate slicing', () => {
📂 ui
📄 index.ts
📄 Button.tsx
`)
`,
joinFromRoot('users', 'user', 'project', 'src'),
)

const { diagnostics } = excessiveSlicing.check(root)

expect(diagnostics).toEqual([])
})

it('reports errors on a project with an excessive amount of features', () => {
const root = parseIntoFsdRoot(`
const root = parseIntoFsdRoot(
`
📂 entities
📂 users
📂 ui
Expand Down Expand Up @@ -106,21 +110,24 @@ it('reports errors on a project with an excessive amount of features', () => {
📂 bitcoin
📂 ui
📄 index.ts
`)
`,
joinFromRoot('users', 'user', 'project', 'src'),
)

const { diagnostics } = excessiveSlicing.check(root)

expect(diagnostics).toEqual([
{
location: { path: joinFromRoot('features') },
location: { path: joinFromRoot('users', 'user', 'project', 'src', 'features') },
message:
'Layer "features" has 23 ungrouped slices, which is above the recommended threshold of 20. Consider grouping them or moving the code inside to the layer where it\'s used.',
},
])
})

it('works with slice groups', () => {
const root = parseIntoFsdRoot(`
const root = parseIntoFsdRoot(
`
📂 entities
📂 users
📂 ui
Expand Down Expand Up @@ -206,13 +213,15 @@ it('works with slice groups', () => {
📂 bitcoin
📂 ui
📄 index.ts
`)
`,
joinFromRoot('users', 'user', 'project', 'src'),
)

const { diagnostics } = excessiveSlicing.check(root)

expect(diagnostics).toEqual([
{
location: { path: joinFromRoot('features', 'junk') },
location: { path: joinFromRoot('users', 'user', 'project', 'src', 'features', 'junk') },
message:
'Slice group "junk" has 23 slices, which is above the recommended threshold of 20. Consider grouping them or moving the code inside to the layer where it\'s used.',
},
Expand Down
Loading
Loading