ctimmerm / axios-mock-adapter

Axios adapter that allows to easily mock requests
MIT License
3.42k stars 241 forks source link

Help: Use with Cypress? #322

Open vincerubinetti opened 2 years ago

vincerubinetti commented 2 years ago

I have a Vue project set up with Vue CLI, with axios, axios-mock-adapter, and cypress:

{
  "dependencies": {
    ...
    "axios": "^0.23.0",
    "vue": "^3.0.0",
    ....
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.15",
    "@vue/cli-plugin-e2e-cypress": "^5.0.0-rc.1",
    "@vue/cli-plugin-eslint": "~4.5.15",
    "@vue/cli-plugin-router": "~4.5.15",
    "@vue/cli-plugin-typescript": "~4.5.15",
    "@vue/cli-plugin-unit-jest": "~4.5.15",
    "@vue/cli-plugin-vuex": "~4.5.15",
    "@vue/cli-service": "~4.5.15",
    "@vue/compiler-sfc": "^3.0.0",
    "@vue/eslint-config-prettier": "^6.0.0",
    "@vue/eslint-config-typescript": "^7.0.0",
    "@vue/test-utils": "^2.0.0-0",
    "axios-mock-adapter": "^1.20.0",
    "cypress": "^9.0.0",
    ...
  }
}

I'm able to get axios-mock-adapter to work with the Jest unit tests, but not with the Cypress e2e tests. In Cypress, the calls are not intercepted.

I'm setting my tests up in a normal way like

import axios from "axios";
import MockAdapter from "axios-mock-adapter";

new MockAdapter(axios).onPost(someUrl).reply(200, someResponse);

And in my normal api-calling code, I'm doing normal requests like

import axios from "axios";

export const getSomeData = async () => {
  const response = await axios.post(someUrl, null, someParams);
  // blah blah
}

My best guess is that there's some separation between the main app code and the Cypress test code, where they're not referring to the same global instance of Axios.

166 may be related. Any insight or help is appreciated.

dimitur2204 commented 2 years ago

Did you find a solution to that?

vincerubinetti commented 2 years ago

Unfortunately not.

Fwiw I migrated away from this library to MSW and haven't looked back. Seems to be a more robust and "correct" way to do mocking.