Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise support #7

Open
TheLudd opened this issue Aug 17, 2014 · 12 comments
Open

Promise support #7

TheLudd opened this issue Aug 17, 2014 · 12 comments
Assignees
Milestone

Comments

@TheLudd
Copy link

TheLudd commented Aug 17, 2014

Mocha now has promise support. Would it be possible to also add it to mocha-given?
In mocha i can write:

describe 'myPromiseFunction', ->

  beforeEach -> someAsyncSetup()

  it 'shoud return "foo" async', -> 
    myPromiseFunction().then (result) ->
      result.should.equal 'foo'  

It would be awesome if mocha-given supported this:

describe 'myPromiseFunction', ->

  Given -> someAsyncSetup()
  When -> myPromiseFunction().then (@result) =>
  Then -> @result == 'foo'
@TheLudd
Copy link
Author

TheLudd commented Oct 15, 2014

@rendro @tandrewnichols
How about a comment on this idea? =)

@tandrewnichols
Copy link
Collaborator

Is that significantly different than

describe 'myPromiseFunction', ->

  Given -> someAsyncSetup()
  When (done) -> myPromiseFunction().then (@result) => done()
  Then -> @result == 'foo'

?

@TheLudd
Copy link
Author

TheLudd commented Oct 15, 2014

Yes. If your promise is rejected the error is not caught by your code. And writing catch statements leads to a lot of ceremony and space, especially in coffeescript

@tandrewnichols
Copy link
Collaborator

So what happens in mocha when the promise is rejected?

@TheLudd
Copy link
Author

TheLudd commented Oct 15, 2014

The test fails, like if you pass an error to done done(e). And the promise error is a part of the log.

@tandrewnichols
Copy link
Collaborator

And I assume it just hangs in mocha-given . . . ? But you mostly want to avoid having to write

When (done) -> myPromiseFunction()
  .success (@result) => done()
  .error (@result) => done()

Is that correct? (Incidentally, I think that syntax may only work in later versions of coffeescript, so the implementation might depend on what version you're using.)

I'm probably the wrong one to make a decision on this, not only because this isn't actually my library, but also because I almost never use promises, so familiarity there is not as high.

@TheLudd
Copy link
Author

TheLudd commented Oct 15, 2014

Yes I want to avoid that. I think it makes a big difference. Mostly because if it worked, the async call and result fetching could be put on one single line and I think that is a big win.

@TheLudd
Copy link
Author

TheLudd commented Oct 15, 2014

I mean, if i just want to avoid hitting the keyboard I could solve that with snippets in my editor but supporting promises makes the code more readable IMHO

@tandrewnichols
Copy link
Collaborator

Well, I'm a little unclear how mocha-given could hack into the promise mechanism (or how mocha pulls it off). Maybe @rendro will have a better understanding. Or if you know . . . open a PR :)

@rendro
Copy link
Owner

rendro commented Oct 15, 2014

Hey, I guess we would need to look into the mocha code to understand how they implement promises and how we can use that behaviour to support promises in mocha-given.

I have the feeling this could lead to a major rewrite of the module ;)
Let's do this!

@TheLudd
Copy link
Author

TheLudd commented Oct 16, 2014

I think that all you need to do for it to work is to take what is returned from the Given/When function and make sure it is returned in the before funtion that is added to the suite. If it is a promise, mocha will handle it.

@TheLudd
Copy link
Author

TheLudd commented Oct 17, 2014

Guys, I solved this by writing my own version of Given When Then from scratch.... https://github.com/TheLudd/mocha-gwt

@rendro rendro self-assigned this Mar 10, 2016
@rendro rendro added this to the v0.2.0 milestone Mar 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants