0

(all this example code is coffeescript)

I have a generic promise like so:

describe "foo", () ->
  it "foo", () ->

    p = new Promise (r) -> r(1)
    p.then (x) ->
      console.log("promise run")
      expect(x).toEqual(2)

This is expected to fail, but it doesn't. The expectation is never hit and nothing is logged either.

From the jasmine team's blog post on the topic it seems I can write something like this:

p.then (x) ->
  expect(x).toEqual(2)
  done()

but it has the same effect.

The blog post recommends the mock-promises library, which I used to write this:

p.then (x) ->
  console.log x
  expect(x).toEqual(2)
MockPromises.executeForPromise(p);

but it has the same effect (the expectation is never hit)

8
  • If your test runner still doesn't accept promises as return values in 2016, it might be time to change it (or just update?)
    – Bergi
    Sep 22, 2016 at 19:57
  • Protip: p = Promise.resolve 1
    – Bergi
    Sep 22, 2016 at 19:57
  • @bergi, did you see i already posted an answer for myself? It is possible i was just missing something very minor. Anyway do you have a suggestion for a better testing library than Jasmine? Sep 22, 2016 at 20:14
  • I believe your answer only times out instead of logging the exception, right? I'm not an expert on the market, but I know that your first snippet works in Mocha.
    – Bergi
    Sep 22, 2016 at 20:22
  • It was not timing out. Just skipping the then call entirely and saying it passed. Thanks for the recommendation to try mocha though Sep 22, 2016 at 21:50

1 Answer 1

0

Instant gratification:

it's an easy fix:

all I needed to do was pass done as an argument to the it function:

describe "foo", () ->
  it "foo", (done) ->
    p = new Promise (r) -> r(1)
    p.then (x) ->
      console.log x
      expect(x).toEqual(2)
      done()

Thanks to a suggestion in the comments I should also add

.catch(done)

at the end so that failed cases don't just time out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.