NuxtN
Nuxt13mo ago
19 replies
Yuri

Testing $fetch

My api.ts utils:
export const apiBack = async <T>(
  url: string,
  token: string,
  options: NitroFetchOptions<string> = {},
): Promise<T> => {
  options.baseURL = process.env.BITRIX_API_URL
  setAuthorizationHeader(options, token)

  const response = await $fetch<T>(url, options)
  return response as T
}


I write this test:
import { describe, it, expect,} from 'vitest';
import {apiBack} from "~/utils/api";
import {testMockTokenAccess, testMockUrlRelative} from "~/utils/tests";
import { $fetch } from '@nuxt/test-utils'


describe('api Utils',()=>{
    it('Проверка корректной работы запросов к беку apiBack',()=>{
        apiBack(testMockUrlRelative,testMockTokenAccess)
        expect($fetch).toHaveBeenCalled();
    })
})

And I get the error:
FetchError: [GET] "/catalog": 404 Cannot find any path matching /catalog.

on this line
 const response = await $fetch<T>(url, options)

What is correct variant to check my apiBack function ?
Was this page helpful?