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

TypeError: Cannot read property '_cookieJar' of null #8613

Closed
bravelincy opened this issue Jun 27, 2019 · 11 comments
Closed

TypeError: Cannot read property '_cookieJar' of null #8613

bravelincy opened this issue Jun 27, 2019 · 11 comments

Comments

@bravelincy
Copy link

🐛 Bug Report

Connect to a socket.io cause an this error:

(node:66998) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_cookieJar' of null
    at resolve (/Users/xxx/node_modules/jsdom/lib/jsdom/living/websockets/WebSoc
ket-impl.js:124:39)

To Reproduce

test('connection', async () => {
  const port = await getPort({ port: getPort.makeRange(8080, 65535) })
  const io = socketIO(port)
  const connection = new Promise(resolve => {
    io.on('connection', resolve)
  })
  const client = socket('http://localhost:' + port)
  await connection
})

Expected behavior

no errors log

@thymikee
Copy link
Collaborator

Have you tried newer version of JSDOM, e.g. https://www.npmjs.com/package/jest-environment-jsdom-fifteen? Jest uses pretty outdated one because of Node 6 support (will be dropped in next major).

@bravelincy
Copy link
Author

Have you tried newer version of JSDOM, e.g. https://www.npmjs.com/package/jest-environment-jsdom-fifteen? Jest uses pretty outdated one because of Node 6 support (will be dropped in next major).

I just tried jest-environment-jsdom-fourteen and errors also logged, but when I register an connect handler to the client-side socket intance, these errors disappears.

@thymikee
Copy link
Collaborator

So it looks like it's not an issue with Jest but with jsdom or the way you use the library in tests. I'm gonna close this issue, as we can't help on that.

@wdoug
Copy link

wdoug commented Jul 8, 2019

@chillyond did you figure this out? I am running into the same issue

@bravelincy
Copy link
Author

@chillyond did you figure this out? I am running into the same issue

Yes, it seems if you listening connection on io instance(server side), and doesn't listening connect on socket instance(client side), then the error will appears. So you can either add a
connect listener or remove the connection listener, hope this helps you.

@cdimitroulas
Copy link

cdimitroulas commented Jul 10, 2019

Hey @chillyond I am running into the same error and jest-environment-jsdom-fourteen also doesn't solve the problem for me.

Could you explain your solution a bit better? Where do you add the connect listener, on the client side or on the server side?

My stacktrace:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_cookieJar' of null                                                                                                                                                                                                       
  at resolve (/home/cdimitroulas/code/headstart/hs-student-web/node_modules/jest-environment-jsdom-fourteen/node_modules/jsdom/lib/jsdom/living/websockets/WebSocket-impl.js:124:39)                                                                                                             
  at new Promise (<anonymous>)                                                                                                                                                                        
  at WebSocketImpl.openingQueues.set.openingQueues.get.then (/home/cdimitroulas/code/headstart/hs-student-web/node_modules/jest-environment-jsdom-fourteen/node_modules/jsdom/lib/jsdom/living/websockets/WebSocket-impl.js:112:94)                                                                                                                                                                       
  at <anonymous>                                                                                                                                                                                      
  at process._tickCallback (internal/process/next_tick.js:189:7)

@bravelincy
Copy link
Author

@cdimitroulas Yes, jest-environment-jsdom-fourteen doesn't solve this problem, my workround is, in brief, the client side's connect listener and the server side's connection listener must all be registered, or all not be registered, it works for me.

@beaniemonk
Copy link

In my case, the transports option was mismatched between client and server, and that also triggered this bug.

@cdimitroulas
Copy link

@beaniemonk can you elaborate on how you fixed that?

@beaniemonk
Copy link

beaniemonk commented Aug 6, 2019

Sure,

So on the server side I had this (short version):

// server
const sio = require("socket.io")({
    transports: ["websocket", "polling"],
});

(This specific configuration is OK for our specific case, but may or may not be a good idea for yours).

On the client side, I did not specify this option, so presumably it was rolling with the default transports: ["polling", "websocket"] instead. When I changed it to match:

// client
const socket = require("socket.io-client")("foo/bar", {
    transports: ["websocket", "polling"],
});

This error went away for me.

This mismatch should not be an issue normally, as far as I know -- it's something specific to the Jest/JSDOM environment.

@chillyond's suggestion of adding a socket.on("connect", ...) event handler unfortunately did not work in my case, but this did for some reason.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants