Reputation: 2556
I'm never calling WebSocket directly; it’s a dependency of Asteroid. Why is Jest failing to load it?
Also, this is supposed to be a default create-react-app
test to check the App "renders without crashing". But the app loads fine even though the Jest test says it should crash.
node scripts/test.js --env=jsdom --silent
Output:
FAIL src/routes/App.test.tsx
● 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
Tests: 0 total
Snapshots: 0 total
Time: 1.915s
Upvotes: 5
Views: 13569
Reputation: 10227
The WebSocket
class doesn't exist globally in Node.js, but in browsers it does. You should mock or define this class globally while testing. I've never faced this particular problem, but I think you can try this:
// In App.test.tsx
const WebSocket = require('ws')
global.WebSocket= WebSocket
// Your tests...
Upvotes: 1
Reputation: 444
I have been bugged by Asteroid for a while before I figured out you can specify the websocket you want to use in the constructor like so:
const asteroid = require('asteroid');
const WebSocket = require('ws');
const Connection = asteroid.createClass()
const portal = new Connection({
endpoint: 'ws://localhost:3000/websocket',
SocketConstructor: WebSocket // <-------------- HERE
})
Upvotes: 3