【问题标题】:fetch-mock: No fallback response defined for POSTfetch-mock:没有为 POST 定义回退响应
【发布时间】:2020-01-03 09:33:59
【问题描述】:

我所有的 GET 请求都在通过,但 POST 请求失败。当我将 fetch-mock7.3.0 更新为 7.3.1稍后时会发生这种情况。

console.warn Unmatched POST to url

错误 fetch-mock: No fallback response defined for POST to url

http.js

export const get = (url) => {
  const options = {
    method: 'GET',
    credentials: 'same-origin'
  };

  return fetch(url, options).then(handleJsonResponse);
};

export const post = (url, body) => {
  const headers = {
    'content-type': 'application/json',
    'pragma': 'no-cache',
    'cache-control': 'no-cache'
  };

  return fetch(url, {
    credentials: 'same-origin',
    method: 'POST',
    cache: 'no-cache',
    body: JSON.stringify(body),
    headers
  }).then(handleJsonResponse);
};

http.spec.js

const url = '/path/to/url'

describe('get', () => {
    it('makes a GET request', async () => {
      fetchMock.mock({
        name: 'route',
        matcher: url,
        method: 'GET',
        credentials: 'same-origin',
        response: {
          status: 200,
          body: []
        }
      });

      const response = await get(url);

      expect(fetchMock.called()).toEqual(true);
      expect(fetchMock.calls().length).toEqual(1);
      expect(fetchMock.calls('route').length).toEqual(1);
      expect(response).toEqual([]);
    });
  });

  describe('post', () => {
    const requestBody = {request: 'request'};

    it('makes a POST request', async () => {
      fetchMock.mock({
        name: 'route',
        matcher: url,
        method: 'POST',
        credentials: 'same-origin',
        cache: 'no-cache',
        body: JSON.stringify(requestBody),
        headers: {
          'content-type': 'application/json',
          'pragma': 'no-cache',
          'cache-control': 'no-cache'
        },
        response: {
          status: 200,
          body: []
        }
      });

      const response = await post(url, requestBody);

      expect(fetchMock.called()).toEqual(true);
      expect(fetchMock.calls().length).toEqual(1);
      expect(fetchMock.calls('route').length).toEqual(1);
      expect(fetchMock.lastOptions().headers).toEqual({
        'content-type': 'application/json',
        'pragma': 'no-cache',
        'cache-control': 'no-cache'
      });
      expect(response).toEqual([]);
    });
  });

对造成这种情况的原因有什么想法吗?有没有办法获得更有意义的日志来帮助调试?

我宁愿不去尝试 nockjest-fetch-mock 的替代路径。

【问题讨论】:

    标签: javascript api unit-testing jestjs fetch-mock


    【解决方案1】:

    好的,在对图书馆本身进行了数小时的研究之后,我发现了问题所在。

    在我的代码(以及上面的 sn-p)中,我正在 stringifying 正文 JSON.stringify(body)。库的generate-matcher.js 正在解析JSON.parse(body),然后比较两者 - 导致失败的点。我现在只是将它作为原始对象发送。

    【讨论】:

    • 你能分享更新的代码吗?我面临着类似的问题。
    【解决方案2】:

    万一其他人将来在这里结束,fetch-mock unmatched get 也会出现同样的错误。

    我看到了对 this issue filed to fetch-mock 的回复,这促使我仔细检查了我的预期值和模拟值。

    事实证明,我的问题与所描述的错误完全一样,我所期望的模拟路由和被调用的实际路由由于拼写错误而不匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 2017-06-05
      • 2018-02-20
      • 2020-04-28
      • 2023-03-04
      相关资源
      最近更新 更多