1

I am new and fail to make supertest work for me. I am wondering:

  • Why is the body undefined?
  • Is there a trick from the command line to show and inspect objects in the console?
  • Why doesn't the test logs "hello"?

 "use strict";
 
 const request = require('supertest');
 const express = require('express');
 const https = require('https');
 const fs = require('fs');
 const path = require('path');
 const certPath = path.resolve(path.resolve(), './certs');
 
 const app = express();
 
 //This line is from the Node.js HTTPS documentation.
 const options = {
  key : fs.readFileSync(certPath+'/server-key.pem'),
  cert : fs.readFileSync(certPath+'/server-crt.pem'),
  ca : fs.readFileSync(certPath+'/ca-crt.pem')
 };
 
 // service
 app.post('/failService', function(req, res) {
  console.log('failService: '+req.body); // failService: undefined
  res.send('hello');
 });
 
 describe('trial not working', function() {
   it('responds with json', function(done) {
    request(app)
       .post('/failService')
       .send({name: 'john'})
       .set('Accept', /json/)
       .expect(200)
       .end(function(err, res) {
         if (err) return done(err);
         console.log('response: '+res.body); // response: [object Object]
         done();
       });
   });
 });

.... shows

$ mocha supertest.js

  trial not working
failService: undefined
response: [object Object]
    √ responds with json (125ms)


  1 passing (171ms)

Please note that the certificates (not included) are self signed.

otembajelle
  • 166
  • 2
  • 12

2 Answers2

0

When isolating my code above I missed the options. As a result it does not use SSL. Sorry for that.

I fell back to starting the server and the use of a client in my test case. For that I had to fix:

  1. CORS
  2. The actual problem of this post by using a body parser
otembajelle
  • 166
  • 2
  • 12
0

This is due to the self signed certificate.

i also faced similar issue there are two possible solutions

  1. Create http server instead of https server for test environment
  2. Replace supertest with superrequest npm package and set strictSsl as false.
Rigin Oommen
  • 3,060
  • 2
  • 20
  • 29