18

Does axios-mock-adapter only work on requests made with axios?

I have written a component that POSTs to an API (using vanilla XHR, not axios). I'm testing it in Storybook and want to intercept those POST requests since the endpoint doesn't exist yet:

import React from "react"
import { storiesOf } from "@kadira/storybook"
import MyComponent from "./MyComponent"
import axios from "axios"
import MockAdapter from "axios-mock-adapter"

var mock = new MockAdapter(axios)

storiesOf("My Component", module).addWithInfo(
  "Simulator",
  () => {
    mock.onPost().reply(500)
    return <MyComponent />
  },
  {}
)

My component still is trying to hit the API endpoint and I am getting a 404 response - not the expected 500 response.

Does axios-mock-adapter only work on requests made with axios? Does the mock call have to be inside MyComponent?

Thanks.

Alan P.
  • 2,898
  • 6
  • 28
  • 52
  • I think the axios mock adapter doesn't intercept ajax calls from other libraries. If you want to use axios and mock api calls on storybook, I wrote a small tutorial about it: https://medium.com/@rafaelrozon/mock-axios-storybook-72404b1d427b – Rafael Rozon Apr 26 '18 at 17:01

5 Answers5

5

xhr-mock should work for local testing where you probably don't want to make requests across the internet.

Outside of testing, if you are waiting on the real endpoints to be built you could use Mock/it (https://mockit.io) in development. You can claim your own dedicated subdomain and swap it out later for the real one. Disclaimer: this is a side project I recently released and would love any feedback on it!

Vance Faulkner
  • 190
  • 2
  • 8
2

You can use xhr-mock instead of axios-mock-adapter.

Utility for mocking XMLHttpRequest.

Great for testing. Great for prototyping while your backend is still being built.

Works in NodeJS and in the browser. Is compatible with Axios, jQuery, Superagent >and probably every other library built on XMLHttpRequest

import mock from 'xhr-mock';

storiesOf("My Component", module).addWithInfo("Simulator",
() => {

    mock.post('', {
      status: 500,
      body: '{}'
    });

    return <MyComponent />
    },
    {}
)

Additionaly you need to add jquery script in preview-head.html file in storybook

1) https://www.npmjs.com/package/xhr-mock

1

I've started using json-server to intercept API calls. You have to start it in one tab, and start storybook in another, but it is pretty cool.

Alan P.
  • 2,898
  • 6
  • 28
  • 52
1

You can use fetchMock npm module. All XHR call will be mocked with data you provide.

Storybook configuration:

import React from 'react';
import Messages from '../components/messagesList';

import fetchMock from "fetch-mock";
import MESSAGES from './data/messages';

fetchMock.get('/messages', MESSAGES);

export default {
  title: 'Messages',
  component: Messages
};

export const ToStorybook = () => <Messages  />;

ToStorybook.story = {
  name: 'Messages list',
};

The complete tutorial how to do it is on YouTube

Maksym Rudnyi
  • 443
  • 3
  • 7
1

You can use storybook-addon-mock to mock any fetch or XHR request using the addon panel. This package supports

  1. Modify response from the panel and test on the fly.
  2. Modify the status code to verify the error response.
  3. Add a delay time to experience the loading state.
fyasir
  • 2,924
  • 2
  • 23
  • 36