dalekjs / dalek

[unmaintained] DalekJS Base framework
MIT License
695 stars 63 forks source link

Optional support for async beforeEach() and afterEach() #127

Closed niallo closed 9 years ago

niallo commented 10 years ago

This PR implements Mocha-style optional async support in beforeEach and afterEach functions. This is extremely useful for per-test setup and teardown functions which need to perform IO. For example, creating test users or other data in order for the test to work.

If you don't accept a parameter in your each() function, synchronous semantics are provided - so it's 100% backwards compatible with existing Dalek tests. If your each functions accept a single argument, then async semantics are expected and you can call the callback when you are done.

Here is an example to show what it looks like:

module.exports = {
  options: {
    beforeEach: function(done) {
      console.log("beforeEach");
      done();
    },
    afterEach: function(done) {
      console.log("afterEach");
      done();
    },
  },  
  'Open Google': function(test) {
    test.open('http://google.com').done();
  }   
};    

Want the existing synchronous style? Doesn't require any changes:

module.exports = {
  options: {
    beforeEach: function() {
      console.log("Sync beforeEach");
    },
    afterEach: function() {
      console.log("Sync afterEach");
    },
  },  
  'Open Google': function(test) {
    test.open('http://google.com').done();
  }   
};    
niallo commented 10 years ago

Any feedback on this PR? Would love to see it upstream!

niallo commented 10 years ago

Ping

asciidisco commented 10 years ago

Checked it & love it ;) As written in one of the other PRs, I have to do some build script fixes first, but then it will land in 0.0.9

asciidisco commented 9 years ago

Finally found some time to get this in, sorry for the delay & thanks for the contribution :)