run cypress tests from command line getting => CypressError: Cypress test was stopped while running this command

1.9k Views Asked by At

Cypress version 10.3.0

I have a following test.cy.js, which is async IT block.

///

import loadFileObjects from '../../../support/load_file_objects'
var fixtureUrl = '/test/mydata/'

describe("TC1_getResidentialInfo", () => {
    var load_test_case;
    it('Validate and load all automation feeds', async () => {
        load_test_case = await loadFileObjects.getJsonObj(fixtureUrl + '/CUP-575-TC1-TC.json');
        })
})

support/load_file_objects.js

class load_file_objects{
        //it should accept fixture folder structure and test case name
        //returns json object directly using promise
     getJsonObj(testCasePath){
        // return new Cypress.Promise((resolve, reject) => {
            return new Cypress.Promise((resolve, reject) => {
                cy.fixture(testCasePath).then((data) => {
                    cy.log("Test Data for "+testCasePath+ ": " +JSON.stringify(data))
                    resolve(data)
                })
                // do something custom here
              })
            
            //  var data = data;
            //  return data
        // })
    }
}
export default load_file_objects

Its working fine when I run the same test using cypress open(Test runner UI) Please let me what I am missing here.

1

There are 1 best solutions below

6
On BEST ANSWER

The class instance needs to be created somewhere.

Convention is class names are capitalized. This is the clean way to do it

class LoadFileObjects {
  ...
export default LoadFileObjects

or

import LoadFileObjects from '../../../support/load_file_objects'

it('Validate and load all automation feeds', async () => {

  const loadFileObjects = new LoadFileObjects()
  const load_test_case = await loadFileObjects.getJsonObj(fixtureUrl + '/CUP-575-TC1-TC.json');
})

Requiring the fixture

If the path to your fixture file is fixed and consistent, then you can use require() at the top of the script to do the async/await heavy lifting for you.

const load_test_case = require('./cypress/fixtures/test/mydata/CUP-575-TC1-TC.json')