Skip to content

ADD: axios example - #1711

Merged
paulmelnikow merged 3 commits into
nock:masterfrom
AlphaWong:patch-1
Sep 6, 2019
Merged

paulmelnikow merged 3 commits into
nock:masterfrom
AlphaWong:patch-1

Conversation

@AlphaWong

@AlphaWong AlphaWong commented Sep 6, 2019 •

Copy link
Copy Markdown
Contributor

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

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

Copy link
Copy Markdown
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
Copy Markdown
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?

@AlphaWong AlphaWong Sep 6, 2019 •

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

I updated yours!

@paulmelnikow paulmelnikow left a comment

Copy link
Copy Markdown
Member

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

nockbot commented Sep 12, 2019

Copy link
Copy Markdown
Collaborator

🎉 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