如何强制 navigator.geolocation.getCurrentPosition 失败

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  Whosebug/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/Whosebug/60062574