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

Does not work with browserify #150

Closed
cacieprins opened this issue Feb 25, 2014 · 50 comments
Closed

Does not work with browserify #150

cacieprins opened this issue Feb 25, 2014 · 50 comments
Labels

Comments

@cacieprins
Copy link

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).

@andrew-luhring
Copy link

@warfangle I've been getting similar errors:

Cannot find module '2LDb9K'
at /var/folders/b3/2dzp4_jd1k77sms22g7vh94h0000gt/T/karma-browerify.js:1

but I'm not running nock-- the reason I'm posting here is because your error, like my error, is calling a file called karma-bro wer ify rather than karma-browserify (karma-broWERify != karma-broWSERify). I'm trying to figure out why it's spelling the name wrong... because I'm wondering if that's causing my/our problems

@pgte
Copy link
Member

pgte commented Mar 25, 2014

Nock depends on Node http client implementation, but theoretically It would be possible to detect browser and use an alternative implementation.
PR welcome :)

@mreinstein
Copy link
Contributor

https://github.com/substack/http-browserify

provides a pretty good shim for browsers.

@bevacqua
Copy link

@pgte this is how visionmedia/superagent does it. https://github.com/visionmedia/superagent/blob/master/package.json#L40-L44

Basically provides two implementations

What I would aim for is something like Sinon's fake server, but one which works in the browser and the server.

http://sinonjs.org/docs/#server

@glassresistor
Copy link

Fixing this would be great

@vvo
Copy link

vvo commented Feb 17, 2015

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?

@pgte
Copy link
Member

pgte commented Feb 18, 2015

It uses the browserify http client request, which wraps XHR.
@vvo If you try it with browsererify and it doesn't work, please reopen this.

@chevdor
Copy link

chevdor commented Mar 13, 2015

I am writting my tests using karma/browserify and thus http-browserify.

As soon as I add var nock = require('nock'); to my test, it breaks as described above and show below:

PhantomJS 1.9.8 (Mac OS X) ERROR
  TypeError: 'undefined' is not an object (evaluating 'superCtor.prototype')
  at /var/folders/ff/pqx4w8zx5yd73j2vq4004r4c0000gn/T/cd5194dbf9ff0644c11b401de24f658d.browserify:21562:0 <- node_modules/browserify/node_modules/inherits/inherits_browser.js:5:0
PhantomJS 1.9.8 (Mac OS X): Executed 0 of 0 ERROR (0.608 secs / 0 secs)

@ryanzec
Copy link

ryanzec commented Mar 25, 2015

Getting the same type of error when trying to use nock in the browser through browserify, should this work as with browserify?

@awlayton
Copy link

awlayton commented Apr 1, 2015

I get TypeError: 'undefined' is not an object (evaluating 'superCtor.prototype') too.

When I apply the changes from browserify/http-browserify#75, that error goes away but the code is making real HTTP requests instead of being mocked.

@jtremback
Copy link

1+

@dmitry
Copy link

dmitry commented Jun 30, 2015

Experiencing the same issue. Running chai + webmock + karma under phantomjs.

@zuripabon
Copy link

+1

1 similar comment
@danillouz
Copy link

+1

@punmechanic
Copy link

+1

1 similar comment
@arjanfrans
Copy link

+1

@dmitry
Copy link

dmitry commented Oct 7, 2015

Looks like it's not possible to run it out of the box on the browser side, just because the browser doesn't have a filesystem.

@punmechanic
Copy link

@dmitry yep. A solution I've done is simply to make all code runnable in node - i.e, using jsdom or by using shallow rendering in react.

For some values of "a solution".

@dmitry
Copy link

dmitry commented Oct 7, 2015

Looks like the "real" solution could be: load a tapes through ajax/websocket to a browser into a memory (could be pouchdb or plain indexeddb, depending on browser support), then ajax mockers should be loaded and those tapes now could be used, but I feel it should be totally another package, lets name it vcr-browser. I don't believe anyone tried to implement similar thing as OSS.

@hiradyazdan
Copy link

Bypassing the fs undefined error, this might be a different issue. But, using karma it still hits the real server, as now it throws an AssertionError comparing the real response returned from the server with the expected mock.

Is there a chance to have a separate package supporting karma, as I don't have an issue when running by mocha?

@miazoom
Copy link

miazoom commented Nov 16, 2015

