Implement aroundEach #153

Closed
searls opened this Issue Oct 28, 2011 · 8 comments

Comments

Projects
None yet
5 participants

searls commented Oct 28, 2011

It'd be nice to have an aroundEach function, particularly for situations where something needs to be set up and unwound, and you'd like those two things to sit right next to each other in the code.

Here's the example I just wanted to write:

aroundEach(function(spec){
  Timecop.travel(new Date(2011, 10, 28, 11, 45));
  spec.run();
  Timecop. returnToPresent();
});

Not something I'd use everyday, but it can be handy once in a while. (Referenced lib is Timecop.js)

Contributor

ragaskar commented Oct 28, 2011

I like the syntax, but this ability seems like it could be accomplished using a describe block and before/after blocks (although you'd have to use "this" or a closure to preserve variables). At the moment, I'm leaning towards keeping the public interface sparse, so I think I'd want to see an example of something that beforeEach/afterEach isn't capable of doing.

searls commented Oct 28, 2011

I doubt there's anything this could do that a beforeEach + an afterEach couldn't do. It would merely tidy up specs in certain cases.

Contributor

infews commented Feb 11, 2012

Can we close this one Mr. Searls?

searls commented Feb 11, 2012

After you, Mr. Frank.

On Feb 11, 2012, at 2:16, "Davis W. Frank"
reply@reply.github.com
wrote:

Can we close this one Mr. Searls?


Reply to this email directly or view it on GitHub:
pivotal#153 (comment)

infews closed this May 27, 2013

TMaYaD commented Oct 13, 2013

Testing callbacks would require this. aroundEach, and aroundAll too.
Using d3.json to demonstrate the async nature. but could be my own too.

describe('d3.json', function() {
  var some_global = undefined;
  aroundEach(function(spec) {
    d3.json('test.json', function(json) {
      some_global = json;
      spec.run();
      //alternatively
      //spec.run(json)
    });
  });
  it("should get right json", function() {  // function(json) {
    expect(some_global).toEqual({ 'a': 1 });
  });
});

without around, I can't make sure the json is loaded without changing d3.json to be synchronous which might be undesirable.

Contributor

sheelc commented Nov 12, 2013

I think what you have there can be accomplished with an async beforeEach in Jasmine 2.0 (and an afterEach if you had something that needed to run after the spec). Modifying your example:

describe('d3.json', function() {
  var some_global = undefined;
  beforeEach(function(done) {
    d3.json('test.json', function(json) {
      some_global = json;
      done(); // calling done allows the beforeEach to complete and then proceeds with running the spec
    });
  });
  it("should get right json", function() { 
    expect(some_global).toEqual({ 'a': 1 });
  });
});

TMaYaD commented Nov 18, 2013

But d3.json is asynchronous call. So system wont wait for done to be called. the beforeEach would execute and move on to running the spec. done would be called again after the d3.json finishes loading the data.

TMaYaD commented Nov 18, 2013

Never mind. I see that you have implemented arity check in v2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment