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

268
Visualizações
How do I properly mock a class public function in jest?

I have the following jest test

it("should return props on stage.e.com", async () => {
  ((savedStoriesService as unknown) as jest.Mock).mockReturnValue({
    getStoriesFromList: jest.fn().mockResolvedValue([]),
  });

  const jwt = mockJwt({
    custom_attributes: { CustomerType: "Subscribed" },
  });

  const result = await getServerSideProps(({
    req: {
      headers: {
        host: <redacted>,
        authorization: `Bearer ${jwt}`,
      },
    },
  } as unknown) as GetServerSidePropsContext);

  expect(result).toMatchObject({
    props: {
      stories: [],
      isSubscriber: true,
      isLapsed: false,
    },
  });
});

I get the following error

    TypeError: _stories.savedStoriesService.getStoriesFromList is not a function

      149 |     const parsedJwt = parseJwtFromRequest(ctx.req);
      150 |
    > 151 |     const stories = await savedStoriesService.getStoriesFromList(
          |                                               ^
      152 |       ctx.req.headers?.authorization,
      153 |       { cursor: "" },
      154 |     );

I have a class called StoriesService like so

export class StoriesService {
  ...
  public getStoriesFromList = async (
    authToken: string,
    options: GetStoriesOptions,
  ): Promise<Story[]> => {
    ...
  }
  ...
}

export const savedStoriesService = new StoriesService();

How would I go about mocking this class?

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

0

you can mock it with:

jest.spyOn(savedStoriesService, 'getStoriesFromList').mockResolvedValueOnce(<the value that you want to return>)
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