简体   繁体   中英

How to call specific test cases from one file to another file

I'm new to Mocha. I want to call Test case 2 from a.js file to b.js like import the test case because of same test cases repeatable in the b.js file. Example

File a.js :
describe('Sample a',function(){
        it('Test case1',function(done){
          console.log('Testing test case1');
        });
     it('Test case2',function(done){
        console.log('Testing test case2');
     });
     it('Test case3',function(done){
        console.log('Testing test case3');
     });
  });

File b.js:
describe('Sample b',function(){
     it('Test case2',function(done){
     console.log('Testing test case2');
      });
 });

Kindly provide the solution for this problem.

Thanks

There's no facility in Mocha to allow one test ( it ) to invoke another test or to allow a suite ( describe ) to invoke a test in another suite.

When you have tests that share logic, the solution is the same as any other two pieces of JavaScript code that share logic: refactor the code so that the shared logic is moved to a function that can be called, and call it from the code that needs it. Since your tests are in different files, you are going to have to import the shared code in your test files. You can use a loader like RequireJS or SystemJS to load the code at runtime, or use a bundler like Webpack or Browserify to pack your modules into a single bundle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM