[test-runner/dev-server] JSON.stringify in web socket plugin fails on circular references in object
Hello!
I've recently started to migrate our tests to web-test-runner (thank you very much for it!) but I've stumbled across a possible bug when I've added chai-dom plugin - whenever assertion on DOM element fails, this plugin returns to the test runner the DOM element in the message object. It then silently fails and test case hangs until the timeout.
Expected behavior:
When assertion fails, the message gets back to test runner and shows assertion error.
Current behavior:
When assertion fails, the sendMessage in web socket plugin throws; the test hangs after finish until the timeout and shows error:
❌ Browser tests did not finish within 120000ms. You can increase this timeout with the testsFinishTimeout option. Check the browser logs or open the browser in debug mode for more information.
When debugging browser throws:
Uncaught TypeError: Converting circular structure to JSON
Where it breaks:
Call to JSON.stringify
https://github.com/modernweb-dev/web/blob/7aea5048a8274e2d609fed225aa2cde6477d4970/packages/dev-server-core/src/web-sockets/webSocketsPlugin.ts#L65-L71
It might possibly break here too: https://github.com/modernweb-dev/web/blob/7aea5048a8274e2d609fed225aa2cde6477d4970/packages/dev-server-core/src/web-sockets/webSocketsPlugin.ts#L28-L31
Why it breaks:
It seems there is possibility that message may contain circular references. I've seen that there is a mechanism for serialization in code base that solves it, but I'm not sure if this applies to this flow.
This was mentioned in https://github.com/modernweb-dev/web/issues/437#issuecomment-721585812 after the issue got closed.
I could try contributing a fix (with a bit of guidance where it should be fixed - in plugin on dev server side or in test runner) 😃
At the least we should have a try catch for this, but it would be nice if we can handle these messages in a proper way as well. The try catch can be added in the code you linked.
It looks like the problem is, actually, not in those mentioned places but rather in Mocha: https://github.com/mochajs/mocha/issues/4910