That example is misleading, spreading possible misunderstanding of it all.
Why would you wrap axios when you can use cy.request?
Another level of misleading would be wrapping your source code with cy.task (you can wrap any node code with it) the code which makes the api call to do the GET request. Instead you should use Cypress as the API client to do that GET request.
However, let's suppose you have used cy.request for the above in 1 test, you are confident that the feature works. Now you want to accelerate other tests that need a certain state, using that feature. Then it's perfectly fine to wrap source code, which might allow backdoor shortcuts, with cy.task. (Still not ok to wrap an axios call that does the same thing as cy.request, unless somehow axios is faster, which probably isn't)
This is the analogy of UI login vs backdoor authentication. Do 1 UI login, get confidence that login works. In other tests, use a faster, backdoor way to get to the logged-in state.
module.exports = (on, config) => {
on('task', {
getVerificationCode(args: { userId: string; password: string; }) {
const result = await axios.request({
method: 'GET',
url: '/api/getVerification'
params: {
userId: args.userId
},
body: {
password: args.password
}
});
expect(result.status).to.eq(200); // Can make assertions too!
return result.data;
},
});
}