mondora / asteroid

An alternative client for a Meteor backend
MIT License
734 stars 101 forks source link

ReferenceError: WebSocket is not defined #121

Closed holmrenser closed 7 years ago

holmrenser commented 7 years ago

When trying to create a new connection the following error pops up:

ReferenceError: WebSocket is not defined
    at Asteroid.init (node_modules/asteroid/lib/base-mixins/ddp.js:46:67)
    at node_modules/asteroid/lib/asteroid.js:72:33
    at Array.forEach (native)
    at new Asteroid (node_modules/asteroid/lib/asteroid.js:70:16)
    at repl:1:16
    at REPLServer.defaultEval (repl.js:272:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:441:12)
    at emitOne (events.js:82:20)
infacq commented 7 years ago

unfortunately you have to define on your own if you run it on node environment

Falieson commented 7 years ago

Can someone provide more information about this? I made a stack overflow question about the same issue: https://stackoverflow.com/questions/45684811/jest-referenceerror-is-not-defined

  ● Test suite failed to run

    ReferenceError: WebSocket is not defined

      at Asteroid.init (node_modules/asteroid/lib/base-mixins/ddp.js:46:67)
      at node_modules/asteroid/lib/asteroid.js:72:33
      at Array.forEach (native)
      at new Asteroid (node_modules/asteroid/lib/asteroid.js:70:16)
      at Object.<anonymous> (src/config/asteroid.ts:6:16)
      at Object.<anonymous> (src/routes/Login.tsx:14:18)
      at Object.<anonymous> (src/routes/App.tsx:19:15)
      at Object.<anonymous> (src/routes/App.test.tsx:5:13)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

Test Suites: 1 failed, 1 total

I get this same error when I run jest test:

it('renders without crashing', () => {
  const div = document.createElement('div');
  ReactDOM.render(<App />, div);
});

Someone suggested this, but didn't work for me

const WebSocket = require('ws')
global.WebSocket= WebSocket

it('renders without crashing', () => {

  const div = document.createElement('div');
  ReactDOM.render(<App />, div);
});
holmrenser commented 7 years ago

As far as the undefined websocket issue goes: you should define your own websocket in the class constructor like this:

const asteroid = require('asteroid');
const WebSocket = require('ws');

const Connection = asteroid.createClass()

const portal = new Connection({
    endpoint: 'ws://localhost:3000/websocket',
    SocketConstructor: WebSocket // <-------------- HERE
})

I have also commented this on the stack overflow issue: https://stackoverflow.com/a/45697156/6573438

Falieson commented 7 years ago

@holmrenser what fixed it for me was actually installing your PR: https://github.com/mondora/asteroid/pull/122

The changes to SocketConstructor weren't necessary for me.

 ReferenceError: WebSocket is not defined

      at Asteroid.init (node_modules/asteroid/lib/base-mixins/ddp.js:46:67)

which is caused by (I think) trying to access the global.WebSocket which Jest doesn't have

Your PR declares it, removing the need to search for it in the global

PS @holmrenser would you like to update your answer on SO and I'll approve it?