Как сделать jest.mock simple-git / обещание - PullRequest
0 голосов
/ 01 февраля 2020

Я проверяю, чтобы макетировать simple-git/promise функцию проверки, и я не успешен. это то, что я сделал

    jest.mock('simple-git/promise', () => {
      return {
        checkout: async () => {
          Promise.resolve();
        }
      }
    });

может кто-нибудь помочь?

1 Ответ

0 голосов
/ 03 февраля 2020

Вот пример модульного теста:

index.ts:

import git from 'simple-git/promise';

function main() {
  git().checkout('https://github.com/user/repo.git');
}

export default main;

index.test.ts:

import main from './';
import git from 'simple-git/promise';

jest.mock('simple-git/promise', () => {
  const mGit = {
    checkout: jest.fn(),
  };
  return jest.fn(() => mGit);
});

describe('60018953', () => {
  it('should pass', () => {
    main();
    expect(git().checkout).toBeCalledWith('https://github.com/user/repo.git');
  });
});

Результаты модульного теста со 100% покрытием:

 PASS  src/stackoverflow/60018953/index.test.ts
  60018953
    ✓ should pass (8ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 index.ts |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        7.24s, estimated 13s

Исходный код: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/60018953

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...