[BUGFIX] Missing await on async fetch calls#4
Open
GAURAV-1313 wants to merge 2 commits into
Open
Conversation
✅ Deploy Preview for nofriction canceled.
|
Owner
Author
AI Code ReviewSummaryThe introduced async function has multiple async/await issues and lacks input validation, which leads to probable runtime errors and unstable behavior under erroneous conditions. Key FindingsBugs
Security
Performance
Recommended FixesGeneral
Security Fixes
Performance Fixes
Suggested Tests
import { fetchAndProcess } from "./api";
describe("fetchAndProcess", () => {
beforeEach(() => {
global.fetch = jest.fn();
});
afterEach(() => {
jest.restoreAllMocks();
});
it("should return items array when fetch and json resolve correctly", async () => {
const mockItems = [{ id: 1 }, { id: 2 }];
const mockJson = jest.fn().mockResolvedValue({ items: mockItems });
const mockResponse = { json: mockJson };
global.fetch.mockResolvedValue(mockResponse);
const url = "https://example.com/api";
const result = await fetchAndProcess(url);
expect(global.fetch).toHaveBeenCalledWith(url);
expect(mockJson).toHaveBeenCalled();
expect(Array.isArray(result)).toBe(true);
expect(result).toEqual(mockItems);
});
it("should handle empty items array from response", async () => {
const mockJson = jest.fn().mockResolvedValue({ items: [] });
const mockResponse = { json: mockJson };
global.fetch.mockResolvedValue(mockResponse);
const result = await fetchAndProcess("https://example.com/api");
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(0);
});
it("should throw if fetch rejects", async () => {
global.fetch.mockRejectedValue(new Error("Network error"));
await expect(fetchAndProcess("https://example.com/api")).rejects.toThrow("Network error");
});
it("should throw if json parsing fails", async () => {
const mockJson = jest.fn().mockRejectedValue(new Error("Invalid JSON"));
const mockResponse = { json: mockJson };
global.fetch.mockResolvedValue(mockResponse);
await expect(fetchAndProcess("https://example.com/api")).rejects.toThrow("Invalid JSON");
});
it("should throw if data.items is undefined", async () => {
const mockJson = jest.fn().mockResolvedValue({ noItems: true });
const mockResponse = { json: mockJson };
global.fetch.mockResolvedValue(mockResponse);
await expect(fetchAndProcess("https://example.com/api")).rejects.toThrow(TypeError);
});
it("should throw if called with undefined url", async () => {
await expect(fetchAndProcess(undefined)).rejects.toThrow();
});
it("should not mutate input url", async () => {
const url = "https://example.com/api";
const urlCopy = url;
const mockJson = jest.fn().mockResolvedValue({ items: [1, 2] });
const mockResponse = { json: mockJson };
global.fetch.mockResolvedValue(mockResponse);
await fetchAndProcess(url);
expect(url).toBe(urlCopy);
});
});Auto-Fix AvailableAdd the 6 issues found. Reviewed by RepoSpace AI. |
- src/api.js: Added 'await' to fetch and response.json() to handle asynchronous operations correctly.
Owner
Author
Auto-Fix ResultsApplied (1)
Generated by RepoSpace AI. Review AI commits before merging. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Async fetch calls missing await, returns Promise instead of data.