lensesio / cypress-websocket-testing

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

Cannot read properties of undefined #32

Open LeandroPereiraMB opened 2 years ago

LeandroPereiraMB commented 2 years ago

I'm trying to get messages from a WebSocket to make assertions using Cypress. I used the example from the website [link] (https://github.com/lensesio/cypress-websocket-testing/blob/master/README.md) to do the automation.

Basically, I made a post to a Rest service and then I listen to WebSocket to see if any new messages are displayed.

The first statement passes, but the second does not. Cypress displays a message saying: "Cannot read properties of undefined (reading 'payload').

The console.log command does not print anything to the console. I think the return message from the WebSocket is not being stored in the "result" variable.

It looks like cypress is not reading all messages sent by WebSocket. Could anyone tell me what's wrong with my code?

`///

import { addStreamCommands } from '@lensesio/cypress-websocket-testing';

addStreamCommands();

describe('Should create investment ', () => {

it('Should create investment type commitment', () => {

    const ws = 'wss://xxxx.xxxx.xxx.xx/api/v1/ws'

    cy.request({
        method: 'POST',
        headers: {
            Authorization: `Bearer ${'xxxxxxxxxxxxxxxxxx'}`},

        url:'https://xxxx.xxxx.xxx.xx/api/v1/investment',
        body:{
            "projectId": 40,
            "quoteAssetQuantity": "5100",
            "investmentType": "Commitment"
        }
    })
    cy.streamRequest(ws).then(result => {
        console.log(result)
        expect(result).to.not.be.undefined;
        expect(result[0].payload.InvestmentId).to.eq('906')

    })
})

})`

image

image

image