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

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

Open
yodivanbaped opened this issue Jan 18, 2023 · 0 comments
Open

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

yodivanbaped opened this issue Jan 18, 2023 · 0 comments

Comments

@yodivanbaped
Copy link

yodivanbaped commented Jan 18, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant