Ошибка типа: поддерживаются только абсолютные URL - PullRequest
0 голосов
/ 31 мая 2019

Я слежу за этим https://jestjs.io/docs/en/bypassing-module-mocks, который состоит в основном из createUser.js и шутки за него.В моем случае я использую TypeScript

// createUser.ts
import fetch from 'node-fetch';

export const createUser = async () => {
  const response = await fetch('http://website.com/users', {method: 'POST'});
  const userId = await response.text();
  return userId;
};
// jest for createUser.ts
jest.mock('node-fetch');

import fetch from 'node-fetch';
const {Response} = jest.requireActual('node-fetch');

import {createUser} from './createUser';

test('createUser calls fetch with the right args and returns the user id', async () => {
  fetch.mockReturnValue(Promise.resolve(new Response('4')));

  const userId = await createUser();

  expect(fetch).toHaveBeenCalledTimes(1);
  expect(fetch).toHaveBeenCalledWith('http://website.com/users', {
    method: 'POST',
  });
  expect(userId).toBe('4');
});

Хотя вместо получения TypeError: response.text is not a function, как описано в руководстве, я получаю TypeError: Only absolute URLs are supported.

Когда я изменяю на fetch.mockReturnValue(Promise.resolve('http://website.com/users') TypeError: Only absolute URLs are supported ушел, и я получил TypeError: response.text is not a function.

Итак, как на самом деле высмеивать ответ в шутку?

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