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

Testing in Create React App #21

Closed
allengordon011 opened this issue Apr 10, 2018 · 3 comments
Closed

Testing in Create React App #21

allengordon011 opened this issue Apr 10, 2018 · 3 comments

Comments

@allengordon011
Copy link

I built my app starting with the convenient create-react-app. It comes with Jest built in for testing. However, I can't run any tests because of the following error:

ReferenceError: WebSocket is not defined
      
      at Object.<anonymous>.module.exports.r.open (node_modules/sockette/dist/sockette.js:1:508)
      at new Object.<anonymous>.module.exports (node_modules/sockette/dist/sockette.js:1:745)
      at new NTWebSocket (src/server.js:14:15)
      at Object.<anonymous> (src/index.js:22:17)
      at Object.<anonymous> (src/test/App.test.js:21:14)
@only-cliches
Copy link

only-cliches commented Apr 22, 2018

You can't run websocket libraries in NodeJS. This isn't a limitation of sockette but of NodeJS itself, it just doesn't provide the same WebSocket api as browsers.

You need to adjust your tests or your application to use a different or emulated code path depending on the environment it's in.

Something like this:

if (typeof WebSocket !== "undefined") {
   // Do real WebSocket stuff in the browser
} else {
   // We're in NodeJS, no WebSocket API!
}

@lukeed
Copy link
Owner

lukeed commented May 2, 2018

What @ClickSimply said is correct. There's no WebSocket built-in within Node.js -- you have to either add the conditional check, or include a mock/stand-in for your tests.

You can use something like ws or uws as your Node.js version of WebSocket. Here's a usage example from the ws README.

Personally, I haven't tested Sockette extending from WebSocket replacement (since this is a browser module). But if I ever get around to writing tests for this library, I'd use either of those replacements to do so.

Hope that helps!

@lukeed lukeed closed this as completed May 2, 2018
@allengordon011
Copy link
Author

Thanks @ClickSimply , I only saw your response after this issue was closed. Thanks also to @lukeed.

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