lensesio / cypress-websocket-testing

Test WebSocket connections with Cypress
Apache License 2.0
83 stars 12 forks source link

JavaScript sample configuration #13

Closed rizkysyazuli closed 3 years ago

rizkysyazuli commented 3 years ago

The JS example in the README is missing the config and options. How do I translate these from TS to JS? Could use abit more details on this.

const config: WebSocketSubjectConfig<IMessage> = {
  url: "ws://localhost:8080/"
};

let options: Partial<StreamRequestOptions<IMessage>>;

Here's what i did. I don't think I'm doing it right though. Coz it gives me an error.

import { StreamRequestOptions } from '@lensesio/cypress-websocket-testing';
import { WebSocketSubjectConfig } from 'rxjs/webSocket';

const config = WebSocketSubjectConfig({
  url: 'ws://localhost:8888/ws'
});

// i just assume this is where i put the request payload
const options = StreamRequestOptions({
  operation: 'login',
  payload: {
    data: {
      ...
    }
  }
});

Here's the error from Cypress:

> _webSocket.WebSocketSubjectConfig is not a function

Or when i changed the import from WebSocketSubjectConfig to webSocket as seen in the RxJS doc.

> _cypressWebsocketTesting.StreamRequestOptions is not a function
dsebastian commented 3 years ago

Hi @rizkysyazuli , when switching from TS to JS, the types disappear. So the options/config are plain objects that you just need to pass to the request: // see rxjs possible config values const config = { url: 'ws://localhost:8081', ...etc }; // see lib options const options = { streamTimeout:2000, takeWhileFn: (msg)=>!!msg }; cy.streamRequest(config, options).then(results => { expect(results).to.not.be.undefined; })

rizkysyazuli commented 3 years ago

ah. i see @dsebastian but it's still not clear where i should put the request payload.

thx

dsebastian commented 3 years ago

If you just need to send a message(payload) on connection open, you have the startUpMessage property: https://github.com/lensesio/cypress-websocket-testing/blob/master/examples/cypress/integration/examples/streamRequest.spec.ts#L66

rizkysyazuli commented 3 years ago

ah.. yes @dsebastian i mean message. sorry. i'm also new with websocket. but yeah. i see the data now. thx!

so in case anyone's stumbled upon the same problem. here's my somewhat working code.

const config = {
  url: 'ws://localhost/ws'
};

const options = {
  startUpMessage: {
    data:  { ... }
  }
};

describe('Page', () => {
  it('Can do stuff', () => {
    cy.streamRequest(config, options).then(response => {
      // do some checks
    });
  });
});