programing

Vue Jest 테스트에서 navigato.geolocation.getCurrentposition을 "mock"하는 방법

prostudy 2022. 7. 4. 21:44
반응형

Vue Jest 테스트에서 navigato.geolocation.getCurrentposition을 "mock"하는 방법

vue에서 Google Maps 컴포넌트를 테스트하려고 하는데 navigator.geolocation.getCurrentPosition()이 메서드 내에 있습니다.

이 테스트는 실패하고 콘솔에 출력됩니다.

오류:TypeError: 정의되지 않은 속성 'getCurrentPosition'을 읽을 수 없습니다.

  167 |     },
  168 |     geolocate: function() {
> 169 |       navigator.geolocation.getCurrentPosition(position => {
      | ^

이건 내 코드야

          export default {
            name: "GoogleMap",
            data() {

            },

            created() {

            },

            mounted() {
              this.geolocate();
            },

            methods: {
              ..............
              geolocate: function() {
                navigator.geolocation.getCurrentPosition(position => {
                  this.center = {
                    lat: position.coords.latitude,
                    lng: position.coords.longitude
                  };
                });
              ...............
            },
            computed: {

            }
          };

          </script>

나는 그것을 조롱하려 했지만 효과가 없다

    describe("Maps.vue", () => {
      it("renders ... Google maps ", () => {
        const wrapper = shallowMount(Maps, { 

           mocks : {
            getCurrentPosition:()=> jest.fn()

           },
         })
        expect().....
      })
    })

어떻게 하면 이 네비게이터 방법을 '복제'하여 시험에 합격할 수 있을까요?

언급URL : https://stackoverflow.com/questions/54033294/how-to-mock-navigato-geolocation-getcurrentposition-in-a-vue-jest-test

반응형