Skip to content

Commit

Permalink
updated test
Browse files Browse the repository at this point in the history
  • Loading branch information
vishvamsinh28 committed Sep 20, 2024
1 parent 0f61abe commit e06fa47
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
7 changes: 6 additions & 1 deletion scripts/build-rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const fs = require('fs')
const json2xml = require('jgexml/json2xml')

function getAllPosts() {
return require('../config/posts.json')
try {
return require('../config/posts.json');
} catch (err) {
throw new Error('Cannot find posts.json');
}
}

function clean(s) {
Expand All @@ -18,6 +22,7 @@ function clean(s) {
module.exports = function rssFeed(type, title, desc, outputPath) {
return new Promise((resolve, reject) => {
try {

const posts = getAllPosts()[`${type}`]
.sort((i1, i2) => {
const i1Date = new Date(i1.date)
Expand Down
24 changes: 14 additions & 10 deletions tests/build-rss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,24 @@ describe('rssFeed', () => {
});

it('should throw an error when posts.json is not found', async () => {
jest.doMock('../config/posts.json', () => {
throw new Error('Cannot find module');
}, { virtual: true });

let error;
const postsJsonPath = path.join(__dirname, '..', 'config', 'posts.json');
const tempPath = path.join(__dirname, '..', 'config', 'posts.json.temp');

if (fs.existsSync(postsJsonPath)) {
fs.renameSync(postsJsonPath, tempPath);
}

try {
await rssFeed(type, title, desc, outputPath);
} catch (err) {
error = err;
expect(err.message).toContain('Failed to generate RSS feed');
expect(err.message).toContain('Cannot find posts.json');
} finally {
if (fs.existsSync(tempPath)) {
fs.renameSync(tempPath, postsJsonPath);
}
}

expect(error).toBeDefined();
expect(error.message).toContain('Failed to generate RSS feed');
expect(error.message).toContain('Cannot find module');

});

it('should throw an error when posts.json is malformed', async () => {
Expand Down

0 comments on commit e06fa47

Please sign in to comment.