-
-
Notifications
You must be signed in to change notification settings - Fork 696
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
feat: add tests for check markdown script #3378
Changes from 4 commits
5a90f49
5373c9f
a30cef3
a9da240
1079101
86201ed
7c44f4a
c98ecdd
6e34205
4445ce5
333e0b2
8849351
1fbac7c
fada5ab
68cd3ee
998af5a
f5c8964
d92b519
d0fe33a
216a511
8a5235b
a93b45d
2732f0f
4fcdea8
e1a16c1
7401e7f
0fbdc06
02e221b
b17b83e
35d2e84
7921604
9d75691
b36972d
dff6ccc
53c86fd
92cc885
871e305
c0afa97
10c1ba5
2fa09e6
aa0e241
746dc88
4558748
29cd78c
e50392c
7a4b871
d993379
8d9221b
f595339
1cb57f0
4ee6c48
04127eb
bcaf621
dd802c4
1a46eda
3a9916c
cc23800
486b4af
0a91465
17dab5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,110 @@ | ||||
const fs = require('fs'); | ||||
const path = require('path'); | ||||
const os = require('os'); | ||||
const { | ||||
validateBlogs, | ||||
validateDocs, | ||||
checkMarkdownFiles | ||||
} = require('../../scripts/markdown/check-markdown'); | ||||
|
||||
describe('Frontmatter Validator', () => { | ||||
let tempDir; | ||||
let mockConsoleError; | ||||
|
||||
beforeEach(done => { | ||||
mockConsoleError = jest.spyOn(console, 'error').mockImplementation(); | ||||
fs.mkdtemp(path.join(os.tmpdir(), 'test-config'), (err, directory) => { | ||||
if (err) throw err; | ||||
tempDir = directory; | ||||
done(); | ||||
}); | ||||
}); | ||||
|
||||
afterEach(done => { | ||||
mockConsoleError.mockRestore(); | ||||
fs.rm(tempDir, { recursive: true, force: true }, done); | ||||
}); | ||||
|
||||
it('validates authors array and returns specific errors', () => { | ||||
const frontmatter = { | ||||
title: 'Test Blog', | ||||
date: '2024-01-01', | ||||
type: 'blog', | ||||
tags: ['test'], | ||||
cover: 'cover.jpg', | ||||
authors: [{ name: 'John' }, { photo: 'jane.jpg' }, { name: 'Bob', photo: 'bob.jpg', link: 'not-a-url' }] | ||||
}; | ||||
|
||||
const errors = validateBlogs(frontmatter); | ||||
expect(errors).toEqual(expect.arrayContaining([ | ||||
'Author at index 0 is missing a photo', | ||||
'Author at index 1 is missing a name', | ||||
'Invalid URL for author at index 2: not-a-url' | ||||
])); | ||||
}); | ||||
|
||||
it('validates docs frontmatter for required fields', () => { | ||||
const frontmatter = { title: 123, weight: 'not-a-number' }; | ||||
const errors = validateDocs(frontmatter); | ||||
expect(errors).toEqual(expect.arrayContaining([ | ||||
'Title is missing or not a string', | ||||
'Weight is missing or not a number' | ||||
])); | ||||
}); | ||||
|
||||
it('checks for errors in markdown files in a directory', done => { | ||||
fs.writeFileSync(path.join(tempDir, 'invalid.md'), `---\ntitle: Invalid Blog\n---`); | ||||
const mockConsoleLog = jest.spyOn(console, 'log').mockImplementation(); | ||||
|
||||
checkMarkdownFiles(tempDir, validateBlogs); | ||||
|
||||
setTimeout(() => { | ||||
expect(mockConsoleLog).toHaveBeenCalledWith(expect.stringContaining('Errors in file invalid.md:')); | ||||
mockConsoleLog.mockRestore(); | ||||
done(); | ||||
}, 100); | ||||
}); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Refactor async tests to use more reliable patterns. The current implementation has several issues:
Consider refactoring to use async/await pattern consistently: - it('checks for errors in markdown files in a directory', done => {
- fs.writeFileSync(path.join(tempDir, 'invalid.md'), `---\ntitle: Invalid Blog\n---`);
- const mockConsoleLog = jest.spyOn(console, 'log').mockImplementation();
-
- checkMarkdownFiles(tempDir, validateBlogs);
-
- setTimeout(() => {
- expect(mockConsoleLog).toHaveBeenCalledWith(expect.stringContaining('Errors in file invalid.md:'));
- mockConsoleLog.mockRestore();
- done();
- }, 100);
+ it('checks for errors in markdown files in a directory', async () => {
+ fs.writeFileSync(path.join(tempDir, 'invalid.md'), `---\ntitle: Invalid Blog\n---`);
+ const mockConsoleLog = jest.spyOn(console, 'log').mockImplementation();
+
+ await checkMarkdownFiles(tempDir, validateBlogs);
+
+ expect(mockConsoleLog).toHaveBeenCalledWith(expect.stringContaining('Errors in file invalid.md:'));
+ mockConsoleLog.mockRestore(); Also applies to: 83-94, 96-108 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we adding timeout here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replaced it with async/await |
||||
|
||||
it('returns multiple validation errors for invalid blog frontmatter', () => { | ||||
const frontmatter = { | ||||
title: 123, | ||||
date: 'invalid-date', | ||||
type: 'blog', | ||||
tags: 'not-an-array', | ||||
cover: ['not-a-string'], | ||||
authors: { name: 'John Doe' } | ||||
}; | ||||
const errors = validateBlogs(frontmatter); | ||||
console.log(errors) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove debug console.log statement. Debug logging should not be committed to the test suite. - console.log(errors) 📝 Committable suggestion
Suggested change
|
||||
|
||||
expect(errors.length).toBeGreaterThan(3); | ||||
}); | ||||
|
||||
it('handles filesystem errors gracefully', done => { | ||||
fs.writeFileSync(path.join(tempDir, 'test.md'), '---\ntitle: Test\n---\nContent'); | ||||
jest.spyOn(fs, 'stat').mockImplementation((path, callback) => callback(new Error('File stat error'))); | ||||
|
||||
checkMarkdownFiles(tempDir, validateBlogs); | ||||
|
||||
setTimeout(() => { | ||||
expect(mockConsoleError).toHaveBeenCalledWith('Error reading file stats:', expect.any(Error)); | ||||
fs.stat.mockRestore(); | ||||
done(); | ||||
}, 100); | ||||
}); | ||||
|
||||
it('handles errors when reading a directory', done => { | ||||
jest.spyOn(fs, 'readdir').mockImplementation((_, callback) => { | ||||
callback(new Error('Directory read error')); | ||||
}); | ||||
|
||||
checkMarkdownFiles(tempDir, validateBlogs); | ||||
|
||||
setTimeout(() => { | ||||
expect(mockConsoleError).toHaveBeenCalledWith('Error reading directory:', expect.any(Error)); | ||||
fs.readdir.mockRestore(); | ||||
done(); | ||||
}, 100); | ||||
}); | ||||
|
||||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix variable shadowing of mockConsoleError.
The
mockConsoleError
variable is already declared in the outer scope (line 12) but is redeclared in the test case (line 77). This could lead to confusion and potential issues.Apply this fix:
Also applies to: 75-82
🧰 Tools
🪛 eslint
[error] 11-11: Delete
··
(prettier/prettier)
[error] 12-12: Delete
··
(prettier/prettier)