cypress-example-recipes icon indicating copy to clipboard operation
cypress-example-recipes copied to clipboard

Stubbed requests with route2() all failing when running Cypress cli (npx cypress run)

Open jprealini opened this issue 5 years ago • 6 comments

My cypress framework is testing a Blazor app... I managed to stub fetch calls using the experimentalFetchPolyfill feature. With the new experimentalNetworkStubbing I was able to remove all the polyfill related code, and just build the stubbed request using the new cy.route2() command... All nice and working from the test runner BUT when executing the tests with "npx cypress run" all tests that use stubbed calls are failing

Any ideas?

jprealini avatar Sep 02 '20 15:09 jprealini

Please give us a reproducible example we can run.

Sent from my iPhone

On Sep 2, 2020, at 11:17, Juan Pablo [email protected] wrote:

 My cypress framework is testing a Blazor app... I managed to stub fetch calls using the experimentalFetchPolyfill feature. With the new experimentalNetworkStubbing I was able to remove all the polyfill related code, and just build the stubbed request using the new cy.route2() command... All nice and working from the test runner BUT when executing the tests with "npx cypress run" all tests that use stubbed calls are failing

Any ideas?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

bahmutov avatar Sep 02 '20 15:09 bahmutov

Thanks @bahmutov ... I kept playing around, and found that it fails only if I run using "npx cypress run"... But if I specify a browser (npx cypress run --browser chrome --headless) it works.. I suppose that npx cypress run alone uses electron.. right? Could that explain the problem?

jprealini avatar Sep 02 '20 15:09 jprealini

It could - yes, it uses Electron by default, but we would love to find out what is causing the problem, since other people probably hit it too

bahmutov avatar Sep 02 '20 15:09 bahmutov

Thanks again @bahmutov , the problem is that this is running against a proprietary app in development, and I don't think I can share that... But basically, this is a payments portal built with Blazor. I am stubbing the call to the transactions endpoint in order to populate the transactions grid... this is my "stubTRXrequest" custom command (I suppose you could try to replicate this with a basic blazor grid and populate it with whatever you want)

Cypress.Commands.add('stubTransactionRequest', (fixture) => {
    cy.fixture(fixture).then((fixture) => {
        cy.route2('POST', apiBaseUrl + 'transaction/*/*' + '?', fixture)
    }).as('stubTrx')
    cy.wait(500)
})

And this is where I call the stub custom command from, in my transactions.spec.js


const dataExamples = require("../../data/dataExamples.json");

describe('Transactions', () => {

beforeEach(() => {
        cy.authenticatedVisit()
    })

dataExamples["transactionsPaging"].forEach((paging) => {
            it(paging.pages + ' Transaction Pages when there are ' + paging.trxs + ' TRXs', () => {
                cy.stubTransactionRequest(paging.fixture)
                cy.get('a').contains('Transactions').click()
                cy.get('tr').should('have.class', 'table-row-selectable')
                cy.get('ul.pagination').find('li').should('have.length', parseInt(paging.pages) + 4)
            })
        })
}

This is my "dataExamples.js" file I use as kind of "data driven test" data provider

{
    "trxGridColumns": [
        "Store ID",
        "Terminal ID",
        "Currency",
        "Amount",
        "Payment Method",
        "Date",
        "Status"
    ],
    "transactionsPaging": [
        {
            "trxs": 15,
            "fixture": "15transactions_1",
            "pages": "2"
        },
        {
            "trxs": 25,
            "fixture": "25transactions_1",
            "pages": "3"
        }
    ]
}

And this is the content of the fixture file used to stub the response

{
    "results": [
        {
            "currency": "KHR",
            "amount": 466.83,
            "paymentMethod": "AMEX",
            "status": 3,
            "date": "2020-05-04T18:33:18.7940368+00:00",
            "storeId": "Store1",
            "terminalId": "TER3",
            "merchantId": "Sony Store",
            "action": null,
            "cardNumber": "****-****-****-5192",
            "expirationDate": "2020-02-09T17:54:02.6511614+00:00",
            "customerEmail": "[email protected]",
            "order": {
                "id": "920db1d3-d975-4605-8422-fe7741dce610",
                "description": "Gorgeous Metal Tuna",
                "customerId": "8cd4f22f-2309-41e4-8b37-59f1f8a76696",
                "customerName": "Kevin Schultz",
                "shippingAddress": "925 Toy Hill, Howellberg, Comoros",
                "shippingStatus": "In Progress"
            },
            "authorization": {
                "authorizationCode": null,
                "currency": "KHR",
                "amount": 466.83,
                "authorizationResponse": null,
                "status": 3,
                "date": "2020-05-04T18:33:18.7940368+00:00",
                "operationReference": "d75f2a85-7200-426e-b617-39bdfdf89237",
                "cardCountry": "Tunisia",
                "description": "Gorgeous Metal Tuna"
            },
            "id": "5f29ba486c1a680001772e35",
            "createdAt": "0001-01-01T00:00:00+00:00"
        },
        {
            "currency": "SCR",
            "amount": 241.01,
            "paymentMethod": "PAYPAL",
            "status": 2,
            "date": "2020-03-15T08:24:12.9244762+00:00",
            "storeId": "Store3",
            "terminalId": "TER4",
            "merchantId": "Sony Store",
            "action": null,
            "cardNumber": "****-****-****-0722",
            "expirationDate": "2019-12-02T17:04:14.4038609+00:00",
            "customerEmail": "[email protected]",
            "order": {
                "id": "d224652c-4049-4447-9f36-1e64a79b4220",
                "description": "Awesome Granite Table",
                "customerId": "9f2c4b1b-f0f8-448f-91e7-7b038dd8ad74",
                "customerName": "Kimberly Mohr",
                "shippingAddress": "20154 Lynch Ranch, Tevinville, Georgia",
                "shippingStatus": "In Progress"
            },
            "authorization": {
                "authorizationCode": null,
                "currency": "SCR",
                "amount": 241.01,
                "authorizationResponse": null,
                "status": 2,
                "date": "2020-03-15T08:24:12.9244762+00:00",
                "operationReference": "39337847-ac34-4e0d-87dc-64c5d1482ccb",
                "cardCountry": "Saint Kitts and Nevis",
                "description": "Awesome Granite Table"
            },
            "id": "5f202ac54885560001de7101",
            "createdAt": "0001-01-01T00:00:00+00:00"
        },
        {
            "currency": "ISK",
            "amount": 230.67,
            "paymentMethod": "PAYPAL",
            "status": 6,
            "date": "2020-07-14T17:51:59.8505337+00:00",
            "storeId": "Store4",
            "terminalId": "TER2",
            "merchantId": "Sony Store",
            "action": null,
            "cardNumber": "****-****-****-8794",
            "expirationDate": "2019-12-01T03:48:03.3680203+00:00",
            "customerEmail": "[email protected]",
            "order": {
                "id": "ac68dc92-7ea1-4ae9-b7f4-e6d4b4977311",
                "description": "Handmade Soft Mouse",
                "customerId": "14078168-f81d-4c0d-a2e3-170a00111b09",
                "customerName": "Allen Pagac",
                "shippingAddress": "41710 Kozey Meadows, Stromanfort, Hong Kong",
                "shippingStatus": "Delivered"
            },
            "authorization": {
                "authorizationCode": null,
                "currency": "ISK",
                "amount": 230.67,
                "authorizationResponse": null,
                "status": 6,
                "date": "2020-07-14T17:51:59.8505337+00:00",
                "operationReference": "d6543c81-7b43-4ea2-a375-f8acb85d25d5",
                "cardCountry": "Philippines",
                "description": "Handmade Soft Mouse"
            },
            "id": "5f202ac54885560001de7102",
            "createdAt": "0001-01-01T00:00:00+00:00"
        }
]
}

Thanks!

jprealini avatar Sep 02 '20 15:09 jprealini

Please make a full (even if simple) reproducible example we can run. I see cy.route2 working just fine in https://github.com/cypress-io/cypress-example-recipes/pull/555 but maybe there is something in the your situation that prevents it from working.

bahmutov avatar Sep 09 '20 12:09 bahmutov

Could you try cy.intercept (which is renamed and released cy.route2) please with Cypress v6

bahmutov avatar Nov 25 '20 20:11 bahmutov