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.

TypeError: Cannot read property '_layerAdd' of null

See original GitHub issue

I using leaflet v1.3.3 jest v 22.4.3

My code

import L from 'leaflet'

describe('TEST', () => {
  it('Circle', () => {
    const mapElement = document.createElement('div')
    mapElement.setAttribute('id', 'map')
    document.body.appendChild(mapElement)
    let map = L.map('map', {
    }).setView([1,  1], 14)
    L.circle([1,  1]).addTo(map)
    expect(123).toBe(123)
  })
})

Run yarn test

The results here:

TypeError: Cannot read property '_layerAdd' of null

       8 |     let map = L.map('map', {
       9 |     }).setView([1,  1], 14)
    > 10 |     L.circle([1,  1]).addTo(map)
      11 |     expect(123).toBe(123)
      12 |   })
      13 | })

      at NewClass.addLayer (node_modules/leaflet/src/layer/Layer.js:158:14)
      at NewClass.getRenderer (node_modules/leaflet/src/layer/vector/Renderer.getRenderer.js:21:9)
      at NewClass.beforeAdd (node_modules/leaflet/src/layer/vector/Path.js:80:24)
      at NewClass.addLayer (node_modules/leaflet/src/layer/Layer.js:169:10)
      at NewClass.addTo (node_modules/leaflet/src/layer/Layer.js:52:7)
      at Object.addTo (spec/javascript/abc.spec.js:10:38)

When I use L.circle or L.polyline it will get this error.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

12reactions
kentrcommented, Sep 27, 2020

Adding this in case it helps someone.

If you don’t want to mock Leaflet for whatever reason and want to continue using the SVG renderer, another fix for this error is to add a stub function for createSVGRect in the test environment, as suggested by Vadim Gremyachev.

11reactions
xielcommented, Jun 7, 2019

What I can recommend for testing is the following, just mock leaflet using this in your test file, that is rendering the map component: jest.mock('leaflet')

And then you place this leaflet mock you can find in react-leaflet into the jest mocks folder (__mocks__/leaflet.js, download current file from: https://github.com/PaulLeCam/react-leaflet/blob/master/__mocks__/leaflet.js)

That should be enough in most cases.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Leaflet JS TypeError: Cannot read property 'addLayer' of null
I'm creating map on Leaflet JS + Mapbox. Trying to highlight my regions visually. I have code, which paint over my regions by...
Read more >
leaflet Uncaught TypeError: Cannot read property 'addLayer ...
Either use new with the class (UPPER case first letter): map = new L.Map('mapid'); var mondayLayer = new L.GeoJSON().
Read more >
Uncaught TypeError: Cannot read property of null - iDiallo
This will result in Uncaught TypeError: Cannot read property 'value' of null . The reason will be that the element with id input...
Read more >
Cannot read properties of undefined (reading 'addLayer')
Hi. Suddenly I have this error message : TypeError: Cannot read properties of undefined (reading 'addLayer'). I am using Elementor Pro (last version)....
Read more >
uncaught typeerror: cannot read properties of null ... - You.com
Your issue is because you started your data out as null . On initial render before the api has a chance to come...
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