Skip to content

Commit

Permalink
added try catch block for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vishvamsinh28 committed Sep 13, 2024
1 parent 5e8c0c7 commit 1eb6790
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions tests/build-rss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ describe('rssFeed', () => {
});

it('should generate RSS feed and write to file', () => {
rssFeed(type, title, desc, outputPath);
try {
rssFeed(type, title, desc, outputPath);
} catch (err) {
throw new Error('Error encountered during test execution:', err)
}

const filePath = path.join(__dirname, '..', 'public', outputPath);
expect(fs.existsSync(filePath)).toBe(true);
Expand All @@ -32,26 +36,34 @@ describe('rssFeed', () => {
});

it('should prioritize featured posts over non-featured ones', () => {
rssFeed(type, title, desc, outputPath);

try {
rssFeed(type, title, desc, outputPath);
} catch (err) {
throw new Error('Error encountered during test execution:', err)
}

const filePath = path.join(__dirname, '..', 'public', outputPath);
const fileContent = fs.readFileSync(filePath, 'utf8');

const itemTitles = fileContent.match(/<title>(.*?)<\/title>/g);

expect(itemTitles[1]).toContain('Test Post 1');
expect(itemTitles[2]).toContain('Another Featured Post');
expect(itemTitles[3]).toContain('Non-Featured Post 1');
});

it('should sort posts by date in descending order', () => {
rssFeed(type, title, desc, outputPath);

try {
rssFeed(type, title, desc, outputPath);
} catch (err) {
throw new Error('Error encountered during test execution:', err)
}

const filePath = path.join(__dirname, '..', 'public', outputPath);
const fileContent = fs.readFileSync(filePath, 'utf8');

const itemTitles = fileContent.match(/<title>(.*?)<\/title>/g);

expect(itemTitles[1]).toContain('Test Post 1');
expect(itemTitles[2]).toContain('Another Featured Post');
expect(itemTitles[3]).toContain('Non-Featured Post 1');
Expand All @@ -60,7 +72,11 @@ describe('rssFeed', () => {
});

it('should set correct enclosure type based on image extension', () => {
rssFeed(type, title, desc, outputPath);
try {
rssFeed(type, title, desc, outputPath);
} catch (err) {
throw new Error('Error encountered during test execution:', err)
}

const filePath = path.join(__dirname, '..', 'public', outputPath);
const fileContent = fs.readFileSync(filePath, 'utf8');
Expand All @@ -75,9 +91,9 @@ describe('rssFeed', () => {

it('should throw error when write operation fails', () => {
const outputPath = "invalid/path"
try{
try {
rssFeed(type, title, desc, outputPath)
}catch(err){
} catch (err) {
expect(err.message).toMatch(/ENOENT|EACCES/);
}
});
Expand Down

0 comments on commit 1eb6790

Please sign in to comment.