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

feat: add tests for check markdown script #3378

Merged
merged 60 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
5a90f49
initial setup
vishvamsinh28 Nov 9, 2024
5373c9f
tests updated
vishvamsinh28 Nov 9, 2024
a30cef3
test updated again
vishvamsinh28 Nov 9, 2024
a9da240
fwqfwef
vishvamsinh28 Nov 9, 2024
1079101
Merge branch 'master' into markdownTest
vishvamsinh28 Nov 10, 2024
86201ed
use async await
vishvamsinh28 Nov 10, 2024
7c44f4a
tests updated
vishvamsinh28 Nov 10, 2024
c98ecdd
wqegeg
vishvamsinh28 Nov 10, 2024
6e34205
test updated
vishvamsinh28 Nov 10, 2024
4445ce5
Merge branch 'master' into markdownTest
vishvamsinh28 Nov 11, 2024
333e0b2
feg
vishvamsinh28 Nov 11, 2024
8849351
Fixed variable shadowing
vishvamsinh28 Nov 11, 2024
1fbac7c
Merge branch 'master' into markdownTest
vishvamsinh28 Nov 13, 2024
fada5ab
added tests for continue
vishvamsinh28 Nov 13, 2024
68cd3ee
workflow re-run
vishvamsinh28 Nov 14, 2024
998af5a
workflow update
vishvamsinh28 Nov 14, 2024
f5c8964
ff
vishvamsinh28 Nov 14, 2024
d92b519
Merge branch 'master' into markdownTest
vishvamsinh28 Nov 15, 2024
d0fe33a
Merge branch 'master' into markdownTest
vishvamsinh28 Nov 18, 2024
216a511
Merge branch 'master' into markdownTest
vishvamsinh28 Nov 22, 2024
8a5235b
Merge branch 'master' into markdownTest
vishvamsinh28 Nov 23, 2024
a93b45d
suggetion applied
vishvamsinh28 Nov 23, 2024
2732f0f
suggetion applied
vishvamsinh28 Nov 23, 2024
4fcdea8
suggetion applied 2
vishvamsinh28 Nov 23, 2024
e1a16c1
test added for err
vishvamsinh28 Nov 23, 2024
7401e7f
Merge branch 'master' into markdownTest
vishvamsinh28 Nov 28, 2024
0fbdc06
main function added
vishvamsinh28 Nov 28, 2024
02e221b
added test for main function
vishvamsinh28 Nov 28, 2024
b17b83e
tests updated
vishvamsinh28 Nov 28, 2024
35d2e84
jgk
vishvamsinh28 Nov 28, 2024
7921604
test updated
vishvamsinh28 Nov 28, 2024
9d75691
Merge branch 'master' into markdownTest
vishvamsinh28 Nov 29, 2024
b36972d
Merge branch 'markdownTest' of https://github.com/vishvamsinh28/websi…
vishvamsinh28 Nov 29, 2024
dff6ccc
main function test added
vishvamsinh28 Nov 29, 2024
53c86fd
test updated
vishvamsinh28 Nov 29, 2024
92cc885
test updates
vishvamsinh28 Nov 29, 2024
871e305
Merge branch 'master' into markdownTest
vishvamsinh28 Nov 29, 2024
c0afa97
script updated
vishvamsinh28 Nov 29, 2024
10c1ba5
add tests for main function
vishvamsinh28 Nov 29, 2024
2fa09e6
test updated
vishvamsinh28 Nov 29, 2024
aa0e241
status code upudated
vishvamsinh28 Nov 29, 2024
746dc88
test updated
vishvamsinh28 Nov 29, 2024
4558748
Merge branch 'markdownTest' of https://github.com/vishvamsinh28/websi…
vishvamsinh28 Nov 29, 2024
29cd78c
script updated
vishvamsinh28 Nov 29, 2024
e50392c
Merge branch 'master' into markdownTest
vishvamsinh28 Nov 30, 2024
7a4b871
Merge branch 'master' into markdownTest
vishvamsinh28 Dec 2, 2024
d993379
Merge branch 'master' into markdownTest
vishvamsinh28 Dec 4, 2024
8d9221b
apply nitpicks
vishvamsinh28 Dec 4, 2024
f595339
coverage udpated
vishvamsinh28 Dec 4, 2024
1cb57f0
Merge branch 'master' into markdownTest
vishvamsinh28 Dec 5, 2024
4ee6c48
Merge branch 'master' into markdownTest
vishvamsinh28 Dec 7, 2024
04127eb
Merge branch 'master' into markdownTest
vishvamsinh28 Dec 7, 2024
bcaf621
remove nested try/catch
vishvamsinh28 Dec 8, 2024
dd802c4
Merge branch 'master' into markdownTest
vishvamsinh28 Dec 9, 2024
1a46eda
Merge branch 'master' into markdownTest
vishvamsinh28 Dec 9, 2024
3a9916c
Merge branch 'master' into markdownTest
vishvamsinh28 Dec 10, 2024
cc23800
apply nitpicks
vishvamsinh28 Dec 10, 2024
486b4af
Merge branch 'master' into markdownTest
vishvamsinh28 Dec 12, 2024
0a91465
Merge branch 'master' into markdownTest
vishvamsinh28 Dec 13, 2024
17dab5c
Merge branch 'master' into markdownTest
vishvamsinh28 Dec 14, 2024
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
2 changes: 2 additions & 0 deletions scripts/markdown/check-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,5 @@ const blogsFolderPath = path.resolve(__dirname, '../../markdown/blog');

checkMarkdownFiles(docsFolderPath, validateDocs);
checkMarkdownFiles(blogsFolderPath, validateBlogs);

module.exports = { validateBlogs, validateDocs, checkMarkdownFiles,}
110 changes: 110 additions & 0 deletions tests/markdown/check-markdown.test.js
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;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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:

 it('logs error to console when an error occurs in checkMarkdownFiles', async () => {
     const invalidFolderPath = path.join(tempDir, 'non-existent-folder');
-    const mockConsoleError = jest.spyOn(console, 'error').mockImplementation();

     await checkMarkdownFiles(invalidFolderPath, validateBlogs);

     expect(mockConsoleError.mock.calls[0][0]).toContain('Error processing files:');
-    mockConsoleError.mockRestore();
 });

Also applies to: 75-82

🧰 Tools
🪛 eslint

[error] 11-11: Delete ··

(prettier/prettier)


[error] 12-12: Delete ··

(prettier/prettier)

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);
});
Copy link

Choose a reason for hiding this comment

The 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:

  1. Hardcoded timeout values (100ms) might cause flaky tests
  2. setTimeout with done callback is an anti-pattern in Jest
  3. Inconsistent async testing patterns across different test cases

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we adding timeout here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove debug console.log statement.

Debug logging should not be committed to the test suite.

-        console.log(errors)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
console.log(errors)


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);
});

});
Loading