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

Improve tests of .reply() with a synchronous function #1521

Merged
merged 5 commits into from
May 3, 2019

Conversation

paulmelnikow
Copy link
Member

Depends on #1519 as it removes the two tests which are being moved elsewhere in that PR.

@paulmelnikow
Copy link
Member Author

@mastermatt would you be up for reviewing this? All other reviewers welcome too :)

@paulmelnikow
Copy link
Member Author

The drop in coverage may be on account of the two tests removed for #1519. Let's make sure after that is merged that the coverage stays the same.

// The observed behavior is that this returns a 123 status code.
//
// The expected behavior is that this should either throw an error or reply
// with 200 and the JSON-stringified '[123]'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expected the observed behavior, given the current documentation.

Is there a plan to change the expected behavior with a breaking change?

Copy link
Member Author

@paulmelnikow paulmelnikow May 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. It seems very confusing that the 123 status code would override an also-provided 200 status code without giving an error. It doesn't seem helpful either. It seems certain that someone who writes this does so by mistake.

Out of curiosity, is there something in the docs that makes it sound like it behaves this way? I'm game to fix it, breaking change or no. Maybe we can resolve this quickly enough to get it shipped in 11.0!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not from specific examples in the docs, but from an understanding that status, body, and headers can be provided two separate ways: either as separate args to reply or by returning an array from the callback passed to reply. My understanding (presumption?) was that if an array was returned from the callback, it would override other values.

But if that is not explicitly the desired design; I would vote for throwing an error if both were provided because it can be confusing, especially for newcomers to the lib. I've always thought the pattern of "if its an array, then it's a special value, otherwise it's the body" was a bit too magical.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, yea, my assumption was different. It would be good to include tests of all the cases and pick the behavior that seems safest and least confusing.

Let's take a look at the behavior in the context of the changes you're proposing in #1520. We can enable these tests and make sure we've got all the bases covered.

Other than the skipped tests, do these changes look good to you?

.post('/endpoint', exampleRequestBody)
.reply(404, (uri, requestBody) => {
t.equal(uri, '/endpoint')
t.equal(requestBody, exampleRequestBody)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything that asserts these two lines get executed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fortunately, yes: the t.plan(3) means exactly three tap asserts have to be called for the test to pass.

test('reply can take a callback', async t => {
test(
'reply function returning array with status code',
// Seems likely a bug related to https://github.com/nock/nock/issues/1222.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not the desired behavior? #1170

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think it is the desired behavior. However this test doesn't pass. The status code is 202 as expected, but the body is '[202]', while '' is expected.


t.equal(statusCode, expectedStatusCode)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the amount of hand waving that happens around optional data, it's probably worth explicitly asserting the status code and body down in these tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good shout. Updated!

@paulmelnikow paulmelnikow mentioned this pull request May 2, 2019
6 tasks
@paulmelnikow
Copy link
Member Author

Coveralls is reporting a coverage drop, though it looks exactly the same to me. I'm not inclined to worry about it.

@paulmelnikow
Copy link
Member Author

paulmelnikow commented May 2, 2019

@gr2m any objections to my merging this based on @mastermatt's review?

@paulmelnikow paulmelnikow merged commit 3d74a15 into beta May 3, 2019
@paulmelnikow paulmelnikow deleted the test-reply-fn-cleanup branch May 3, 2019 20:57
@gr2m
Copy link
Member

gr2m commented May 3, 2019

post-merge 😄👍

@nockbot
Copy link
Collaborator

nockbot commented May 7, 2019

🎉 This PR is included in version 11.0.0-beta.12 🎉

The release is available on:

Your semantic-release bot 📦🚀

@nockbot
Copy link
Collaborator

nockbot commented Aug 12, 2019

🎉 This PR is included in version 11.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

gr2m pushed a commit that referenced this pull request Sep 4, 2019
- Add several tests for several missing edge cases.
- Rewrite the test for #1222 so it reflects what I believe is the desired behavior.
- Indicate one test where behavior is supported today but seems unnecessary.
- Add one skipped test for a likely bug.
gr2m pushed a commit that referenced this pull request Sep 4, 2019
- Add several tests for several missing edge cases.
- Rewrite the test for #1222 so it reflects what I believe is the desired behavior.
- Indicate one test where behavior is supported today but seems unnecessary.
- Add one skipped test for a likely bug.
gr2m pushed a commit that referenced this pull request Sep 5, 2019
- Add several tests for several missing edge cases.
- Rewrite the test for #1222 so it reflects what I believe is the desired behavior.
- Indicate one test where behavior is supported today but seems unnecessary.
- Add one skipped test for a likely bug.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants