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: increase code coverage #3517

Merged
merged 6 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
coverageReporters: ['text', 'lcov', 'json-summary'],
coverageDirectory: 'coverage',
collectCoverageFrom: ['scripts/**/*.js'],
coveragePathIgnorePatterns: ['scripts/compose.js'],
coveragePathIgnorePatterns: ['scripts/compose.js', 'scripts/tools/categorylist.js', 'scripts/tools/tags-color.js'],
// To disallow netlify edge function tests from running
testMatch: ['**/tests/**/*.test.*', '!**/netlify/**/*.test.*'],
};
10 changes: 7 additions & 3 deletions scripts/build-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ const TARGET_DIR = 'pages';
const capitalizeTags = ['table', 'tr', 'td', 'th', 'thead', 'tbody'];

// Check if target directory doesn't exist then create it
if (!fs.existsSync(TARGET_DIR)) {
fs.mkdirSync(TARGET_DIR, { recursive: true });
function ensureDirectoryExists(directory) {
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, { recursive: true });
}
}

ensureDirectoryExists(TARGET_DIR);

function capitalizeJsxTags(content) {
return content.replace(/<\/?(\w+)/g, function (match, letter) {
if (capitalizeTags.includes(letter.toLowerCase())) {
Expand Down Expand Up @@ -57,4 +61,4 @@ function copyAndRenameFiles(srcDir, targetDir) {

copyAndRenameFiles(SRC_DIR, TARGET_DIR);

module.exports = {copyAndRenameFiles,capitalizeJsxTags}
module.exports = { copyAndRenameFiles,capitalizeJsxTags, ensureDirectoryExists }
11 changes: 10 additions & 1 deletion tests/build-pages.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs');
const path = require('path');
const { capitalizeJsxTags, copyAndRenameFiles } = require('../scripts/build-pages');
const { capitalizeJsxTags, copyAndRenameFiles, ensureDirectoryExists } = require('../scripts/build-pages');

describe('capitalizeJsxTags', () => {
test('should capitalize JSX tags', () => {
Expand Down Expand Up @@ -45,4 +45,13 @@ describe('copyAndRenameFiles', () => {
expect(targetFile).toBe('<Table><Tr><Td>Hello</Td></Tr></Table>');
expect(nestedTargetFile).toBe('<Table><Tr><Td>Hello</Td></Tr></Table>');
});

test('should create a directory if it does not exist', () => {
const NEW_TEST_DIR = 'testDir';

expect(fs.existsSync(NEW_TEST_DIR)).toBe(false);
ensureDirectoryExists(NEW_TEST_DIR);
expect(fs.existsSync(NEW_TEST_DIR)).toBe(true);
});

});
5 changes: 5 additions & 0 deletions tests/dashboard/build-dashboard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,9 @@ describe('GitHub Discussions Processing', () => {
localConsoleErrorSpy.mockRestore();
});

it('should handle write failures gracefully', async () => {

await expect(writeToFile()).rejects.toThrow();
});

});
11 changes: 11 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const buildCaseStudiesList = require('../scripts/casestudies');
const buildAdoptersList = require('../scripts/adopters');
const buildFinanceInfoList = require('../scripts/finance');
const start = require('../scripts/index');
const fs = require('fs');

jest.mock('../scripts/build-rss');
jest.mock('../scripts/build-post-list');
Expand Down Expand Up @@ -32,4 +33,14 @@ describe('start function', () => {
expect(buildAdoptersList).toHaveBeenCalled();
expect(buildFinanceInfoList).toHaveBeenCalled();
});

test('should throw an error if no finance data is found', async () => {
jest.spyOn(fs, 'readdirSync').mockReturnValue([]);

await expect(start()).rejects.toThrow('No finance data found in the finance directory.');

expect(buildFinanceInfoList).not.toHaveBeenCalled();

fs.readdirSync.mockRestore();
});
});
Loading