From 2182feb488290439b0925b85a853b6f65c063d0e Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Thu, 17 Oct 2024 13:05:30 +0530 Subject: [PATCH] tests updated --- scripts/build-post-list.js | 5 +++- tests/build-post-list.test.js | 45 ++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/scripts/build-post-list.js b/scripts/build-post-list.js index 75867807292f..84fe1caebec1 100644 --- a/scripts/build-post-list.js +++ b/scripts/build-post-list.js @@ -28,6 +28,9 @@ const addItem = (details) => { async function buildPostList(postDirectories, basePath, writeFilePath) { try { + if (postDirectories.length === 0) { + throw new Error('Error while building post list: No post directories provided'); + } walkDirectories(postDirectories, result, basePath) const treePosts = buildNavTree(result["docs"].filter((p) => p.slug.startsWith('/docs/'))) result["docsTree"] = treePosts @@ -157,4 +160,4 @@ function capitalize(text) { return text.split(/[\s\-]/g).map(word => `${word[0].toUpperCase()}${word.substr(1)}`).join(' ') } -module.exports = {slugifyToC, buildPostList} \ No newline at end of file +module.exports = {slugifyToC, buildPostList} diff --git a/tests/build-post-list.test.js b/tests/build-post-list.test.js index a1c0113ced5b..7eda419e444b 100644 --- a/tests/build-post-list.test.js +++ b/tests/build-post-list.test.js @@ -145,4 +145,47 @@ describe('buildPostList', () => { expect(explorerEntry).toBeUndefined(); }); -}); \ No newline at end of file + it('throws an error if the directory cannot be read', async () => { + const invalidDir = [join(tempDir, 'non-existent-dir'), '/invalid']; + + let error; + try { + await buildPostList([invalidDir], tempDir, writeFilePath); + } catch (err) { + error = err; + } + + expect(error).toBeDefined(); + expect(error.message).toMatch(/Error while building post list/); + }); + + + it('throws an error if the front matter cannot be parsed', async () => { + writeFileSync(join(tempDir, 'docs', 'invalid.mdx'), '---\ninvalid front matter\n---\nContent'); + + let error; + try { + await buildPostList(postDirectories, tempDir, writeFilePath); + } catch (err) { + error = err; + } + + expect(error).toBeDefined(); + expect(error.message).toMatch(/Error while building post list/); + }); + + it('throws an error if no post directories are provided', async () => { + + let error; + + try { + await buildPostList([], tempDir, writeFilePath); + } catch (err) { + error = err; + } + + expect(error).toBeDefined(); + expect(error.message).toMatch(/Error while building post list/); + }); + +});