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

Mock adding third random parameter #749

Open
joelsantosbr opened this issue Jan 30, 2024 · 3 comments
Open

Mock adding third random parameter #749

joelsantosbr opened this issue Jan 30, 2024 · 3 comments

Comments

@joelsantosbr
Copy link

Environment

  • Operating System: Linux
  • Node Version: v20.10.0
  • Nuxt Version: 3.9.3
  • CLI Version: 3.10.0
  • Nitro Version: 2.8.1
  • Package Manager: npm@10.4.0
  • Builder: -
  • User Config: devtools, modules, devServer, srcDir, css, postcss, i18n, runtimeConfig
  • Runtime Modules: @nuxt/test-utils/module@3.10.0, @nuxtjs/i18n@8.0.0
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/spy-use-fetch-is-not-working?file=src%2FuseLogin.nuxt.test.ts

Describe the bug

I'm trying to create a spy for nuxt's composable useFetch and validate what is being passed as a parameter in the call, but for some reason a third random parameter is added
image

Additional context

No response

Logs

No response

@antoinezanardi
Copy link

I can confirm I have the same problem when spying the useFetch method. However, the third parameter on my side is $dk08ZMi4Ri …!

@joelsantosbr
Copy link
Author

joelsantosbr commented Jan 31, 2024

@antoinezanardi I discovered here #567 (comment) that we need to use registerEndpoint to mock the useFetch response, it worked for me, but it is limited, you can't change the responses based on each test, you would need to create a test file to validate an error response and another for success, this suggestion was said here by someone else #551 (comment)

@ennioVisco
Copy link

ennioVisco commented Mar 2, 2024

@antoinezanardi I discovered here #567 (comment) that we need to use registerEndpoint to mock the useFetch response, it worked for me, but it is limited, you can't change the responses based on each test, you would need to create a test file to validate an error response and another for success, this suggestion was said here by someone else #551 (comment)

Sweet technique, I missed that! I actually was also able to change the response, even within a test, in the following way:

// usual imports...
const { responseMock } = vi.hoisted(() => {
  return {
    responseMock: vi.fn().mockImplementation(() => {
      console.log("called base!");
      return { testDataOfResponse: "baseline" };
    }),
  };
});
registerEndpoint("/test/", () => responseMock())

The previous code is added at the top of the test file, while then, in the test:

// it("test name", () => {
      responseMock.mockImplementation(() => {
          console.log("called modified!");
          return { testDataOfResponse: "test-related response" };
      });
// });

You can call the .mockImplementation method as many times as you want within a test :)

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

No branches or pull requests

3 participants