0

I'm trying to loop through some options and build tests based on those options in mocha. I've set up a simple proof of concept for making dynamic tests based roughly on this gist: https://gist.github.com/cybertk/fff8992e12a7655157ed

I keep getting the error: "TypeError: test.retries is not a function" when I run dynamicSuite.addTest(). I cannot figure out what is causing the error. There doesn't seem to be much documentation for this method of building tests in mocha.

Here's the code:

var dynamicSuite = describe('dynamic suite', function() {
this.timeout(10000);

before( function (done) {
   var a = ['a', 'b', 'c'];
   for(let item of a){
      dynamicSuite.addTest(new common.Mocha.Test('test' + item, function(done){
        done();
      }));
    }
  done();
});

it('this is needed to make sure tests run', function (done) {
  done();
});

after(function(done) {
  done();
});
});//end describe test block
mags
  • 590
  • 1
  • 8
  • 25

1 Answers1

1

Test readability is important. Consider whether programatically generating tests compromises the readability of the tests and/or increases the chances that the tests contain bugs.

That said, this should work:

  describe('dynamic suite', function(){
    ['a','b','c'].forEach(function(letter, index){
      it('should make a test for letter' + letter, function(done){
        // do something with letter
        done();
      });
    });
  });

You are currently adding tests in the beforeEach block, which will execute once per test in the file. So if you added another test in the file all of tests would get duplicated.

The code above works because declaring a test is just executing a function (it(name, test)), so we can just iterate over each input and execute the it function.

Dan
  • 3,229
  • 2
  • 21
  • 34