admin管理员组

文章数量:1123948

I followed the GooglePay implementation instructions here:

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=".js"
></script>

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>
Share Improve this question edited yesterday Sarah asked yesterday SarahSarah 7032 gold badges9 silver badges24 bronze badges 2
  • Please show where you import google. – Fernando Di Leo Commented yesterday
  • Ah, my bad! I added it to the question – Sarah Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 2

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})

本文标签: google payHow to mock GooglePay in CypressStack Overflow