-
-
Notifications
You must be signed in to change notification settings - Fork 768
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
[feature] adds spy.calledImmediatelyBefore and spy.calledImmediatelyAfter #1337
[feature] adds spy.calledImmediatelyBefore and spy.calledImmediatelyAfter #1337
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the pull request. I only have a couple of comments
test/spy-test.js
Outdated
assert.isFunction(this.spy1.calledImmediatelyAfter); | ||
}); | ||
|
||
it("returns true if first call to A was immediately after first to B", function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is minor, but it would make the tests easier to read, if you use A
in both description and implementation. Otherwise readers would have to understand that A
is this.spy1
test/spy-test.js
Outdated
assert.isFunction(this.spy1.calledImmediatelyBefore); | ||
}); | ||
|
||
it("returns true if first call to A was immediately after first to B", function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, A
is this.test1
Thanks for reviewing @mroderick - updated the names in my tests and the tests i stole the template from. |
Purpose (TL;DR) - mandatory
Sometimes you need to be sure that you are calling
a().c()
and nota().b().c()
. When mocking a large library likeknexjs
, being able to ensure the exact order of calls is incredibly helpful.Background (Problem in detail) - optional
When mocking a large library that chains calls, you may end up writing one large mock in the
beforeEach
of the test library as to not duplicate code within your tests. Unfortunately there is no way to ensure that chained calls do not sneak into calls chains currently, as thecalledBefore
andcalledAfter
implementations simply use>
or<
. In order to use a large mock and reduce duplicitous code in test suites when dealing with libraries that chain calls,calledImmediatelyBefore
andcalledImmediatelyAfter
allow you to ensure that no additional modifiers end up between your chained calls.Solution - optional
This solution works just like
calledBefore
andcalledAfter
, but uses strict equality to ensure that the calls to the spies did in fact happen sequentially.How to verify - mandatory
npm install
npm test
(includes 100% test coverage)