Testing Authentication Token Management in OpenHands Frontend
This test suite validates the authentication service functionality in the OpenHands frontend application, focusing on token management and storage operations. The tests ensure proper token retrieval and local storage interactions, critical for maintaining user authentication state.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
all-hands-ai/openhands
frontend/__tests__/services/auth.test.ts
import { beforeEach, describe, expect, it, vi, type Mock } from "vitest";
import { getToken } from "../../src/services/auth";
Storage.prototype.getItem = vi.fn();
Storage.prototype.setItem = vi.fn();
describe("Auth Service", () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe("getToken", () => {
it("should fetch and return a token", () => {
(Storage.prototype.getItem as Mock).mockReturnValue("newToken");
const data = getToken();
expect(localStorage.getItem).toHaveBeenCalledWith("token"); // Used to set Authorization header
expect(data).toEqual("newToken");
});
});
});