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

219
Visualizações
Mocha js Uncaught TypeError: Cannot read properties of undefined (reading 'should')

So I'm trying to test my API and with mocha js and I'm getting this error.

SAMPLE unit test
1) should return posts with tag tech sorted by likes


0 passing (26ms)
1 failing

1) SAMPLE unit test
  should return home page:
  Uncaught TypeError: Cannot read properties of undefined (reading 'should')
  at Test.<anonymous> (test\get_info.test.js:22:18)
  at Test.assert (node_modules\supertest\lib\test.js:172:8)
  at localAssert (node_modules\supertest\lib\test.js:120:14)
  at C:\Users\tiktk\Desktop\Fetch\node_modules\supertest\lib\test.js:125:7
  at Test.Request.callback (node_modules\superagent\lib\node\index.js:927:3)
  at ClientRequest.<anonymous> (node_modules\superagent\lib\node\index.js:844:12)
  at ClientRequest.emit (node:events:390:28)
  at Socket.socketErrorListener (node:_http_client:447:9)
  at Socket.emit (node:events:390:28)
  at emitErrorNT (node:internal/streams/destroy:157:8)
  at emitErrorCloseNT (node:internal/streams/destroy:122:3)
  at processTicksAndRejections (node:internal/process/task_queues:83:21)

I have all the dependencies installed and up to date but I still get that error.

var supertest = require("supertest");
var should = require("should");

var server = supertest.agent("http://127.0.0.1:3000");

describe("Api unit test",function(){

  it("should return posts with tag tech sorted by likes",function(done){

    server
    .get("/api/posts?tags=tech&sortBy=likes&direction=desc")
    .expect("Content-type",/json/)
    .expect(200) 
    .end(function(res){
      // HTTP status should be 200
      res.status.should.equal(200);
      // Error key should be false.
      res.body.error.should.equal(false);
      done();
    });
  });
});

The link should return a bunch of objects in an array as a JSON file sorted by likes (in this case)

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

0

The error most probably comes from the fact that res.body.error is undefined when you don't have an error. In which case, your test should be something like:

res.body.should.not.have.property('error')

To confirm my hypothesis, you should console.log the content of res.body before checking for the error.

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