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
Comments
@warfangle I've been getting similar errors:
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 |
Nock depends on Node http client implementation, but theoretically It would be possible to detect browser and use an alternative implementation. |
https://github.com/substack/http-browserify provides a pretty good shim for browsers. |
@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. |
Fixing this would be great |
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? |
It uses the browserify http client request, which wraps XHR. |
I am writting my tests using karma/browserify and thus http-browserify. As soon as I add
|
Getting the same type of error when trying to use nock in the browser through browserify, should this work as with browserify? |
I get 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. |
1+ |
Experiencing the same issue. Running chai + webmock + karma under phantomjs. |
+1 |
1 similar comment
+1 |
+1 |
1 similar comment
+1 |
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. |
@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". |
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 |
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? |
Same problem here: Using Karma and nock hits the real server. :( Any solutions ?? |
@hyphenbash @miazoom Are you sure that nock is being loaded by the browser? |
@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? |
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 |
@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'] }) |
@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. |
@hyphenbash ha, I've read the code of |
related: #409 |
Thanks for the follow up @pgte |
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:
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) : It somehow just keep going to the real server :( Any other suggestions?? |
@miazoom yes, nock can catch ieomorphic-fetch because when it runs in node it uses the node native HTTP client. |
I am still seeing this with v3.1.1. Running karma + webpack using https://github.com/erikras/react-redux-universal-hot-example
|
@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 ). |
having same issue as @austinmao |
@pgte I'm getting a couple warnings, then an error:
|
@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 |
I have the same problem with 'fs'. |
Not sure if this will break something else, but in Karma.conf you can try this to get around
|
@lecstor Thanks! This is the second time you've helped me with my karma config issues :) 👍 |
@liorbrauer 8) cool, glad I could help! |
@miazoom Did you ever get around to mocking the requests on the browser? |
@miazoom @choonkending having the same issue while following the redux test examples from the doc. |
@JuHwon @miazoom @choonkending |
@alexpanov I'm getting this issue. Did you fix this? |
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! |
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:
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).
The text was updated successfully, but these errors were encountered: