Skip to content

ADD: axios example #1711

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

Merged
merged 3 commits into from
Sep 6, 2019
Merged

ADD: axios example #1711

merged 3 commits into from
Sep 6, 2019

Conversation

AlphaWong
Copy link
Contributor

@AlphaWong AlphaWong commented Sep 6, 2019

Hi Nock community,

I would like to add back the knowledge of axios in Readme. Thanks Nock team. It is a very powerful testing package.

Reference

#699 (comment)

Best,
Alpha

README.md Outdated
await axios.get('/test');
t.is(call.isDone(), true);
});
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Thanks for opening this.

Could I suggest using this snippet (assuming it works)? It matches the naming and style of the rest of nock + the readme, doesn't set host which isn't strictly necessary, and adds some comments.

import axios from 'axios'
import nock from 'nock'
import test from 'ava' // You can use any test framework.

// If you are using jsdom, axios will default to using the XHR adapter which
// can't be intercepted by nock. So, configure axios to use the node adapter
// instead.
//
// References:
// https://github.com/nock/nock/issues/699#issuecomment-272708264
// https://github.com/axios/axios/issues/305
axios.defaults.adapter = require('axios/lib/adapters/http')

test('can fetch test response', async t => {
  // Set up the mock request.
  const scope = nock('http://localhost')
    .get('/test')
    .reply(200, 'test response')

  // Make the request. Note that the hostname must match exactly what is passed
  // to `nock()`. Alternatively you can set `axios.defaults.host = 'http://localhost'`
  // and run `axios.get('/test')`.
  await axios.get('http://localhost/test')

  // Assert that the expected request was made.
  scope.done()
})

Also, there's a section "Common issues" in the readme. Would you mind moving this new section there?

I hadn't read those threads before – it's good to know about!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like u idea. I modify my change or u modify my change?

Copy link
Contributor Author

@AlphaWong AlphaWong Sep 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like ur idea. I modify my change or u modify my change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated yours!

Copy link
Member

@paulmelnikow paulmelnikow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@paulmelnikow paulmelnikow merged commit 588cd1f into nock:master Sep 6, 2019
@AlphaWong AlphaWong deleted the patch-1 branch September 7, 2019 01:35
@nockbot
Copy link
Collaborator

nockbot commented Sep 12, 2019

🎉 This PR is included in version 11.3.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

depoulo added a commit to depoulo/nock that referenced this pull request Apr 17, 2023
This was added with nock#1711 but [has changed with axios v1](axios/axios#5277)
mastermatt pushed a commit that referenced this pull request Apr 26, 2023
* fix(README): Update axios example

This was added with #1711 but [has changed with axios v1](axios/axios#5277)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants