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

Async use of verify results in timeout #129

Closed
pellejacobs opened this issue Sep 7, 2016 · 4 comments
Closed

Async use of verify results in timeout #129

pellejacobs opened this issue Sep 7, 2016 · 4 comments

Comments

@pellejacobs
Copy link

pellejacobs commented Sep 7, 2016

When a test with a verify of a method called in a promise fails, it only blocks further execution instead of throwing an error.

eg. running a test with mocha app.spec.js

app.js

var send = require('./module').send
module.exports.sender = function(message) {
  return new Promise(resolve => {
    send('the message')
    resolve()
  })
}

module.js

module.exports.send = function(message) {
  throw new Error("send was not stubbed")
}

app.spec.js

const td = require('testdouble')
const { send } = td.replace('./module')
it('should send', done => {
  const sender = require('./app').sender
  let result = sender().then(() => {
    td.verify(send('the message')) // => executes fine
    td.verify(send('the wrong message')) // => blocks further execution instead of throwing error
    done()
  })
})
@searls
Copy link
Member

searls commented Sep 10, 2016

This is because testdouble.js throws errors on failure as opposed to providing integration with any assertion libraries.

You might consider checking out testdouble-chai for assertion integration with Mocha.

@searls searls closed this as completed Sep 10, 2016
@pellejacobs
Copy link
Author

pellejacobs commented Sep 10, 2016

🤔 I think I'm missing something, I don't think I'm using any external assertion library?

Instead of throwing a failure telling what the problem is (You called send with "the wrong message" instead of the expected "the message"), td just blocks execution. Is this intended behavior? Do you have any advice on how you would test this use case otherwise?

Just to be certain, I tried testdouble-chai. However, I encounter the same problem~~, although even the 'correct' case even blocks execution. (but this is probably a bug from their end)~~

const td = require('testdouble')
const chai = require("chai");
const tdChai = require("testdouble-chai");
chai.use(tdChai(td));
const expect = chai.expect // EDIT: missed expect import here 
const { send } = td.replace('./module')
it('should send', done => {
  const sender = require('./app').sender
  let result = sender().then(() => {
    expect(send).to.have.been.calledWith("the message") // => even blocks execution ¯\_(ツ)_/¯
    expect(send).to.have.been.calledWith("the message wrong")
    done()
  })
})

@searls
Copy link
Member

searls commented Sep 11, 2016

tl;dr I don't think this is an issue with testdouble.js, but rather an issue with Mocha and how it works for asynchronous tests. In general, most uses of testdouble.js we want to promote should be accomplished with completely synchronous tests.

did read

I don't think I know what you mean by "blocks execution". What td.verify does is throw an error when the verification is not satisfied. What looks likely to me is that done is never called, because an exception was thrown.

If you want to use testdouble.js inside a promise then, then you'd be responsible for catching anything thrown in that then with a catch clause, for instance:

let result = sender().then(() => {
  throw new Error("hi")    
  done()
}).catch(done)

Otherwise, I suspect Mocha will not know the test is done and will probably time out. I believe if a mocha test returns a promise, that it'll handle this more gracefully (and probably not require you to use done at all). That said, I don't use Mocha very often.

More broadly, we don't go out of our way to provide integrations to test frameworks with testdouble.js, but what you're describing might be an opportunity for testdouble-chai to improve. I'm not sure what hooks are available.

Finally, it's a shame that Mocha appears to not handle uncaught exceptions gracefully. The same test written with teenytest will detect the uncaught exception and fail immediately and gracefully.

@pellejacobs
Copy link
Author

Great, thank you so much for the detailed answer! Doing awesome work with this library 🙂

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

2 participants