stalniy / bdd-lazy-var

Provides UI for testing frameworks such as mocha, jasmine and jest which allows to define lazy variables and subjects.
MIT License
162 stars 14 forks source link

Support for async definition #5

Closed alex-di closed 6 years ago

alex-di commented 8 years ago

Hey, folks. It would be nice to do async def, my issue is that I need to save instance cuz it triggers a lot of calcs. Something like that usage:

def "name" (done) -> 

  user = new User 
    name: "Todd"

  name.save (e) -> 
    done(user)
stalniy commented 8 years ago

@alex-di I believe this is impossible because of laziness. Variables doesn't have influence on the tests flow and are created when you access them first time. So, imagine case:

describe('Suite', function() {
  def('user', function(done) {
    var user = new User();
    user.save().finally(done)
    return user;
  });

  it('creates user', function() {
    expect($user).to.have.key('id'); // at this moment "user" variable has been just created and can't stop the execution of the test function which currently is running
  });
});

Even if we do this hard implementation, we will loose laziness and variable's composition which are very handy featutes.

The solution may be in the area of async functions. Something like this:

describe('Suite', function() {
  def('user', async function() {
    var user = new User();
    await user.save();
    return user;
  });

  it('creates user', async function() {
    var user = await $user;
    expect(user).to.have.key('id');
  });
});

As a current workaround I suggest to use beforeEach block which can alter tests flow:

describe('Suite', function() {
  def('user', function() {
    return new User();
  });

  beforeEach(function(done) {
    $user.save(done);
  });

  it('creates user', function() {
    expect($user).to.have.key('id');
  });
});
stalniy commented 8 years ago

Anyway I'm opened to suggestions regarding possible implementations of such behavior