1

I followed the GooglePay implementation instructions here: https://developers.google.com/pay/api/web/guides/tutorial

Now I'm trying to test out the functionality via Cypress for my website and I'm getting an error

ReferenceError: google is not defined at googlePayClient

I am using Vue 3 and creating my client that I'm trying to mock in the store like so:

googlePayClient(_, getters) {
 return new google.payments.api.PaymentsClient({environment});
},

I am importing google in index.html like so:

<script async
 src="https://pay.google.com/gp/p/js/pay.js"
></script>
asked Jan 9, 2025 at 21:16
2
  • Please show where you import google. Commented Jan 9, 2025 at 21:39
  • Ah, my bad! I added it to the question Commented Jan 10, 2025 at 14:29

1 Answer 1

1

Mocks should be created in the test not the app, since you do not want to mock when a real user is accessing the app.

The <script> to load the google module places it as a global variable on the window object, so your test should do something like this:

cy.window().then((win) => { // "win" is the app window object
 const google = win.google; // get the instance of google loaded via script
 // now make your mock...
 return new google.payments.api.PaymentsClient({environment})
answered Jan 11, 2025 at 2:30
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.