1

I have an error when I run a test: mocha test.js.

This is the error :

const chaiHttp = require('chai-http');
const sinon = require('sinon');
const sinontest = require('sinon-test');
const test = sinontest(sinon);
const chai = require('chai');
const accounts_controller = require('../controllers/accountsController');
const tickets_controller = require('../controllers/ticketsController');
const gains_controller = require('../controllers/gainsController');
const config = require('./config');
const router = config.serverdev;
const { expect } = chai;
const accounts_spec = require('./accountsSpec'); 

//configuration of chai
chai.use(chaiHttp);
chai.should();

Account
   create an admin account
     Should create a new admin account:
 Uncaught TypeError: Cannot read property 'should' of undefined
  at /api/test/accountsSpec.js:41:17
  at Test.Request.callback (node_modules/superagent/lib/node/index.js:728:3)
  at ClientRequest.<anonymous> (node_modules/superagent/lib/node/index.js:647:10)
  at TLSSocket.socketErrorListener (_http_client.js:432:9)
  at errorOrDestroy (internal/streams/destroy.js:128:12)
  at onwriteError (_stream_writable.js:463:3)
  at onwrite (_stream_writable.js:484:7)
  at internal/streams/destroy.js:60:7
  at TLSSocket.Socket._destroy (net.js:673:5)
  at TLSSocket.destroy (internal/streams/destroy.js:55:8)
  at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:93:12)

and this is my code :

describe('Account', () => {
/* 
* create admin account group description
*/
describe('create an admin account', () => {
    /* 
    * test case to check if addadmintestaccount function exist 
    */
    it('Should exist',test(function(){
        expect(accounts_controller.addadmintestaccount).to.not.be.undefined;
    }));
    /* 
    * test case to create a new admin account
    */
    it('Should create a new admin account',(done) => {
        chai.request(router)
        .post('/api/test/addadmintestaccount')
        .set("Content-Type", "application/json")
        .end((err,res)=>{
        res.should.have.status(200);
        expect(res.body.success).to.equals(true);
        adminToken = res.body.result;
        exports.adminToken = adminToken;
        done();
        });
    });
});
});
halfer
  • 19,824
  • 17
  • 99
  • 186
mitsu
  • 429
  • 2
  • 10
  • 23
  • Have you tried checking the "err" param in the callback supplied in the .end? What libraries typically do with error-first callbacks is that if an error occurs in the asynchronous operation, they send some non-null error and null in the result, however, it is not impossible for that result to be supplied as undefined, which is how "res.should" might lead to this error. – Anuj Pancholi Feb 24 '20 at 03:34
  • i juste made ```console.log(err);``` and it gave me : Error: write EPROTO 140231124449088:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:332: – mitsu Feb 24 '20 at 13:11
  • any solution please ? – mitsu Feb 26 '20 at 23:19
  • Try adding `process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;` at the very beginning of your code. Warning: this will cause a security vulnerability. – Anuj Pancholi Feb 27 '20 at 00:46
  • hey, what is this code for please ? – mitsu Feb 27 '20 at 00:47
  • The error you logged seems like an ssl issue. I found similar issues that were fixed in previous versions of node but not this exact issue. So, setting an env variable that node uses to 0, to disable certificate validation might circumvent the steps where the issue is occurring. Of course this means that communication between your program and the server to which you're making the request may well be unencrypted. – Anuj Pancholi Feb 27 '20 at 00:52
  • where exactly can i add this code @Anuj Pancholi ? – mitsu Feb 27 '20 at 19:45
  • i put it in my code, i always have the same error + this : (node:90) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification. – mitsu Feb 28 '20 at 01:25
  • You could try something like this: https://stackoverflow.com/a/46374534/6718353 however if if is a proxy-related error like Hongarc says then this might not work, because TLS validation might still be enabled on the proxy. – Anuj Pancholi Feb 28 '20 at 02:02
  • The post related to the link said that we need to add process.env.NODE_TLS_REJECT_UNAUTHORIZED =0; however you said that it will cause a security vulnerability – mitsu Feb 28 '20 at 02:10
  • Actually, it will, but if it is some certificate validation issue, it should get rid of it (unless there's something between you and the server you're making a request to, like a proxy). – Anuj Pancholi Feb 28 '20 at 02:46
  • Am on ubuntu, do you have an idea how can i check the proxy please? – mitsu Feb 28 '20 at 10:28
  • in any case, the ```process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';``` doen't solve my problem, i still have the same error ! – mitsu Feb 28 '20 at 20:29

1 Answers1

0

You must use chai-http plugin and call chai.should:

let chaiHttp = require('chai-http');

let should = chai.should();

chai.use(chaiHttp);

Second, your error is Cannot read property 'should' of undefined so res is undefined. Please check you create success post(without err)

hong4rc
  • 3,999
  • 4
  • 21
  • 40
  • hi, i had this : ```chai.use(chaiHttp); chai.should();``` but when you said ```let should = chai.should();``` whe can i put should variable in my code ? – mitsu Feb 24 '20 at 13:03
  • Actually, you don't need to put `should` variable anywhere. call `chai.should` is enough – hong4rc Feb 24 '20 at 13:05
  • Can you check `err` variable, maybe your post have a problem so `res` is `undefined` – hong4rc Feb 24 '20 at 13:19
  • it gives : Error: write EPROTO 140231124449088:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:332: – mitsu Feb 24 '20 at 19:24
  • any solution please ? – mitsu Feb 26 '20 at 23:19
  • I research about this error. All of them say about proxy problem. Are you sure your company/computer don't use proxy – hong4rc Feb 27 '20 at 03:30
  • Am on ubuntu, how can i check that? – mitsu Feb 27 '20 at 11:46