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

npm test results in "ReferenceError: WebSocket is not defined" #10

Open
scottjbarr opened this issue Oct 30, 2016 · 4 comments
Open

npm test results in "ReferenceError: WebSocket is not defined" #10

scottjbarr opened this issue Oct 30, 2016 · 4 comments

Comments

@scottjbarr
Copy link

I'm using react-websocket 1.1.6

This is working in development, but when running npm test on my project I see...

    ReferenceError: WebSocket is not defined

      at new Websocket (node_modules/react-websocket/build/index.js:89:16)
      at node_modules/react/lib/ReactCompositeComponent.js:294:18
      ...

The line in question is...

ws: new WebSocket(this.props.url),

Should ws be a dev dependency?

@stevenpetryk
Copy link

In your test environment, are you sure WebSocket is an available API? WebSocket doesn't seem to be available in Node. Perhaps you could mock a globals.WebSocket object just for your tests. If you need complex mocking, I found this!

@damiangreen
Copy link

I also get this error when using Server Side Rendering

@stevenpetryk
Copy link

@damiangreen you cannot use WebSocket when doing Server Side Rendering, since WebSocket is not available in NodeJS. To get around this, you could do something like:

if (typeof window !== 'undefined') {
  // use WebSocket safely, only in here
}

or, if you're using React, you can place all non-SSR code inside of componentDidMount.

@stevenpetryk
Copy link

@mehmetkose this issue can probs be closed btw—just people misusing the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants