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

Add testing example to README #309

Merged
merged 3 commits into from
Apr 20, 2023

Conversation

theSteveMitchell
Copy link
Contributor

#50 was helpful for me to understand an unexpected issue with testing. Hopefully it'll be helpful to others who have similar issues.

In my case, we are using axois-mock-adapter for mocking api requests. I think this is an extremely common package and pattern. We upgraded apisauce from 1.21.2 to 1.21.4, and all of the mocks stopped working.

@skellock's comment helped us to identify the issue, and when we looked through the code we found that 1.21.3 clones the axios instance which was not happening in 1.21.2. I wasn't able to find anything in release notes about that, and we didn't expect this to change in a minor/patch release

Copy link
Member

@jamonholmgren jamonholmgren left a comment

Choose a reason for hiding this comment

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

Looks good to me!

Copy link
Member

@kevinvangelder kevinvangelder left a comment

Choose a reason for hiding this comment

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

This is good, but I'm thinking it might be a little better to combine the two code blocks into one example and using a diff to show the incorrect versus correct implementation. You can use a codeblock marked diff like so:

import apisauce from 'apisauce'
import MockAdapter from 'axios-mock-adapter'
test('mock adapter', async () => {
  const api = apisauce.create("https://api.github.com")
-  const mock = new MockAdapter(axios)
+  const mock = new MockAdapter(api.axiosInstance)
  mock.onGet("/repos/skellock/apisauce/commits").reply(200, {
    commits: [{ id: 1, sha: "aef849923444" }],
  });
  const response = await api..get('/repos/skellock/apisauce/commits')
  expect(response.data[0].sha).toEqual"aef849923444")
})

Overall I think it would read clearer and has the benefit of clearly indicating the incorrect implementation in red and the correct one in green.

@jamonholmgren
Copy link
Member

Good idea, @kevinvangelder

@theSteveMitchell
Copy link
Contributor Author

Yea, good suggestion @kevinvangelder. I've pushed that update. I wish we could do syntax highlighting with diff, but it is easier to follow with the diff 👍

@jamonholmgren jamonholmgren merged commit 8e35f20 into infinitered:master Apr 20, 2023
@jamonholmgren
Copy link
Member

Thanks for the contribution @theSteveMitchell !! Really appreciate it!

@infinitered-circleci
Copy link

🎉 This PR is included in version 3.0.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

5 participants