Can't mock a secure web socket(wss) request #15195
Unanswered
tr185112
asked this question in
Questions and Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
We are trying to mock a web socket API request with Cypress and not able to mock a server connection. This is what we have so far. In a server.js file -
import { Server } from "mock-socket";
const sockets = {};
export function initServer() {
for (const socket of Object.values(sockets)) {
socket.close();
}
mockServer();
}
function mockServer() {
sockets.mockServer = new Server("wss://10.0.0.291:1199");
sockets.mockServer.on("connection", (socket) => {
sockets.server = socket;
console.log("Connection established");
socket.send("Hello, world!");
socket.on("unlockWithCredentials", (data) => {
// Do whatever you want with the message, you can use socket.send to send a response
socket.send("Hello, world!");
});
});
}
In a cypress test file -
import { initServer } from "../../support/Server.js";
cy.visit("/", {
onBeforeLoad(win) {
initServer();
cy.stub(win, "WebSocket", (url) => new MockSocket.WebSocket(url));
},
});
Seeing the following error -
The following error originated from your application code, not from Cypress.
We already have mock-socket installed.
Beta Was this translation helpful? Give feedback.
All reactions