BAE
BAE

Reputation: 8936

Dynamically Generating Tests of mocha

My codes:

['nl', 'fr', 'de'].forEach(function(arrElement) {
  const var1 = 'var1';
  describe(arrElement + ' suite', function() {
    const var2 = 'var2';
    it('This thing should behave like this', function(done) {
      const var3 = 'var3';
      foo.should.be.a.String();
      done();
    });
  });
});

In the above codes, the describe block will run three times. There are three variables: var1, var2, var3. During the three runs, which variable will be created only once? which variable will be created three times?

Thanks

Upvotes: 0

Views: 182

Answers (1)

SLaks
SLaks

Reputation: 887215

forEach() calls its callback once per array element.

Therefore, your function (and all of its variables and code) will run three times.

Local variables are never shared across multiple calls to their containing function.

Upvotes: 1

Related Questions