skip to Main Content

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

Answers


  1. 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
    
    Login or Signup to reply.
  2. 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).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search