Comments (6)
Got it ! maybe this should be added somewhere in the readme but basically everything I said was wrong. The issue was that in this specific test I had this setup for another test
jest.useFakeTimers();
removing the line allows the client to connect to the server.
I realised this when I simply created a new action_cable_2.js
and action_cable_2.test.js
file using the code I provided to you (that didn't have the jest.useFakeTimers();
line since I didn't think it was relevant to this test [no timers to test in this function]) and noticed it simply ran (not with the right error but it was running as intended).
Now I do still have an issue that I can't seem to find any documentation on how to emulate a connection refused (like an origin header not allowed on the server) I wan work around that but if ever you have an idea of how to do this I'd be open to hearing it. I'll close my ticket for now since I found the issue with my tests.
Thanks for checking in on my issue !
from jest-websocket-mock.
Oh wow, thanks for your write up!!
The fact that mock-socket uses timeouts really makes it harder to write some tests
Would you be open to raising a quick PR that would add a small warning about mock timers to the readme?
Thanks again!
from jest-websocket-mock.
Hmmmm, looks like your mock might not be picked up correctly.
Does it work if you manually mock out the ws module using jest.mock
in your test file?
Also, keep in mind that mock-socket doesn’t actually implement the full feature set of the ws package, so you’ll need to make sure you’re just relying on methods that are part of the spec.
from jest-websocket-mock.
Do you mean if I remove __mocks__/ws.js
and simply do a jest.mock('ws');
in the action_cable.test.js
?
EDIT:
Just tried and I'm getting the same timeout error in my test
which makes me think it's picking not using the real ws
module when running the test because when I don't mock I get this error
Error: connect ECONNREFUSED 127.0.0.1:1234
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1054:14) {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 1234
}
which I think is simply due to me not having an actual WebSocket server running on port 1234
EDIT2:
I've modified the mock-socket
dist folder file mock-socket.js
to add a console log on the WebSocket client creation and it printed out in the test so I do think it's calling to right mock-socket
WebSocket
constructor
// Line 1245
var WebSocket$1 = (function (EventTarget$$1) {
function WebSocket(url, protocols) {
EventTarget$$1.call(this);
console.log('test'); /* Added this line */
this.url = urlVerification(url);
protocols = protocolVerification(protocols);
this.protocol = protocols[0] || '';
this.binaryType = 'blob';
this.readyState = WebSocket.CONNECTING;
// ...
with the following output when running the test
npm test tests/classes/action_cable.test.js
> [email protected] test /Users/jwoodrow/Mooncard/space-labs-node
> TZ='Asia/Shanghai' jest "tests/classes/action_cable.test.js"
console.log node_modules/mock-socket/dist/mock-socket.js:1248
test
FAIL tests/classes/action_cable.test.js (5.913s)
_connect
when WebSocket server refuses the connection
✕ rejects with error and calls _disconnected (5018ms)
● _connect › when WebSocket server refuses the connection › rejects with error and calls _disconnected
: Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error:
166 | });
167 |
> 168 | test('rejects with error and calls _disconnected', async () => {
| ^
169 | expect.assertions(1);
170 | let connect = actioncableInstance._connect();
171 | await connect;
at new Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
at Suite.<anonymous> (tests/classes/action_cable.test.js:168:5)
● _connect › when WebSocket server refuses the connection › rejects with error and calls _disconnected
expect.assertions(1)
Expected one assertion to be called but received zero assertion calls.
167 |
168 | test('rejects with error and calls _disconnected', async () => {
> 169 | expect.assertions(1);
| ^
170 | let connect = actioncableInstance._connect();
171 | await connect;
172 | expect(disconnectedSpy).toHaveBeenCalled();
at Object.<anonymous> (tests/classes/action_cable.test.js:169:14)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 6.034s
Ran all test suites matching /tests\/classes\/action_cable.test.js/i.
npm ERR! Test failed. See above for more details.
from jest-websocket-mock.
After some more research and validating that mock-socket
is correctly used in both the jest test file and the class file I think I've found a possible explanation but no idea how to circumvent this issue.
thoov/mock-socket#291 (comment)
The gist of it is that mock-socket
server and client need to be run in the same file/script to be able to find one another 🤔 I'm not really advanced enough into node and jest to know how to do this but maybe modifying the code so that the mock-socket
client constructor is called in the same file as the server is (in the jest-websocket-mock.cjs.js
file for example) and then just mocking the ws
module using jest-websock-mock
in place of mock-socket
might do the trick.
I'm going to see how I might accomplish this
EDIT:
I don't seem to be getting any kind of result or luck doing this, and in the process of doing this ended up changing all my require
code into import from
which made it so now
// tests/__mocks__/ws.js
export { WebSocket as default } from "mock-socket";
no longer gives me a TypeError: WebSocket is not a constructor
error (which was the reason I wrote the mock file using requires instead.
I can't seem to wrap my head around why this isn't working for me
from jest-websocket-mock.
I've updated the PR 👋
from jest-websocket-mock.
Related Issues (20)
- Usage with NodeJS and ws package HOT 3
- Use of https://github.com/YuzuJS/setImmediate HOT 3
- ArrayBuffer & Binary messages? HOT 2
- Timeout error running simple test in jest HOT 3
- Is there a way to simulate a lost connection? HOT 1
- Using JWM with partysocket causes Jest error: `TypeError: ports is not iterable` HOT 2
- server.error() removes the server from the urlMap, and it cannot be reconnected to HOT 3
- Add the possibility to pass mock-socket server options
- Add ability to flush pending message queue HOT 5
- Cannot assign to read only property 'readyState' of object '#<WebSocket> HOT 2
- Using QueryParams in ws url doesn't work HOT 2
- Open handles HOT 1
- require error HOT 1
- server.connected and server.closed are usable only for the first connection HOT 1
- Does it also have support with cypress HOT 1
- Jenkins build failure after upgrade to v2.3.0 HOT 4
- Does not work with React <= 16.8.6 HOT 1
- Jest encountered an unexpected token: jest-websocket-mock.cjs.js HOT 4
- `jest-websocket-mock` only support a single client connection
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jest-websocket-mock.