为什么笑话模拟没有捕获我模拟的请求(slack web-api)

问题描述 投票:0回答:1

我已经为此奋斗了一段时间,但尝试了我发现的所有 slack web-api 模拟的变体,但没有一个对我有用,所以我想我会在这里分享它,也许有人会知道我是什么这里不见了。

这是我的笑话测试文件的结构:

import ....(all the imports)

describe('my slack tests', () => {
  beforeAll(() => {
    // Mock slack web api
    jest.mock('@slack/web-api', () => ({
      ...jest.requireActual('@slack/web-api'),
    
      WebClient: jest.fn().mockImplementation(() => ({
        auth: {
          test: () => Promise.resolve({ ok: true }),
        },
        conversations: {
          list: () => Promise.resolve({ ok: true,
                                        response_metadata: {},
                                        channels: [
                                          {name: 'test_channel', id: 'AAABBB123456'}, 
                                          {name: 'ABCD1234', id: 'AAABBB123457'},
                                        ] }),
        },
      })),
    }));
  });
  test('My Test', async () => {
    ...Calling a function that calls the WebClient.conversations.list(...)...
    ... Verified the function works outside the test with proper token....
  });
});

我确信我在这里遗漏了一些东西,但不确定是什么。 我得到的结果是以下错误“

Error: An API error occurred: invalid_auth
”——对我来说,这意味着 api 调用可能会发送到真正的 slack API,而模拟不会捕获请求。

任何帮助或想法将不胜感激。

我尝试了在网上找到的同一模拟的所有可能变体,它们的行为都相同 - 就好像没有模拟一样。

mocking slack-api
1个回答
0
投票

在对该问题进行了几次尝试后,似乎只需将 jest.mock 从描述块中拉出即可解决该问题。 对于遇到类似问题的人,请将笑话模拟保留在文件顶部以及任何函数/代码块之外。

© www.soinside.com 2019 - 2024. All rights reserved.