From 02e221b624b1daf38ceecbf1a9a6ed2491251013 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Thu, 28 Nov 2024 22:29:11 +0530 Subject: [PATCH] added test for main function --- scripts/markdown/check-markdown.js | 2 +- tests/markdown/check-markdown.test.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/scripts/markdown/check-markdown.js b/scripts/markdown/check-markdown.js index d36f43d024e..45df04962ef 100644 --- a/scripts/markdown/check-markdown.js +++ b/scripts/markdown/check-markdown.js @@ -157,4 +157,4 @@ async function main() { main(); -module.exports = { validateBlogs, validateDocs, checkMarkdownFiles } +module.exports = { validateBlogs, validateDocs, checkMarkdownFiles, main } diff --git a/tests/markdown/check-markdown.test.js b/tests/markdown/check-markdown.test.js index b8bf563a544..d59c92987f4 100644 --- a/tests/markdown/check-markdown.test.js +++ b/tests/markdown/check-markdown.test.js @@ -2,6 +2,7 @@ const fs = require('fs').promises; const path = require('path'); const os = require('os'); const { + main, validateBlogs, validateDocs, checkMarkdownFiles @@ -116,4 +117,19 @@ describe('Frontmatter Validator', () => { mockReadFile.mockRestore(); }); + it('should handle main function errors', async () => { + const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {}); + jest.spyOn(fs, 'readdir').mockRejectedValue(new Error('Test error')); + + await main(); + + expect(mockConsoleError).toHaveBeenCalledWith( + 'Failed to validate markdown files:', + expect.any(Error) + ); + expect(mockExit).toHaveBeenCalledWith(1); + + mockExit.mockRestore(); + }); + });