admin管理员组文章数量:1320593
I want to mock the google maps API that I am using with the @googlemaps/js-api-loader package.
For now, I need to ensure that the geocoder was called and I am trying to do so by:
const mockGeocoder = jest.fn();
jest.mock('google.maps.Geocoder', () => () => ({
geocoder: mockGeocoder,
}));
But I get
Cannot find module 'google.maps.Geocoder' from 'tests/unit/usecases/maps/getPlaceByAddress.spec.ts'
3 | const mockGeocoder = jest.fn();
4 |
> 5 | jest.mock('google.maps.Geocoder', () => () => ({
Not sure how to proceed. Can anyone help me with this?
I want to mock the google maps API that I am using with the @googlemaps/js-api-loader package.
For now, I need to ensure that the geocoder was called and I am trying to do so by:
const mockGeocoder = jest.fn();
jest.mock('google.maps.Geocoder', () => () => ({
geocoder: mockGeocoder,
}));
But I get
Cannot find module 'google.maps.Geocoder' from 'tests/unit/usecases/maps/getPlaceByAddress.spec.ts'
3 | const mockGeocoder = jest.fn();
4 |
> 5 | jest.mock('google.maps.Geocoder', () => () => ({
Not sure how to proceed. Can anyone help me with this?
Share Improve this question asked Feb 15, 2022 at 20:28 Eduardo SousaEduardo Sousa 1,0751 gold badge12 silver badges28 bronze badges2 Answers
Reset to default 3I did the following:
const setupGoogleMock = () => {
global.window.google = {
maps: {
Geocoder: jest.fn(() => ({
geocode: mockGeocoder,
})),
GeocoderStatus: {
ERROR: 'ERROR',
INVALID_REQUEST: 'INVALID_REQUEST',
OK: 'OK',
OVER_QUERY_LIMIT: 'OVER_QUERY_LIMIT',
REQUEST_DENIED: 'REQUEST_DENIED',
UNKNOWN_ERROR: 'UNKNOWN_ERROR',
ZERO_RESULTS: 'ZERO_RESULTS',
},
} as any,
};
};
and then
beforeAll(() => {
setupGoogleMock();
});
This is where I found the answer.
Check the package https://www.npmjs./package/@googlemaps/jest-mocks for mocks and examples. I'm not sure if Geocoder has a mock, but it should be possible to follow the pattern there (and send a pr).
本文标签: javascriptManually mock Google Maps in JestStack Overflow
版权声明:本文标题:javascript - Manually mock Google Maps in Jest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742078923a2419577.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论