fix: remove window.location mocking to fix CI test failures

Remove all window.location mocking from AuthDebugger tests as it was causing "Cannot redefine property" errors in CI. The tests work correctly with JSDOM's default window.location implementation.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jerome 2025-06-13 18:06:54 +01:00
commit b08827d94d
No known key found for this signature in database

View file

@ -84,21 +84,6 @@ Object.defineProperty(window, "sessionStorage", {
value: sessionStorageMock, value: sessionStorageMock,
}); });
// Try to mock window.location, but don't fail if it doesn't work (e.g., in CI)
try {
Object.defineProperty(window, "location", {
value: {
origin: "http://localhost:3000",
},
});
} catch {
// Ignore error - tests that depend on this will be skipped
}
// Skip location-dependent tests in CI where window.location mocking fails
const isCI = process.env.CI === "true";
const skipIfCI = isCI ? it.skip : it;
describe("AuthDebugger", () => { describe("AuthDebugger", () => {
const defaultAuthState = EMPTY_DEBUGGER_STATE; const defaultAuthState = EMPTY_DEBUGGER_STATE;
@ -113,7 +98,7 @@ describe("AuthDebugger", () => {
jest.clearAllMocks(); jest.clearAllMocks();
sessionStorageMock.getItem.mockReturnValue(null); sessionStorageMock.getItem.mockReturnValue(null);
// Supress // Suppress console errors in tests to avoid JSDOM navigation noise
jest.spyOn(console, "error").mockImplementation(() => {}); jest.spyOn(console, "error").mockImplementation(() => {});
mockDiscoverOAuthMetadata.mockResolvedValue(mockOAuthMetadata); mockDiscoverOAuthMetadata.mockResolvedValue(mockOAuthMetadata);
@ -453,65 +438,58 @@ describe("AuthDebugger", () => {
}); });
describe("OAuth State Persistence", () => { describe("OAuth State Persistence", () => {
skipIfCI( it("should store auth state to sessionStorage before redirect in Quick OAuth Flow", async () => {
"should store auth state to sessionStorage before redirect in Quick OAuth Flow", const updateAuthState = jest.fn();
async () => {
const updateAuthState = jest.fn();
// This test depends on window.location being mocked at the top of the file // Setup mocks for OAuth flow
mockStartAuthorization.mockResolvedValue({
authorizationUrl: new URL(
"https://oauth.example.com/authorize?client_id=test_client_id&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Foauth%2Fcallback%2Fdebug",
),
codeVerifier: "test_verifier",
});
// Setup mocks for OAuth flow await act(async () => {
mockStartAuthorization.mockResolvedValue({ renderAuthDebugger({
authorizationUrl: new URL( updateAuthState,
"https://oauth.example.com/authorize?client_id=test_client_id&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Foauth%2Fcallback%2Fdebug", authState: { ...defaultAuthState },
),
codeVerifier: "test_verifier",
}); });
});
await act(async () => { // Click Quick OAuth Flow
renderAuthDebugger({ await act(async () => {
updateAuthState, fireEvent.click(screen.getByText("Quick OAuth Flow"));
authState: { ...defaultAuthState }, });
});
});
// Click Quick OAuth Flow // Wait for the flow to reach the authorization step
await act(async () => { await waitFor(() => {
fireEvent.click(screen.getByText("Quick OAuth Flow")); expect(sessionStorage.setItem).toHaveBeenCalledWith(
}); SESSION_KEYS.AUTH_DEBUGGER_STATE,
expect.stringContaining('"oauthStep":"authorization_code"'),
// Wait for the flow to reach the authorization step
await waitFor(() => {
expect(sessionStorage.setItem).toHaveBeenCalledWith(
SESSION_KEYS.AUTH_DEBUGGER_STATE,
expect.stringContaining('"oauthStep":"authorization_code"'),
);
});
// Verify the stored state includes all the accumulated data
const storedStateCall = (
sessionStorage.setItem as jest.Mock
).mock.calls.find(
(call) => call[0] === SESSION_KEYS.AUTH_DEBUGGER_STATE,
); );
});
expect(storedStateCall).toBeDefined(); // Verify the stored state includes all the accumulated data
const storedState = JSON.parse(storedStateCall![1] as string); const storedStateCall = (
sessionStorage.setItem as jest.Mock
).mock.calls.find((call) => call[0] === SESSION_KEYS.AUTH_DEBUGGER_STATE);
expect(storedState).toMatchObject({ expect(storedStateCall).toBeDefined();
oauthStep: "authorization_code", const storedState = JSON.parse(storedStateCall![1] as string);
authorizationUrl: expect.stringMatching(
/^https:\/\/oauth\.example\.com\/authorize/, expect(storedState).toMatchObject({
), oauthStep: "authorization_code",
oauthMetadata: expect.objectContaining({ authorizationUrl: expect.stringMatching(
token_endpoint: "https://oauth.example.com/token", /^https:\/\/oauth\.example\.com\/authorize/,
}), ),
oauthClientInfo: expect.objectContaining({ oauthMetadata: expect.objectContaining({
client_id: "test_client_id", token_endpoint: "https://oauth.example.com/token",
}), }),
}); oauthClientInfo: expect.objectContaining({
}, client_id: "test_client_id",
); }),
});
});
}); });
describe("OAuth Protected Resource Metadata", () => { describe("OAuth Protected Resource Metadata", () => {