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

frontend: fix websocket logs errors in tests #2754

Merged
merged 2 commits into from
Jan 17, 2025
Merged

Conversation

knrt10
Copy link
Contributor

@knrt10 knrt10 commented Jan 17, 2025

We now mock console logs in tests and do not console errors for tests env.

This:

  • Remove unnecessary console logs due to v2 websocket usage
  • Fix some message handling bugs in some error conditions

Part of:

Click to view Old logs
WebSocket error: Event {
  type: 'error',
  timeStamp: 1737091724622,
  target: WebSocket {
    listeners: { open: [Array], message: [Array], error: [Array], close: [Array] },
    _onopen: [Function (anonymous)],
    _onmessage: [Function: bound handleWebSocketMessage],
    _onerror: [Function (anonymous)],
    _onclose: [Function (anonymous)],
    url: 'ws://localhost:4466/wsMultiplexer',
    protocol: '',
    binaryType: 'blob',
    readyState: 3
  },
  srcElement: WebSocket {
    listeners: { open: [Array], message: [Array], error: [Array], close: [Array] },
    _onopen: [Function (anonymous)],
    _onmessage: [Function: bound handleWebSocketMessage],
    _onerror: [Function (anonymous)],
    _onclose: [Function (anonymous)],
    url: 'ws://localhost:4466/wsMultiplexer',
    protocol: '',
    binaryType: 'blob',
    readyState: 3
  },
  returnValue: true,
  isTrusted: false,
  eventPhase: 0,
  defaultPrevented: false,
  currentTarget: WebSocket {
    listeners: { open: [Array], message: [Array], error: [Array], close: [Array] },
    _onopen: [Function (anonymous)],
    _onmessage: [Function: bound handleWebSocketMessage],
    _onerror: [Function (anonymous)],
    _onclose: [Function (anonymous)],
    url: 'ws://localhost:4466/wsMultiplexer',
    protocol: '',
    binaryType: 'blob',
    readyState: 3
  },
  cancelable: false,
  cancelBubble: false,
  bubbles: false
}

stderr | src/lib/k8s/api/v2/webSocket.test.ts > WebSocket Tests > useWebSocket hook > should handle connection errors
WebSocket error: Event {
  type: 'error',
  timeStamp: 1737091724691,
  target: WebSocket {
    listeners: { open: [Array], message: [Array], error: [Array], close: [Array] },
    _onopen: [Function (anonymous)],
    _onmessage: [Function: bound handleWebSocketMessage],
    _onerror: [Function (anonymous)],
    _onclose: [Function (anonymous)],
    url: 'ws://localhost:4466/wsMultiplexer',
    protocol: '',
    binaryType: 'blob',
    readyState: 3
  },
  srcElement: WebSocket {
    listeners: { open: [Array], message: [Array], error: [Array], close: [Array] },
    _onopen: [Function (anonymous)],
    _onmessage: [Function: bound handleWebSocketMessage],
    _onerror: [Function (anonymous)],
    _onclose: [Function (anonymous)],
    url: 'ws://localhost:4466/wsMultiplexer',
    protocol: '',
    binaryType: 'blob',
    readyState: 3
  },
  returnValue: true,
  isTrusted: false,
  eventPhase: 0,
  defaultPrevented: false,
  currentTarget: WebSocket {
    listeners: { open: [Array], message: [Array], error: [Array], close: [Array] },
    _onopen: [Function (anonymous)],
    _onmessage: [Function: bound handleWebSocketMessage],
    _onerror: [Function (anonymous)],
    _onclose: [Function (anonymous)],
    url: 'ws://localhost:4466/wsMultiplexer',
    protocol: '',
    binaryType: 'blob',
    readyState: 3
  },
  cancelable: false,
  cancelBubble: false,
  bubbles: false
}
WebSocket connection failed: Error: WebSocket connection failed
    at WebSocket.socket.onerror [as _onerror] (/Users/kautilya/dev/microsoft/headlamp/frontend/src/lib/k8s/api/v2/webSocket.ts:153:16)
    at /Users/kautilya/dev/microsoft/headlamp/frontend/node_modules/mock-socket/dist/mock-socket.js:859:16
    at Array.forEach (<anonymous>)
    at WebSocket.dispatchEvent (/Users/kautilya/dev/microsoft/headlamp/frontend/node_modules/mock-socket/dist/mock-socket.js:855:13)
    at WebSocket.delayCallback (/Users/kautilya/dev/microsoft/headlamp/frontend/node_modules/mock-socket/dist/mock-socket.js:1530:14)
    at Timeout._onTimeout (/Users/kautilya/dev/microsoft/headlamp/frontend/node_modules/mock-socket/dist/mock-socket.js:757:58)
    at listOnTimeout (node:internal/timers:575:11)
    at processTimers (node:internal/timers:514:7)

stderr | src/lib/k8s/api/v2/webSocket.test.ts > WebSocket Tests > WebSocket error handling > should handle invalid message format
Failed to process WebSocket message: SyntaxError: Unexpected token 'i', "invalid json" is not valid JSON
    at JSON.parse (<anonymous>)
    at Object.handleWebSocketMessage (/Users/kautilya/dev/microsoft/headlamp/frontend/src/lib/k8s/api/v2/webSocket.ts:311:25)
    at /Users/kautilya/dev/microsoft/headlamp/frontend/node_modules/mock-socket/dist/mock-socket.js:859:16
    at Array.forEach (<anonymous>)
    at Proxy.dispatchEvent (/Users/kautilya/dev/microsoft/headlamp/frontend/node_modules/mock-socket/dist/mock-socket.js:855:13)
    at /Users/kautilya/dev/microsoft/headlamp/frontend/node_modules/mock-socket/dist/mock-socket.js:2133:16
    at Array.forEach (<anonymous>)
    at Server.emit (/Users/kautilya/dev/microsoft/headlamp/frontend/node_modules/mock-socket/dist/mock-socket.js:2121:16)
    at file:///Users/kautilya/dev/microsoft/headlamp/frontend/node_modules/vitest-websocket-mock/dist/index.js:133:19
    at act (file:///Users/kautilya/dev/microsoft/headlamp/frontend/node_modules/vitest-websocket-mock/dist/index.js:33:5)

stderr | src/lib/k8s/api/v2/webSocket.test.ts > WebSocket Tests > WebSocket error handling > should handle message callback errors in useWebSocket
Failed to process WebSocket message: Error: Message processing failed
    at /Users/kautilya/dev/microsoft/headlamp/frontend/src/lib/k8s/api/v2/webSocket.test.ts:536:15
    at mockCall (file:///Users/kautilya/dev/microsoft/headlamp/frontend/node_modules/vitest/node_modules/@vitest/spy/dist/index.js:61:17)
    at spy (file:///Users/kautilya/dev/microsoft/headlamp/frontend/node_modules/tinyspy/dist/index.js:45:80)
    at /Users/kautilya/dev/microsoft/headlamp/frontend/src/lib/k8s/api/v2/webSocket.ts:421:9
    at Object.handleWebSocketMessage (/Users/kautilya/dev/microsoft/headlamp/frontend/src/lib/k8s/api/v2/webSocket.ts:339:15)
    at /Users/kautilya/dev/microsoft/headlamp/frontend/node_modules/mock-socket/dist/mock-socket.js:859:16
    at Array.forEach (<anonymous>)
    at Proxy.dispatchEvent (/Users/kautilya/dev/microsoft/headlamp/frontend/node_modules/mock-socket/dist/mock-socket.js:855:13)
    at /Users/kautilya/dev/microsoft/headlamp/frontend/node_modules/mock-socket/dist/mock-socket.js:2133:16
    at Array.forEach (<anonymous>)

Testing

  • cd frontend && npm test webSocket.test.ts

@knrt10 knrt10 requested a review from illume January 17, 2025 05:10
@knrt10 knrt10 changed the title frontend: fix websocket logs errros in tests frontend: fix websocket logs erros in tests Jan 17, 2025
@knrt10 knrt10 force-pushed the websocket-frontend-tests-fix branch from 77e2c03 to 2ac5203 Compare January 17, 2025 05:11
@knrt10 knrt10 changed the title frontend: fix websocket logs erros in tests frontend: fix websocket logs errors in tests Jan 17, 2025
@knrt10 knrt10 force-pushed the websocket-frontend-tests-fix branch 3 times, most recently from 15dac73 to 6432c2e Compare January 17, 2025 05:24
@illume illume added bug Something isn't working frontend Issues related to the frontend multi Multi cluster aggregated view labels Jan 17, 2025
We now mock console logs in tests and do not console errors for tests
env.

Signed-off-by: Kautilya Tripathi <[email protected]>
This prevents cascading failures where one broken listener could
affect other WebSocket subscriptions, and ensures control messages
are handled correctly before any data parsing attempts.

Signed-off-by: Kautilya Tripathi <[email protected]>
@knrt10 knrt10 force-pushed the websocket-frontend-tests-fix branch from 6432c2e to 39dfc3a Compare January 17, 2025 05:36
Copy link
Collaborator

@illume illume left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thank you!

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. lgtm This PR has been approved by a maintainer labels Jan 17, 2025
@illume illume merged commit e67e512 into main Jan 17, 2025
18 checks passed
@illume illume deleted the websocket-frontend-tests-fix branch January 17, 2025 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working frontend Issues related to the frontend lgtm This PR has been approved by a maintainer multi Multi cluster aggregated view size:M This PR changes 30-99 lines, ignoring generated files.
Projects
Development

Successfully merging this pull request may close these issues.

2 participants