Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!