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

336
Visualizações
JEST test passes, but coverage fails

Could someone help me? I've been on this for hours and I can't solve it.

I'm wanting to test a request with the superagent and check if it returns the status code 200. I put Jest Spy in my Class for it to simulate the return. The test passes but my coverage doesn't. What am I doing wrong?

I'm using NestJS

Code:

auth.service.ts

import { Injectable } from '@nestjs/common';
import { Scalar, Query, Args } from '@nestjs/graphql';
import { get } from 'superagent';

@Injectable()
@Scalar("JSON", (_) => Object)
export class AuthService {

    @Query((_) => Object)
    async checkIfUserIsOnline(@Args({ name: "jwtToken", type: () => String }) jwt: string) {
        const resp = await get(process.env.KEYCLOACK_CHECK_URL).set("Authorization", `Bearer${jwt}`)
        return { statusCode: resp.statusCode, body: true }
    }
}

auth.service.spec.ts

import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from './auth.service';

describe('AuthService', () => {
  let service: AuthService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [AuthService],
    }).compile();

    service = module.get<AuthService>(AuthService);

  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });


  test("if user is online", async () => {

    let spy = jest.spyOn(service, "checkIfUserIsOnline")
    spy.mockResolvedValue({ statusCode: 200, body: true })

    let req = await service.checkIfUserIsOnline("blablabla")
    expect(req).toStrictEqual({ "body": true, "statusCode": 200 })
  })


});

Coverage:

Jest Coverage Report

about 4 years ago · Juan Pablo Isaza
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