Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asserting function calls inside a promise

I'm writing some tests for an async node.js function which returns a promise using the Mocha, Chai and Sinon libraries.
Let's say this is my function:

function foo(params) {
  return (
    mkdir(params)
    .then(dir => writeFile(dir))
  )
}

mkdir & writeFile are both async functions which return promises.
I need to test that mkdir is being called once with the params given to foo.

How can I do this?
I've seen quite a few examples on how to assert the overall return value of foo (sinon-as-promised is super helpful for that) but non about how to make sure individual functions are being called inside the promise.

Maybe I'm overlooking something and this is not the right way to go?

like image 377
Pavel Tarno Avatar asked Feb 13 '26 11:02

Pavel Tarno


1 Answers

mkdir isn't called asynchronously here, so it's rather trivial to test:

mkdir = sinon.stub().resolves("test/dir")
foo(testparams)
assert(mkdir.calledOnce)
assert(mkdir.calledWith(testparams))
…

If you want to test that writeFile was called, that's only slightly more complicated - we have to wait for the promise returned by foo before asserting:

… // mdir like above
writeFile = sinon.stub().resolves("result")
return foo(testparams).then(r => {
    assert.strictEqual(r, "result")
    assert(writeFile.calledOnce)
    assert(writeFile.calledWith("test/dir"))
    …
})
like image 172
Bergi Avatar answered Feb 15 '26 00:02

Bergi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!