#Cannot find any path matching when mocking using registerEndpoint
3 messages · Page 1 of 1 (latest)
Based on the knowledge sources provided, I can help you with your issue regarding mocking an API endpoint using registerEndpoint in Nuxt.
The problem you're encountering is likely due to the way you're registering the endpoint. According to the Nuxt documentation on testing (https://nuxt.com/docs/getting-started/testing#registerendpoint), registerEndpoint should be used as follows:
import { registerEndpoint } from '@nuxt/test-utils/runtime'
registerEndpoint('/api/auth/recovery', () => ({
// Your mocked response here
}))
In your case, you're using an object with method and handler properties, which isn't the correct format. Instead, you should provide a function that returns the mocked response. Here's how you could modify your code:
import { registerEndpoint } from '@nuxt/test-utils/runtime'
registerEndpoint('/api/auth/recovery', () => {
return {
statusCode: 204,
// Add any other response data you want to mock
}
})
Also, note that setResponseStatus is not necessary when using registerEndpoint. The status code can be included directly in the returned object.
Additionally, make sure that the path you're registering ('/api/auth/recovery') exactly matches the path in your $fetch call.
If you're still encountering issues after making these changes, it's possible that there might be a conflict with end-to-end testing. As mentioned in the Nuxt documentation (https://nuxt.com/docs/getting-started/testing#conflict-with-end-to-end-testing), @nuxt/test-utils/runtime and @nuxt/test-utils/e2e need to run in different testing environments and can't be used in the same file.
If you're using both end-to-end and unit testing functionality, you might need to split your tests into separate files and specify the test environment per-file or use the .nuxt.spec.ts extension for your runtime unit test files.