Same problem here: Using Karma and nock hits the real server. :( Any solutions ??

@pgte
Copy link
Member

pgte commented Nov 16, 2015

@hyphenbash @miazoom Are you sure that nock is being loaded by the browser?

@hiradyazdan
Copy link

@pgte I see what you mean. I think my nock build was incorrect so it was not even being called and it was hitting the real server. Built it again and failing fs when running on phantomjs. Can this module be also extended to work on browsers, like sinon?

@miazoom
Copy link

miazoom commented Nov 18, 2015

@pgte

  • We are trying to intercept calls to localhost and use nock reply, but for whatever reason our call is still going to the real server.
  • We're using phantomjs and karma.
  • We use 'isomorphic-fetch' library to send request to server.
describe('async actions', () => {

      it('creates FETCH_LIST_SUCCESS when fetching todos has been done', (done) => {
        nock('http://localhost:8888')
          .get('/api/list/1234567/items')
          .reply(400, { items: ['do something'] })

        const expectedActions = [
          { type: REQUEST_ITEMS, categor: category },
          { type: RECEIVE_ITEMS,  categor: categor, items: ['do something']}
        ]
        const store = mockStore({}, expectedActions,done)
        store.dispatch(actions.fetchItems(category))
      })
    })

Any help is appreciated

@ali322
Copy link

ali322 commented Nov 18, 2015

@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'] })

@pgte
Copy link
Member

pgte commented Nov 18, 2015

@hyphenbash at least from version 3.0 onwards it should be compatible with browsers (even though the automated tests don't run in a real browser.
Did you try with latest?

@hiradyazdan
Copy link

@pgte I am using the latest. but I can see that @ali322 suggested to use fetch-mock as I am also using isomorphic-fetch, but it shouldn't make a difference as isomorphic-fetch is also for node and browserify, that are supported I guess, or it should?

@pgte
Copy link
Member

pgte commented Nov 18, 2015

@hyphenbash ha, I've read the code of isomorphic-fetch. In the browser it uses the native fetch (or a polyfill), so it's not catched by nock.

@pgte
Copy link
Member

pgte commented Nov 18, 2015

related: #409

@hiradyazdan
Copy link

Thanks for the follow up @pgte

@miazoom
Copy link

miazoom commented Nov 19, 2015

Thank for reponding @ali322 , I've tried fetch-mock, fetchmock and reqwest, non of them works yet - I'll keep trying other modules :(

@pgte I'm not sure about native fetch: I've tried something simpler:

  • created index.js with below content;
  • run "node index.js n"
var nock    = require('nock');
var fetch = require('isomorphic-fetch')

nock('http://www.google.com')
  .get('/')
  .reply(499999999)
  ;

fetch('http://www.google.com/')
.then( function(response) {console.log(response); });

nock was able to intercept the (isomorphic) fetch call and return me 499999999.

We've been strickly follow the example on Redux (apart from mocha--> we use karma) :
http://rackt.org/redux/docs/recipes/WritingTests.html

It somehow just keep going to the real server :( Any other suggestions??

@pgte
Copy link
Member

pgte commented Nov 19, 2015

@miazoom yes, nock can catch ieomorphic-fetch because when it runs in node it uses the node native HTTP client.
I'm afraid I can't say the same for the browser...

@austinmao
Copy link

I am still seeing this with v3.1.1. Running karma + webpack using https://github.com/erikras/react-redux-universal-hot-example

WARNING in ./~/nock/lib/back.js
Module not found: Error: Cannot resolve module 'fs' in /Users/austinmao/Sites/aditive-frontend/node_modules/nock/lib
 @ ./~/nock/lib/back.js 17:7-20

WARNING in ./~/nock/lib/scope.js
Module not found: Error: Cannot resolve module 'fs' in /Users/austinmao/Sites/aditive-frontend/node_modules/nock/lib
 @ ./~/nock/lib/scope.js 17:7-20

@pgte
Copy link
Member

pgte commented Nov 23, 2015

@austinmao looks like a warning only, since the require is in a try-catch block (https://github.com/pgte/nock/blob/master/lib/scope.js#L17 ).
Can you confirm that?

@alixeb
Copy link

alixeb commented Dec 7, 2015

having same issue as @austinmao

@rixth
Copy link

rixth commented Dec 7, 2015

@pgte I'm getting a couple warnings, then an error:

WARNING in ./~/nock/lib/scope.js
Module not found: Error: Cannot resolve module 'fs' in /Users/trix/workspace/project/app/node_modules/nock/lib
 @ ./~/nock/lib/scope.js 18:7-20

WARNING in ./~/nock/lib/back.js
Module not found: Error: Cannot resolve module 'fs' in /Users/trix/workspace/project/app/node_modules/nock/lib
 @ ./~/nock/lib/back.js 17:7-20

ERROR in ./~/mkdirp/index.js
Module not found: Error: Cannot resolve module 'fs' in /Users/trix/workspace/project/app/node_modules/mkdirp
 @ ./~/mkdirp/index.js 2:9-22

@vyorkin
Copy link

vyorkin commented Dec 8, 2015

@pgte getting the same error as @austinmao, @alixeb and @rixth, the problem is this call to mkdirp.sync(dir, opts) => https://github.com/pgte/nock/blob/master/lib/back.js#L175, for now I've just commented it out

@Poplava
Copy link

Poplava commented Dec 8, 2015

I have the same problem with 'fs'.

@subblue
Copy link

subblue commented Dec 19, 2015

I'm also getting the same errors as @rixth and others. Commenting out the mkdirp references in lib/back.js as mentioned by @vyorkin is a temporary hack fix for the moment.
My testing setup is using Webpack via Karma using PhantomJS and Jasmine.

@lecstor
Copy link

lecstor commented Dec 20, 2015

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' }
}

@liorbrauer
Copy link

@lecstor Thanks! This is the second time you've helped me with my karma config issues :) 👍

@lecstor
Copy link

lecstor commented Dec 22, 2015

@liorbrauer 8) cool, glad I could help!

@choonkending
Copy link

@miazoom Did you ever get around to mocking the requests on the browser?

@JuHwon
Copy link

JuHwon commented Jun 3, 2016

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

@alexpanov
Copy link

@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?

@nguyendinhdoan
Copy link

@alexpanov I'm getting this issue. Did you fix this?

@lock
Copy link

lock bot commented Sep 13, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue and add a reference to this one if it’s related. Thank you!

@lock lock bot locked as resolved and limited conversation to collaborators Sep 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests