How to call a test suite from another test case in Cypress?

292 Views Asked by At

The application I am working on requires me to call multiple test suites in the current testcase. I know that we can add the resuable command in suport/commands.js but that is not I am looking for.

testsuiteA.js

  describe(This should be called in another, function({
   .....
   })
  it(TestCase1,function(){....})
  it(TestCase2,function(){....})

I want to call the entire testSuiteA in a new testsuiteB or sometimes want to call only specific test cases of that suite. I tried some ways but I am stuck now.

2

There are 2 best solutions below

2
agoff On BEST ANSWER

Since the describe() and it() blocks are JavaScript functions, you can create a function that calls them and export that function.

// foo.js
export myTests function() {
  describe('Reusable tests', function () {
    // test content here
  });
}

// test-file.js
import { myTests } from "./foo.js" // import the function

myTests(); // run the tests
0
Layla Boyd On

Running one spec inside another is possible with import or require,
see the general idea at How to run all tests in one go

Using require() is preferable if you want to structure testSuiteB:

describe('testSuiteB', () => {
  context('it runs testSuiteA', () => {
    require('testSuiteA.js')
  })
  context('it runs testSuiteC', () => {
    require('testSuiteC.js')
  })
})

Doing it this way means you get a well formatted report, and you can still run testSuiteA independently (no function wrapper is required).