TypeError: Cannot read property '_layerAdd' of null
See original GitHub issueI 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:
- Created 5 years ago
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.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.