Having a component that uses the embedded geolocation
feature to get coordinates, how can I mock the call in a Jest test?
The component's code work in the org:
...
get locationService () {
return navigator.geolocation
}
...
getCurrentPosition () {
if (this.locationService) {
let component = this
this.locationService.getCurrentPosition(position => {
component.markers = [{
location: {
Latitude: position.coords.latitude,
Longitude: position.coords.longitude
},
title: 'You are here!'
}]
component.coordinates.lat = position.coords.latitude
component.coordinates.lon = position.coords.longitude
})
}
}
...
But how can I test it, since having the following mock implementation results in an error when calling afterEach
?
it('Should display the coordinate.', () => {
const mockGeolocation = {
getCurrentPosition: jest.fn()
.mockImplementation((success) => Promise.resolve(success({
coords: {
latitude: 10,
longitude: 10
}
})))
}
navigator.geolocation = mockGeolocation
...
... rest of the test goes below
})
The error on afterEach
is this one on the terminal:
Assert Violation: undefined is not a vm.
36 | afterEach(() => {
37 | while (document.body.firstChild) {
> 38 | document.body.removeChild(document.body.firstChild)
| ^
39 | }
40 | jest.clearAllMocks()
41 | })
The error doesn't show up if I comment out the line that assigns the mockGeolocation
object to the navigator.geolocation
:
// navigator.geolocation = mockGeolocation
getCurrentPosition
method and I'm unable to reproduce the error. Have you tried running your test with a debugger attached to see if there's any other errors being thrown that could give some clues?npm run test:unit -- force-app/main/default/lwc/hello/
github.com/trevor-bliss/lwc-recipes/commit/…