Giter VIP home page Giter VIP logo

Comments (8)

simoneb avatar simoneb commented on June 3, 2024 2

If you're using this library don't mock axios, mock this library via jest

from axios-hooks.

simoneb avatar simoneb commented on June 3, 2024 1

@LiaoJimmy @lmiguelvargasf @tgfischer mocking this library with jest is as easy as:

import useAxios from 'axios-hooks'

jest.mock('axios-hooks')

// in your test
useAxios.mockReturnValue([{ data: { ... } }])

from axios-hooks.

simoneb avatar simoneb commented on June 3, 2024 1

@KristianLonergan in the same way as you would mock a function which is called multiple times, see here.

I appreciate this is not ideal so I'll be looking into #218 that's been opened a few days ago. But, there are ways to mock this easily already.

from axios-hooks.

dkershner6 avatar dkershner6 commented on June 3, 2024 1

Just to throw out there, and it is not mutally exclusive with #218 ...

I really appreciate this library, and I had some more complicated mocking scenarios that required several useAxios hooks to be used with each other, relying on one another. The ordered mocking method above falls down when firing off items in random order and such.

So, I created a library to handle every scenario I (and the rest of my company) needed, and you all are free to use it as well: https://www.npmjs.com/package/axios-hooks-mock

It's an implementation that should work well for both simple and complex scenarios and abstracts most of the testing boilerplate away.

Tagging @simoneb just to let him know.

from axios-hooks.

LiaoJimmy avatar LiaoJimmy commented on June 3, 2024

Thanks. I'll try to mock axios-hooks response array.

from axios-hooks.

lmiguelvargasf avatar lmiguelvargasf commented on June 3, 2024

@LiaoJimmy , how does your code looks after mocking axios-hooks response?

from axios-hooks.

LiaoJimmy avatar LiaoJimmy commented on June 3, 2024

@LiaoJimmy , how does your code looks after mocking axios-hooks response?

I think mock axios-hooks response is not a good solution.
For mock API response, we have better choice like Mirage JS (https://miragejs.com/).
It has many features and better developer experience.

from axios-hooks.

KristianLonergan avatar KristianLonergan commented on June 3, 2024

@simoneb how would you go about mocking axios-hooks when you have multiple instances of useAxios within a file?
For example:

const [{ data: booksData, loading: booksLoading }] = useAxios('/books');
const [{ data: addBookData, loading: addBookLoading }] = useAxios({url: '/addBook', method: 'POST'}, {manual: true});

from axios-hooks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.