javascript - 如何强制 navigator.geolocation.getCurrentPosition 失败
问题描述
我正在使用地理定位 API
// index.js
navigator.geolocation.getCurrentPosition(() => {...}, (err) => {
handleError(err)
})
让我们想象一下,handleError 在测试中被模拟了。
// index.spec.js
it("should call error handler", () => {
expect(handleError).toBeCalled()
})
我想让navigator.geolocation.getCurrentPosition
to 失败以验证是否调用了 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
推荐阅读
- javascript - 从“类”更改为“React Hooks”
- c - 如何正确编解码由宏扩展的代码
- rust - 有没有办法让我使用标准结果
在 Substrate 模块中输入? - typescript - Vue 和 TypeScript 所需的道具
- swift - Swift 版本获取错误的像素数据,但 Object-c 版本使用方法“CGBitmapContextCreate”从同一图像中获取正确的像素数据
- java - 是否可以将“GostR3410”标准证书文件导入 JKS 并使用 keytool 或 bouncycastle 包与 java 获取私钥?
- ajax - 如何解决 ajax 问题:laravel 中的 500 内部服务器错误?
- node.js - 如何使用 Chrome-Node-Debugger 捕获 Node.js 启动错误?
- vue.js - 如何删除 vuetify 自动完成组件默认图标
- agens-graph - 如何在 AgensGraph 上查找标签的所有属性名称?