Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
Mocking PubSub publish method

I am following this Mock new Function() with Jest to mock PubSub, unfortunately no luck.

jest.mock('@google-cloud/pubsub', () => jest.fn())

...

const topic = jest.fn((name) => ({ '@type': 'pubsub#topic', name }))
const publish = jest.fn((data) => ({ '@type': 'Buffer', data }))

const mockedPubSub = PubSub as jest.Mock<PubSub>
mockedPubSub.mockImplementation(() => ({ topic }))

I got two erros.

The first one is one the line

PubSub as jest.Mock<PubSub>

Conversion of type 'typeof PubSub' to type 'Mock<PubSub, any>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.

And the second one is on the last line

mockedPubSub.mockImplementation(() => ({ publish }))

Argument of type '() => { publish: jest.Mock<{ '@type': string; data: any; }, [data: any]>; }' is not assignable to parameter of type '(...args: any) => PubSub'.

How can I mock this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Found the answer

const mockTopic = jest.fn().mockImplementation(() => ({
  get: jest.fn(),
  publish: jest.fn(),
}));

const mockPublish = jest.fn();

jest.mock('@google-cloud/pubsub', () => ({
  __esModule: true,
  PubSub: jest.fn().mockImplementation(() => ({
    topic: mockTopic,
    publish: mockPublish,
  })),
}))
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda