[英]navigator.geolocation.getCurrentPosition always fail in chrome and firefox
[英]How to force navigator.geolocation.getCurrentPosition to fail
我正在使用地理定位 API
// index.js
navigator.geolocation.getCurrentPosition(() => {...}, (err) => {
handleError(err)
})
让我们想象一下在测试中模拟了 handleError 。
// index.spec.js
it("should call error handler", () => {
expect(handleError).toBeCalled()
})
我想让navigator.geolocation.getCurrentPosition
失败以验证 handleError 是否被调用。
我正在使用 Jest,那么有没有办法控制getCurrentPosition
的失败?
这里是单元测试解决方案,测试环境是node
:
index.js
:
import { handleError } from './errorHandler';
function main() {
navigator.geolocation.getCurrentPosition(
() => {
console.log('success');
},
(err) => {
handleError(err);
},
);
}
export { main };
errorHandler.js
:
import { handleError } from './errorHandler';
function main() {
navigator.geolocation.getCurrentPosition(
() => {
console.log('success');
},
(err) => {
handleError(err);
},
);
}
export { main };
index.test.js
:
import { main } from './';
import { handleError } from './errorHandler';
jest.mock('./errorHandler', () => {
return { handleError: jest.fn() };
});
describe('60062574', () => {
beforeEach(() => {
global.navigator = { geolocation: { getCurrentPosition: jest.fn() } };
});
it('should handle error', () => {
const mError = new Error('some error');
global.navigator.geolocation.getCurrentPosition.mockImplementationOnce((successCallback, errorCallback) => {
errorCallback(mError);
});
main();
expect(navigator.geolocation.getCurrentPosition).toBeCalledWith(expect.any(Function), expect.any(Function));
expect(handleError).toBeCalledWith(mError);
});
it('should handle success', () => {
const logSpy = jest.spyOn(console, 'log');
global.navigator.geolocation.getCurrentPosition.mockImplementationOnce((successCallback, errorCallback) => {
successCallback();
});
main();
expect(logSpy).toBeCalledWith('success');
expect(navigator.geolocation.getCurrentPosition).toBeCalledWith(expect.any(Function), expect.any(Function));
});
});
100% 覆盖率的单元测试结果:
PASS stackoverflow/60062574/index.test.js
60062574
✓ should handle error (5ms)
✓ should handle success (17ms)
console.log node_modules/jest-mock/build/index.js:814
success
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 2.715s, estimated 5s
jest.config.js
:
module.exports = {
preset: 'ts-jest/presets/js-with-ts',
testEnvironment: 'node',
setupFilesAfterEnv: ['./jest.setup.js'],
testMatch: ['**/?(*.)+(spec|test).[jt]s?(x)'],
verbose: true,
};
源代码: https : //github.com/mrdulin/react-apollo-graphql-starter-kit/tree/master/stackoverflow/60062574
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.