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.

Specifying maxBounds causes mocha tests to explode with "Invalid LatLng object: (NaN, NaN)"

See original GitHub issue

Using Leaflet 0.7.7 and jsdom 9.4.2, I’m unable to successfully execute a unit test on L.map.

I’m not using a test runner–just plain ol’ mocha under Node.

$ mocha --require test/support/bugReportSetup.js --compilers js:babel-register --require babel-polyfill test/unit/components/MaxBoundsBugTest.jsx


  MaxBoundsBug
    when maxBounds is provided
      1) mounts without problems


  0 passing (898ms)
  1 failing

  1) MaxBoundsBug when maxBounds is provided mounts without problems:
     Error: Invalid LatLng object: (NaN, NaN)
      at Object.L.LatLng (node_modules/leaflet/dist/leaflet-src.js:1122:9)
      at Object.L.Projection.SphericalMercator.unproject (node_modules/leaflet/dist/leaflet-src.js:1403:10)
      at Object.L.CRS.pointToLatLng (node_modules/leaflet/dist/leaflet-src.js:1439:26)
      at L.Map.L.Class.extend.unproject (node_modules/leaflet/dist/leaflet-src.js:1930:27)
      at L.Map.L.Class.extend._limitCenter (node_modules/leaflet/dist/leaflet-src.js:2279:15)
      at L.Map.include.setView (node_modules/leaflet/dist/leaflet-src.js:8687:17)
      at L.Map.L.Class.extend.initialize (node_modules/leaflet/dist/leaflet-src.js:1545:9)
      at new NewClass (node_modules/leaflet/dist/leaflet-src.js:229:20)
      at Object.L.map (node_modules/leaflet/dist/leaflet-src.js:2318:9)
      at Context.<anonymous> (MaxBoundsBugTest.jsx:13:9)

Here are the gory details…

$ npm show leaflet version
0.7.7

$ npm show jsdom version
9.4.2
// File: MaxBoundsBugTest.jsx

import { expect } from 'chai'

import L from 'leaflet'

describe('MaxBoundsBug', () => {
  context('when maxBounds is provided', () => {
    const maxBounds = [
      [-80, -180], // SW
      [90, 180],   // NE
    ]

    it('mounts without problems', () => {
      L.map('test', {
        center: [30, 0],
        zoom: 13,
        maxBounds: maxBounds
      });
    })
  })
})
// File: bugReportSetup.js

// From https://github.com/airbnb/enzyme/blob/master/docs/guides/jsdom.md

import { jsdom } from 'jsdom'

const EXPOSED_PROPERTIES = ['window', 'navigator', 'document'];

MAIN: {
  global.document = jsdom('<div id="test"></div>')
  global.window = document.defaultView

  Object.keys(document.defaultView).forEach((property) => {
    if ( global[property] === void 0 ) {
      EXPOSED_PROPERTIES.push(property)
      global[property] = document.defaultView[property]
    }
  })
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
Scarysizecommented, Oct 14, 2016

Pretty stupid solution actually. Just set clientWidth and clientHeight for the container element you are passing to the map to some valid values:

  const container = document.createElement('div');
  container.clientWidth = 800;
  container.clientHeight = 800;

  const map = L.map(container);

  // now returns actual bounds, doesn't throw with jsdom
  map.getBounds()
2reactions
ughitsaaroncommented, Dec 21, 2016

I just had a very similar issue myself and wanted to provide some extra info.

I’m also running automated tests with jsdom and Mocha. jsdom provides only the DOM for testing, i.e., it doesn’t actually render the document. Element properties such as clientWidth and clientHeight, therefore, are not provided. Those properties, however, are checked in L.getSize in order to calculate the center of the map’s containing element.

A workaround like the one @Scarysize provided works fine enough, e.g.,

if (!container.clientWidth || !container.clientHeight) {
  // for testing
  container.clientWidth = 1024;
  container.clientHeight = 1024;
}

It might be useful, however, if getSize through an error if clientWidth or clientHeight can’t be checked, or if they gracefully fell back to 0 (which seems reasonable since those properties would presumably only be absent if the document wasn’t actually being rendered in a browser).

Read more comments on GitHub >

github_iconTop Results From Across the Web

I had error "Invalid LatLng object: (NaN, 86.06925048939979 ...
I had error "Invalid LatLng object: (NaN, 86.06925048939979)" when using proj4leaflet. Im using react leaflet for an interactive map. for ...
Read more >
tests.html · master · Thomas Louail / voronoize · GitLab
// Retrieves runtime widget sizing information for an element. 413. // The return value is either null, or an object with fill, padding,....
Read more >
https://zenodo.org/record/4572636/files/meta_inter...
Or, can be an array of objects with "code" and "data" // properties; ... new Error("Invalid LatLng object: ("+t+", "+i+")");this.lat=+t,this.lng=+i,void ...
Read more >
Data Visualization with JavaScript - Web Mechanic
This chapter shows off the Underscore.js library, which makes it easy to prepare the data that drives our visualizations.
Read more >
https://raw.githubusercontent.com/geanders/RProgra...
{3}/.test(i.background)},r.backgroundsize=function(){return I("backgroundSize")} ... a[0]-f[0]||f[1]-a[1]});f=[];j=[NaN,NaN];for(c=0;c<b.length;++c)i=b[c] ...
Read more >

github_iconTop Related Medium Post

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