I am using Nuxt.js as frontend framework and Laravel as api server, and writing some e2e testings with Cypress. I'm trying to reduce asyncData api call by using cy.intercept but failed to sucessfully intercept the api call, my test spec looks like below:
const siteUrl = Cypress.env('site_url')
const apiUrl = Cypress.env('api_url')
describe('Post Test', () => {
beforeEach(() => {
cy.intercept('GET', `${apiUrl}/post`, {
fixture: 'post.json',
})
})
it('should render posts same as mock data', () => {
cy.visit(`/post`)
cy.contains('some posts from mock data')
})
})
and my posts/index.vue look like this:
<template>
<div>
<h1>{{ post.title }}</h1>
<p>{{ post.description }}</p>
</div>
</template>
<script>
export default {
async asyncData({ params, $http }) {
const post = await $http.$get(`${apiUrl}/post`)
return { post }
}
}
</script>
When I run the test, in asyncData hook Nuxt.js will still send actual request to Laravel asking for post data. I've read this issue but still want to ask is there any other ways to intercept api calls from server-side with Cypress?