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

obj instanceof http.IncomingMessage hard to create tests for #160

Open
twk-b opened this issue Mar 24, 2015 · 2 comments
Open

obj instanceof http.IncomingMessage hard to create tests for #160

twk-b opened this issue Mar 24, 2015 · 2 comments

Comments

@twk-b
Copy link

twk-b commented Mar 24, 2015

} else if (obj instanceof http.IncomingMessage) {

} else if (obj instanceof http.IncomingMessage) {

that line requires that you really have an instanceof http.IncomingMessage which is not really easy to create in a mock, and then randomly the code runs differently, even though you are pass something that looks very very similar to a request ;)

maybe replace with something like:
} else if (obj.body && obj.method && (obj.method === 'POST' || obj.method === 'PUT')) {

@ljharb
Copy link
Collaborator

ljharb commented Mar 24, 2015

That's a strictly weaker test, that converts a prototype check to a duck type check. I'm not sure how I feel about that.

Typically, mocks themselves are a code smell in tests, indicating that there's a coverage gap. However, http.IncomingMessage doesn't seem that hard to mock:

var http = require('http');
var msg = new http.IncomingMessage();
msg.method = 'POST';
msg.body = { data: true };

In addition, please note

form.parse(obj, function (err, fields, files) {
which does require an IncomingMessage object.

@twk-b
Copy link
Author

twk-b commented Mar 24, 2015

Really appreciate the library. Just switched to using .handle() from using
custom code. Figured it would just work (with previous tests in place), but
I found that line while digging into the failure.

I agree it's a slower check, but it is currently checking something that is
not required (although maybe intended). That path should be the typical
flow, so the performance hit is not real (would typically be done later
anyways).

Checking for a type where it is required makes sense. Not sure if formidable is
used all over, but maybe handle() should expect a req.body() if that is
what is required, simplifying the complexity of this recursive function and
then remove the dependency on formidable.

Your suggestion works to fix our tests. Close this as you see fit.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants