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
Mocking Superagent #223
Comments
+1 |
getting the same problem. The error is "Cannot read property 'buffer' of undefined" and looks like it's coming from superagent/node_modules/debug/node.js (using version 0.21.0 of superagent) ..I would say it's a problem with https://github.com/visionmedia/debug but there isn't any instance of "buffer" anywhere in debug.. |
+1 Same problem here. |
I got the mock provided here: http://bl.ocks.org/simenbrekken/b6282f713605b619834f to work by putting it in the But that mock doesn't provide the convenience methods like get/post/put/etc. I had a similar problem promisifying superagent and getting those functions to work. What I ended up doing is not using the convenience methods and instead using the more constructory method. e.g. Instead of
I did
This might be a bit of a pain if you have lots of places to change, but it's been working for me and I don't mind the syntax. |
FWIW, I only had luck with chaining methods for a POST when I cribbed the definition of
|
I'm using superagent
My app code:
|
This mock is also working for me 👍 |
Worked for me too! Thanks for sharing |
Where exactly do I place this __mocks__folder? I'm having ad hard time figuring this out. |
@9Dave9 copy that mock code, put it in |
@browniefed - Thank you. |
Can verify that @pherris' mock works on superagent 1.3.0. I'm using CoffeeScript so had to rewrite it in that - here's a Gist in case it's helpful for anyone. Thanks for this, really helped me out. 👍 |
The mock that people are using here is working well, so closing this issue. |
If you're using the above mock, please note this bug: __setMockError(mockErr) {
mockErr = mockErr;
} Correct it to __setMockError(mockErr) {
// VV
mockError = mockErr;
// ^^
} |
thx @MrNice - updated |
No worries, eslint's static linter saves the day again 👍 |
Hi, Here is my updated version of the mock proposed by @pherris
It allows for promise chaining, but also have stubs on get, post, etc, to be able to verify how many times it has been called, and the argument passed. I hope it can be of help to someone |
when I change |
I found if only mock MyComponent.spec.js import React from 'react'
import superagent from 'superagent-bluebird-promise'
import { shallow } from 'enzyme'
import MyComponent from './'
test('Render the list from ajax', async () => {
superagent.get = jest.fn().mockResolvedValue({
body:[
{id: 1, content: 'AAA'},
{id: 2, content: 'BBB'}
]})
const wrapper = await shallow(<MyComponent/>)
expect(wrapper.find('p')).toHaveLength(2)
}) MyComponent.jsx import React from 'react'
import superagent from 'superagent-bluebird-promise'
export default class MyComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
responseArr: []
}
}
componentDidMount () {
superagent.get(THE_REQUEST_URL)
.then(({ body }) => {
this.setState({responseArr: body})
})
.catch((err) => {
console.log('Oops! ', err)
})
}
render() {
return (
<div>{
this.state.responseArr.map(item => (
<p key={item.id}>{item.content}</p>
))
}</div>
)}
} |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Has anyone had any luck getting a mock for superagent that works?
My code looks like
It yells at me about all sorts of fun stuff like Buffer of undefined. I found a superagent manual mock on here, but when I install it, it complains that request has no method .get. I don't think the Mock works for the latest superagent, or maybe I don't understand how to use it. does anyone have thoughts?
The text was updated successfully, but these errors were encountered: