cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.72k stars 3.16k forks source link

Cypress custom commands behave different when run from command line #6773

Closed websaid closed 4 years ago

websaid commented 4 years ago

Current behavior:

When running a test from the gui via npx cypress open and command line via npx cypress run, custom commands behave different and will not work properly in the second case.

Own Cypress Command (cy.collectData)

main_full

VIA npx cypress open:

main_full (1)

-The code work perfect, I get the console.log("OUTPUT:",output) of the cy.collectData before the console.log("RESULT:",result) and i have the whole output object

VIA npx cypress run

main_full (2)

-The RESULT: gets logged before -I get an incomplete object, just the selectors object, the last object a cy.fixture function retrieved... -I get a CypressError: Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.

Desired behavior:

The same behavior from run and open

Test code to reproduce

cy.collectData function call in a before hook:

let allNSC = ["NSC"]
  before(() => {
    for (let i = 0; i < allNSC.length; i++) {
      cy.collectData(allNSC[i], TESTCASE, true).then(result => {
        console.log("RESULT:",result)
      })
    }
  })

Custom Command:

Cypress.Commands.add('collectData', (nsc, content, general) => {
    let promisesToCheck = []
    let proInputData = new Promise((aufloesen, ablehnen) => {
        cy.fixture('../fixtures/nscs/' + nsc + '/' + nsc + '_test_data.json').then((data) => {
            if (!data) { cy.log("cy.collectData failed to collect InputData for NSC: " + nsc); ablehnen("cy.collectData Failed") }
            aufloesen(
                { contactInformation: data.contactInformation, caseInformation: findVal(data, content) }
            )
        })
    })
    let proExresults = new Promise((aufloesen, ablehnen) => {
        aufloesen(undefined)/*
        cy.fixture('../fixtures/nscs/' + nsc + '/' + nsc + '_expected_results.json').then((data) => {
            if (!data) { cy.log("cy.collectData failed to collect expected results for NSC: "+ nsc); ablehnen("cy.collectData Failed") }
            cy.log("asd", data)
            aufloesen(findVal(data, content))
        })*/
    })
    let proUrls = new Promise((aufloesen, ablehnen) => {
        if (!general) {
            cy.fixture('../fixtures/nscs/' + nsc + '/' + nsc + '_urls.json').then((data) => {
                if (!data) { cy.log("cy.collectData failed to collect URL for NSC: " + nsc); ablehnen("cy.collectData Failed") }
                aufloesen(findVal(data, content))
            })
        } else {
            cy.fixture('../fixtures/general/general_urls.json').then((data) => {
                if (!data) { cy.log("cy.collectData failed to collect URL for general"); ablehnen("cy.collectData Failed") }
                aufloesen(findVal(data, content))
            })
        }
    })
    let proSelectors = new Promise((aufloesen, ablehnen) => {
        if (!general) {
            cy.fixture('../fixtures/nscs/' + nsc + '/' + nsc + '_selectors.json').then((data) => {
                if (!data) { cy.log("cy.collectData failed to collect selectors for NSC: " + nsc); ablehnen("cy.collectData Failed") }
                aufloesen(data)
            })
        } else {
            cy.fixture('../fixtures/general/general_selectors.json').then((data) => {
                if (!data) { cy.log("cy.collectData failed to collect selectors for general"); ablehnen("cy.collectData Failed") }
                aufloesen(data)
            })
        }
    })
    promisesToCheck.push(proInputData, proExresults, proUrls, proSelectors)
    Promise.allSettled(promisesToCheck).then(results => {
        let output = {
            inputData: results[0].value,
            expected_results: results[1].value,
            url: results[2].value,
            selectors: results[3].value
        }
        if (results.filter(promise => { return !promise.status.includes("fulfilled") }).length) {
            output.error = results.filter(promise => { return !promise.status.includes("fulfilled") })
        }
        console.log("OUTPUT:", output)  
        return cy.wrap(output)
    })
})

Versions

Cypress 4.2.0 Windows 10 Chrome 80

jennifer-shehane commented 4 years ago

Is there any way you can simplify your example so that we can run the code? Right now this code cannot be run as provided.

jennifer-shehane commented 4 years ago

Unfortunately we have to close this issue as there is not enough information to reproduce the problem. This does not mean that your issue is not happening - it just means that we do not have a path to move forward.

Please comment in this issue with a reproducible example and we will consider reopening the issue.