sails.socket testing with npm test

General Tech QA/Testing 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on QA/Testing related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I'm using sails.js v0.11.0 and I am just getting into unit testing. I can test normal controllers through http requests, but I have no idea where to start to test the same calls over a socket requests. If you have a good resource or a sample test using sockets that would be fantastic.

var assert = require('assert');
var request = require('supertest');

describe('Auth Controller', function () {

  describe('#callback()', function () {

    it ('passport-local authentication should succeed if email and password valid', function (done) {

      request(sails.hooks.http.app)
        .post('/auth/local')
        .send({
          identifier: 'existing.user@email.com',
          password: 'admin1234'
        })
        .expect(200)
        .end(function(err) {
          done(err);
        });

    });

    it ('passport-local authentication should fail and return error code if email is invalid', function (done) {

      request(sails.hooks.http.app)
        .post('/auth/local')
        .send({
          identifier: 'invalid@email.com',
          password: 'admin1234'
        })
        .expect(403)
        .end(function(err) {
          done(err);
      });

    });

    it ('passport-local authentication should fail and return error code if password is invalid', function (done) {

      request(sails.hooks.http.app)
        .post('/auth/local')
        .send({
          identifier: 'existing.user@email.com',
          password: 'invalid1235'
        })
        .expect(403)
        .end(function(err) {
          done(err);
      });

    });

    //Test with Web Sockets from sails.io
    describe('sails.socket', function () {

      describe('With default settings', function() {

        describe('once connected, socket', function () {

          it ('passport-local authentication via web socket should succeed if email and password valid', function (done) {

            //Socket version?
            request(sails.hooks.http.app
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

I can't claim credit for this, but incase you've stumbled upon this question and are looking for the answer here it is.

Declare io as a global in the bootstrap:

// test/boostrap.test.js

var client = require('../assets/js/dependencies/sails.io.js');

global.io = new client(require('socket.io-client'));
io.sails.url = 'http://localhost:1337/';

Then call them use them like so.

//test/callbacks.test.js
describe('socket request', function () {

 it ('passport-local authentication should succeed if email and password valid', function (done) {

  io.socket.post('/auth/local', { identifier: 'existing.user@email.com', password: 'admin1234' }, function (data, jwres) {
  assert.equal(jwres.statusCode, 200);
  done();  
 });
});

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community