ctimmerm / axios-mock-adapter

Axios adapter that allows to easily mock requests
MIT License
3.47k stars 245 forks source link

Adding new routes for mock not working but existing is working #360

Open yodivanbaped opened 1 year ago

yodivanbaped commented 1 year ago

Hi, I have an issue when trying to add a new mock route. Cannot hit API mock with the new route but existing route is working.

My mock implementation (index.js)

import MockRoutes from './mock-routes'
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'

let routes = MockRoutes

// axios mock method
const mock = new MockAdapter(axios)
const methodMap = {
  GET: 'onGet',
  PUT: 'onPut',
  POST: 'onPost',
  DELETE: 'onDelete'
}

function applyMock (data) {
  data.forEach(d => {
    const params = [d.url]
    switch (d.method) {
      case 'GET': params.push({ params: d.param_values })
        break
      case 'PUT' || 'POST': params.push(d.param_values)
        break
    }
    mock[methodMap[d.method]](...params).reply(d.status || 200, d.response)
  })
}

applyMock(routes)

if (process.env.NODE_ENV !== 'production') {
  window.concatMockRoutes = function (newRoutes) {
    applyMock(newRoutes)
    routes = routes.concat(newRoutes)
    if (process.env.NODE_ENV !== 'production') {
      return routes
    }
  }
}

export default routes

Mock routes (mock-routes.js)

var routes = [
{
    method: 'GET',
    url: '/route/existing',
    response: {
      "code": 200,
      "status": "OK",
      "data": {
         mock: "existing"
       }
    }
},
{
    method: 'GET',
    url: '/route/new',
    response: {
      "code": 200,
      "status": "OK",
      "data": {
         mock: "new"
       }
    }
}]

Currently /route/existing is working but /route/new is not working Is there any solution to this? Anyone also had experienced this before?

Project details: Vue.js version 2.5 Node.js version 14 Axios mock adapter version 1.17.0 Axios version 0.19.2

Thanks before