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

Help: Use with Cypress? #322

Open
vincerubinetti opened this issue Nov 17, 2021 · 2 comments
Open

Help: Use with Cypress? #322

vincerubinetti opened this issue Nov 17, 2021 · 2 comments

Comments

@vincerubinetti
Copy link

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
Copy link

Did you find a solution to that?

@vincerubinetti
Copy link
Author

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.

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

2 participants