Выдать модульное тестирование этой функции в узле с помощью узла-выборки - PullRequest
1 голос
/ 11 июля 2020

Я пытаюсь выполнить модульное тестирование этой функции .... следуя документации.

Есть идеи, почему я получаю эту ошибку? Согласно документации мы видим, что это называется так ... Что мне не хватает. https://jestjs.io/docs/en/bypassing-module-mocks

Спасибо

async function isValid(pair) {
    const path = process.env.PATH;
    const query = '/check';
    const requestOptions = ccyServiceRequestOptions(pair);
    try {
        const response = await fetch(path + query, requestOptions);
        const json = await response.json();
        return json.result.pair === pair;
    } catch (err) {
        if (err instanceof TypeError) {
            return false;
        }
        else {
            log.error(err);
            throw err;
        }
    }
}

------------------------------------------------------------------------------------------
jest.mock('node-fetch');

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


beforeEach(() => {
    jest.clearAllMocks()
})


it('should call with valid url', async () => {
        const pair = 'EURUSD';
        fetch.mockReturnValue(Promise.resolve(new Response({})));

        await ccyPairIsValid(pair);

        expect(fetch).toHaveBeenCalled();
        expect(fetch.mock.calls[0][0]).toHaveBeenCalledWith(process.env.PATH + '/check');
    })

Error: expect(received).toHaveBeenCalledWith(...expected)

Matcher error: received value must be a mock or spy function

Received has type:  string
Received has value: "http://localhost:8000/check"

1 Ответ

0 голосов
/ 14 июля 2020

Вы вызываете fetch с двумя аргументами, если вас не волнует аргумент requestOptions, вы можете использовать expect.anything()

...