nock: Does not work with browserify

Nock expects http.ClientRequest to be defined; the http module that browserify supplies does not use this internal object. Since ClientRequest is an internal object not typically used in an application space, I’m filing this as a bug – just because you’re CommonJS, doesn’t mean you’re running on node 😃

The error that exposes this issue:

PhantomJS 1.9.2 (Linux) ERROR
        TypeError: 'undefined' is not an object (evaluating 'superCtor.prototype')
        at /tmp/karma-browerify.js:1740
PhantomJS 1.9.2 (Linux): Executed 0 of 0 ERROR (12.249 secs / 0 secs)

I’ve traced it down to line 161 of intercept.js.

I understand if you consider nock to be just-for-node, and that extending nock to support mocking out browserify’s http shim might be out of scope for the project. But it should probably be noted in the README.md that browserify is explicitly not supported (and, to be nice, suggest an alternative).

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 50 (11 by maintainers)

Commits related to this issue

Most upvoted comments

@JuHwon @miazoom @choonkending I’ve been trying to test my axios clients with karma and phantomjs using nock but had 0 success. The client is still hitting the real server. Anyone got around it?

@miazoom @choonkending having the same issue while following the redux test examples from the doc. Did you solve this?

Not sure if this will break something else, but in Karma.conf you can try this to get around fs issues:

webpack: {
  ...
  node : { fs: 'empty' }
}

@miazoom why not try “fetch-mock”

             fetchMock.mock("http://localhost:8888/api/list/1234567/items",{
                 status:200,body:{items:['do something']}
             })

instead of

nock('http://localhost:8888')
          .get('/api/list/1234567/items')
          .reply(400, { items: ['do something'] })

I made https://github.com/algolia/faux-jax/ which is a mock around XHR/XDR with feature detection to support and mimick as many browsers implementations as possible.

Would it be trivial to add it to nock? Let me know.

Also, this issue was resolved, does it means we can mock ajax requests in the browser using nock? Does it supports XDomainRequests? Does it implements the xhr spec?