-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nx-dev): conformance rule for blog post description
- Loading branch information
Showing
6 changed files
with
277 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
148 changes: 148 additions & 0 deletions
148
tools/workspace-plugin/src/conformance-rules/blog-description/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { vol } from 'memfs'; | ||
import { workspaceRoot } from '@nx/devkit'; | ||
import { join } from 'node:path'; | ||
import blogDescriptionRule from './index'; | ||
|
||
jest.mock('fs', () => require('memfs').fs); | ||
jest.mock('@nx/devkit', () => ({ | ||
workspaceRoot: '/root', | ||
})); | ||
|
||
describe('blog-description', () => { | ||
beforeEach(() => { | ||
vol.reset(); | ||
}); | ||
|
||
it('should pass when blog posts have descriptions', async () => { | ||
vol.fromJSON( | ||
{ | ||
'docs/blog/post1.md': `--- | ||
title: Post 1 | ||
description: This is post 1 | ||
--- | ||
Content`, | ||
'docs/blog/post2.md': `--- | ||
title: Post 2 | ||
description: This is post 2 | ||
--- | ||
Content`, | ||
}, | ||
workspaceRoot | ||
); | ||
|
||
const result = await blogDescriptionRule.implementation({ | ||
projectGraph: { | ||
nodes: { | ||
blog: { | ||
name: 'blog', | ||
type: 'lib', | ||
data: { | ||
root: 'docs/blog', | ||
}, | ||
}, | ||
}, | ||
dependencies: {}, | ||
}, | ||
ruleOptions: {}, | ||
}); | ||
|
||
expect(result.details.violations).toHaveLength(0); | ||
}); | ||
|
||
it('should report violations for blog posts without descriptions', async () => { | ||
vol.fromJSON( | ||
{ | ||
'docs/blog/post1.md': `--- | ||
title: Post 1 | ||
--- | ||
Content`, | ||
'docs/blog/post2.md': `--- | ||
title: Post 2 | ||
description: This is post 2 | ||
--- | ||
Content`, | ||
}, | ||
workspaceRoot | ||
); | ||
|
||
const result = await blogDescriptionRule.implementation({ | ||
projectGraph: { | ||
nodes: { | ||
blog: { | ||
name: 'blog', | ||
type: 'lib', | ||
data: { | ||
root: 'docs/blog', | ||
}, | ||
}, | ||
}, | ||
dependencies: {}, | ||
}, | ||
ruleOptions: {}, | ||
}); | ||
|
||
expect(result.details.violations).toHaveLength(1); | ||
expect(result.details.violations[0]).toEqual({ | ||
message: 'Blog posts with frontmatter must have a description field', | ||
sourceProject: 'blog', | ||
file: join(workspaceRoot, 'docs/blog/post1.md'), | ||
}); | ||
}); | ||
|
||
it('should ignore files without frontmatter', async () => { | ||
vol.fromJSON( | ||
{ | ||
'docs/blog/post1.md': '# Just a regular markdown file', | ||
}, | ||
workspaceRoot | ||
); | ||
|
||
const result = await blogDescriptionRule.implementation({ | ||
projectGraph: { | ||
nodes: { | ||
blog: { | ||
name: 'blog', | ||
type: 'lib', | ||
data: { | ||
root: 'docs/blog', | ||
}, | ||
}, | ||
}, | ||
dependencies: {}, | ||
}, | ||
ruleOptions: {}, | ||
}); | ||
|
||
expect(result.details.violations).toHaveLength(0); | ||
}); | ||
|
||
it('should ignore files with invalid YAML', async () => { | ||
vol.fromJSON( | ||
{ | ||
'docs/blog/post1.md': `--- | ||
invalid: yaml: : | ||
--- | ||
Content`, | ||
}, | ||
workspaceRoot | ||
); | ||
|
||
const result = await blogDescriptionRule.implementation({ | ||
projectGraph: { | ||
nodes: { | ||
blog: { | ||
name: 'blog', | ||
type: 'lib', | ||
data: { | ||
root: 'docs/blog', | ||
}, | ||
}, | ||
}, | ||
dependencies: {}, | ||
}, | ||
ruleOptions: {}, | ||
}); | ||
|
||
expect(result.details.violations).toHaveLength(0); | ||
}); | ||
}); |
Oops, something went wrong.