You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jest中Mock Octokit.rest.repos.getContent()方法

在Jest中Mock Octokit库的方法

下面针对你使用的octokit.rest.repos.getContent()方法,提供几种实用的Mock方案:

方案1:Mock整个@octokit/rest模块

适合你的代码中自行实例化Octokit的场景,直接Mock模块并定义实例的方法返回值:

import { Octokit } from '@octokit/rest';
// 替换成你的业务代码导入
import { yourFunctionThatUsesOctokit } from './your-module';

// Mock整个@octokit/rest库
jest.mock('@octokit/rest');

describe('测试Octokit调用逻辑', () => {
  beforeEach(() => {
    // 每次测试前重置Mock,避免交叉影响
    Octokit.mockClear();

    // 定义getContent方法的模拟返回
    const mockGetContent = jest.fn().mockResolvedValue({
      data: { 
        id: 123,
        content: 'SGVsbG8gV29ybGQh' // 模拟base64内容
      }
    });

    // 让Octokit实例返回包含mock方法的结构
    Octokit.mockImplementation(() => ({
      rest: {
        repos: {
          getContent: mockGetContent
        }
      }
    }));
  });

  test('正确调用getContent并处理返回值', async () => {
    const owner = 'test-owner';
    const repo = 'test-repo';
    const path = 'README.md';
    const ref = 'main';

    // 执行你的业务函数
    const result = await yourFunctionThatUsesOctokit(owner, repo, path, ref);

    // 验证Octokit是否被实例化
    expect(Octokit).toHaveBeenCalledTimes(1);
    // 验证getContent方法的调用参数是否正确
    expect(Octokit.mock.instances[0].rest.repos.getContent).toHaveBeenCalledWith({
      owner, repo, path, ref
    });
    // 验证返回值是否符合预期
    expect(result).toEqual({ id: 123, content: 'SGVsbG8gV29ybGQh' });
  });
});

方案2:Mock已传入的Octokit实例

如果你的业务代码是接收外部传入的Octokit实例,直接创建模拟实例即可:

业务代码示例

// your-module.js
export async function fetchRepoContent(octokit, params) {
  const { data: entity } = await octokit.rest.repos.getContent(params);
  return entity;
}

测试代码

import { fetchRepoContent } from './your-module';

describe('fetchRepoContent', () => {
  test('正确调用Octokit方法并返回数据', async () => {
    // 创建模拟的Octokit实例
    const mockOctokit = {
      rest: {
        repos: {
          getContent: jest.fn().mockResolvedValue({
            data: { content: 'mocked repo content' }
          })
        }
      }
    };

    const testParams = {
      owner: 'test-owner',
      repo: 'test-repo',
      path: 'docs/guide.md',
      ref: 'dev'
    };

    const result = await fetchRepoContent(mockOctokit, testParams);

    // 验证方法调用参数
    expect(mockOctokit.rest.repos.getContent).toHaveBeenCalledWith(testParams);
    // 验证返回结果
    expect(result).toEqual({ content: 'mocked repo content' });
  });
});

方案3:使用jest.spyOn Mock特定方法

如果已经存在Octokit实例,只想MockgetContent方法而不影响其他功能:

import { Octokit } from '@octokit/rest';
import { yourFunction } from './your-module';

describe('测试spyOn方式的Mock', () => {
  let octokitInstance;
  let mockGetContent;

  beforeEach(() => {
    // 创建真实的Octokit实例,然后spyOn目标方法
    octokitInstance = new Octokit();
    mockGetContent = jest.spyOn(octokitInstance.rest.repos, 'getContent')
      .mockResolvedValue({ data: { content: 'spy mocked content' } });
  });

  afterEach(() => {
    // 测试结束后恢复原方法,避免影响其他测试
    mockGetContent.mockRestore();
  });

  test('调用Mock后的getContent方法', async () => {
    const result = await yourFunction(octokitInstance);

    expect(mockGetContent).toHaveBeenCalled();
    expect(result).toEqual({ content: 'spy mocked content' });
  });
});

内容的提问来源于stack exchange,提问作者Akash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 13:30:40