Giter VIP home page Giter VIP logo

Comments (3)

Atrue avatar Atrue commented on June 3, 2024

Once you create a Server instance the window.WebSocket object has been mocked by default. Usually the best place for it is beforeEach hook.
If it's not working for you it's better to check the documentation of the library. https://playwright.dev/docs/mock#creating-mocks
For example you can try mocking it manually like this:

import { WebSocket } from 'mock-socket';

test.beforeEach(async ({ page }) => {
  await page.addInitScript(() => {
    window.WebSocket = WebSocket;
  });
});

and create a server with mock: false option later:


test('...', async ({ page }) => {
  const server = new Server('ws://localhost:8080', { mock: false });
});

from mock-socket.

mattoni avatar mattoni commented on June 3, 2024

Hey @Atrue thanks for the response. The issue I had was the WebSocket definition doesn't get passed through the addInitScript(), so the definition is never overwritten. Is there a workaround for that you're aware of? It looks like the stuff in the init script is completely isolated.

from mock-socket.

Atrue avatar Atrue commented on June 3, 2024

You need to try the examples in the documentation and make sure they works, if so you can try mocking the WebSocket the same way, you can even try it without this library, but there is no difference.
As mocking getBattery is the same as the mocking WebSocket it should work in both cases. But if the playwright has difference there you can also try using Object.defineProperty

from mock-socket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.