question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReferenceError: google is not defined

See original GitHub issue

Not sure if this is related to this repo, or Jest. But I’m getting this error when running my test:

ReferenceError: google is not defined
at GoogleAutoComplete.componentDidMount (src/components/GoogleAutoComplete/index.js:38:31)

I have a script tag before my closing body tag that loads the google API. But apparently that’s not loaded into the test environment.

So I download that google api file and put it in my project directory under test/google.js.

I then tried to configure jest to load that file in my package.json:

{
  "name": "web-app",
  "jest": {
    "setupTestFrameworkScriptFile": "test/google.js"
  }
  ...

I also tried using setupFiles:

{
  "name": "web-app",
  "jest": {
    "setupFiles": [
      "test/google.js"
    ],
    ...

Neither of which seem to be loading the file. I also tried loading a file that doesn’t exist and it didn’t throw any errors, which makes me wonder if create-react-app is even loading jest config from package.json.

What’s the correct way to load google into the test environment? Is this a bug, or am I over locking something?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
gaearoncommented, Oct 25, 2016

This worked for me.

src/setupTests.js

window.google = {
  maps: {
    places: {
      Autocomplete: class {}
    }
  }
};

src/App.js

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {
  componentDidMount() {
    const elem = document.getElementById('autocomplete')
    this.autocomplete = new window.google.maps.places.Autocomplete(elem, { types: ['geocode'] })
  }

  // ...

You’d have to either add some mock methods to the class as you use them, or switch to shallow rendering for testing so that componentDidMount never fires.

7reactions
gaearoncommented, Oct 25, 2016

If you need to access a global, access it explicitly as window.google. This is better than a mix of implicit globals scattered around the code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: google is not defined - Stack Overflow
I just noticed that in Google Chrome's Javascript console: it gives the following error: [blocked] The page at mypage.com/GoogleMapPage.aspx ran ...
Read more >
Uncaught ReferenceError: google is not defined - MSDN
Usually this type of error is due to forgetting to reference a JavaScript library or trying to access a library object before the...
Read more >
ReferenceError:'google' is undefined - Geopointe Help Center
If this error message appears while trying to access our map page, it means that Google is being blocked, either by the browser...
Read more >
google is not defined - ionic v3.9.2, angular v5.2.11
Hello guys, i know this error is pretty common but i still have no idea which part did i do wrong. i already...
Read more >
ReferenceError: google is not defined - Stork's Nest
My task is to create a Google Maps component without using any node module helper.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found