-
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Issue Description
Current test coverage for fetch operations is incomplete. The retrieveArticleData() function doesn't properly mock the Fetch API response, causing test errors and incomplete validation of the fetch-then-process chain.
Risk Level
MEDIUM
Affected Files
src/scripts/index.test.jssrc/scripts/index.js(Lines 71-84 inretrieveArticleData())
Details
Current Test Issues
-
Error in Tests: Tests produce console errors:
Error retrieving article data: TypeError: response.json is not a function -
Incomplete Mock: The Fetch API response is not properly mocked in tests
-
Gap in Coverage: Lines 77-78 (error path) are not properly validated
Current Code with Issue
fetch(articleDataUrl)
.then((response) => response.json()) // Not mocked properly in tests
.then((articleData) => {
ArticleFiller.articleData = articleData;
ArticleFiller.callArticle();
})
.catch((error) => {
console.error("Error retrieving article data:", error);
});Expected Behavior
- All fetch operations should be properly mocked in tests
- Both success and error paths should be validated
- Test coverage for these lines should reach 100%
Solution Approach
Option A: Use jest-fetch-mock
npm install --save-dev jest-fetch-mockThen in test setup:
import fetchMock from 'jest-fetch-mock';
fetchMock.enableMocks();
beforeEach(() => {
fetchMock.resetMocks();
});
test('retrieveArticleData fetches and processes data', async () => {
const mockData = { TestArticle: { title: 'Test' } };
fetchMock.mockResponseOnce(JSON.stringify(mockData));
ArticleFiller.retrieveArticleData();
await new Promise(resolve => setTimeout(resolve, 10));
expect(ArticleFiller.articleData).toEqual(mockData);
});Option B: Manual Fetch Mock
global.fetch = jest.fn(() =>
Promise.resolve({
json: () => Promise.resolve({ TestArticle: {...} })
})
);Test Cases to Add
- ✅ Successfully fetch and parse article data
- ❌ Handle network errors
- ❌ Handle JSON parse errors
- ❌ Handle empty response
- ❌ Handle missing articleData.json file (404)
Current Coverage
- Overall: 70.9%
- Branch: 72.09%
- Target: 85%+ with proper fetch mocking
Labels
testing, quality, coverage
Reactions are currently unavailable