kharandziuk
kharandziuk

Reputation: 12880

share variables between mocha initializations and actual tests

Look at the sample:

var app
describe('application', function() {
  beforeEach(function(done) {
    app = initialize()
  });

  afterEach(function(done) {
    app.close(done)
  });

  it('some interaction with app', function () {
    ///
  });
});

app is the global variable which is bad from style perspective and makes initialize not so useful. Is there are better way to share app variable?

Upvotes: 1

Views: 877

Answers (1)

zangw
zangw

Reputation: 48356

Per the share behaviours

Mocha currently has no concept of a "shared behaviour" however the "contexts" facilitate this feature.

So for your case, it could be done as below,

describe('application', function() {
  var app;
  beforeEach(function(done) {
    app = initialize()
  });

  afterEach(function(done) {
    app.close(done)
  });

  it('some interaction with app', function () {
    ///
  });
});

BTW, sort of same question is discussed here

Upvotes: 1

Related Questions