Tengo algo de código en mi archivo sanity.ts :
import sanityClient from '@sanity/client'; // eslint-disable-next-line @typescript-eslint/no-var-requires const blocksToHtml = require('@sanity/block-content-to-html'); const client = sanityClient({ projectId: '', dataset: '', apiVersion: '2021-05-11', token: String(process.env.SANITY_API_KEY), useCdn: false, }); export async function getData(): Promise<void> { const query = ''; const sanityResponse = await client.fetch(query); return; } Estoy tratando de simular esto cuando lo estoy probando, pero tengo problemas para configurar el simulacro. Sigo recibiendo TypeError: client_1.default is not a function . Esto es lo que tengo en mi archivo de prueba Jest:
jest.mock('@sanity/client', () => { const mClient = { fetch: jest.fn(), }; return { client: jest.fn(() => mClient) }; });¿Qué estoy haciendo mal?
ACTUALIZAR:
creó una carpeta __mocks__ con el siguiente código y obtuvo un error diferente:
class sanityClient {} const client = jest.fn(() => new sanityClient()); const fetchMock = jest.fn(); client.prototype = { fetch: fetchMock, }; module.exports = sanityClient; module.exports.client = client; module.exports.fetch = fetchMock; TypeError: client.fetch is not a function
¿Alguna ayuda?
Lo hice funcionar: para cualquiera que necesite burlarse de la búsqueda en función de la cordura:
jest.mock('@sanity/client', () => { return function sanity() { return { fetch: () => ({ methodOne: [{}], methodTwo: [{}], }), }; }; });