axios-mock-adapter icon indicating copy to clipboard operation
axios-mock-adapter copied to clipboard

Help: Use with Cypress?

Open vincerubinetti opened this issue 3 years ago • 2 comments

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.

vincerubinetti avatar Nov 17 '21 17:11 vincerubinetti

Did you find a solution to that?

dimitur2204 avatar Jun 16 '22 11:06 dimitur2204

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.

vincerubinetti avatar Jun 16 '22 13:06 vincerubinetti