trailsjs / sails-swagger

Swagger integration for sails.js
107 stars 47 forks source link

How to test controllers which are written in tyescript ?? #43

Open vishalbiradar opened 7 years ago

vishalbiradar commented 7 years ago

I have followed all the steps mentioned by you to integrate sails js with swagger. But while doing testing I am getting error. In our project we have written the api controllers in tyescript. Then we have written gulp code for converting it into javascript files. But while testing those controllers using mocha, test is getting failed.

api/controllers/UserDetailsController.ts

import dbUtilies = require('../services/dbUtils');

// Get Users
export function get_users(req:any, res:any, next: Function):any {
  let name = req.query['name'];
  let id = req.query['id'];
  if (name) {
    let reqObj = {username: name};
    dbUtilies.getOne('user_details', reqObj).subscribe(
      data => {
        res.status(200).send(data);
      },
      err => {
        res.status(404).send(err);
      }
    );
  } else if (id) {
    let reqObj = {id: id};
    dbUtilies.getOne('user_details', reqObj).subscribe(
      data => {
        res.status(200).send(data);
      },
      err => {
        res.notFound(err);
        // res.status(404).send(err);
      }
    );
  } else {
    dbUtilies.getAll('user_details')
      .subscribe(
        data => {
          res.status(200).send(data);
        },
        err => {
          res.status(500).send(err);
        }
    );
  }
}
}

service in typescript

api/services/dbUtils.ts

import Rxjs = require('rxjs');
import sails = require('sails');

export function getAll(modelName: string) {
    console.log(modelName);
    return Rxjs.Observable.fromPromise(sails.models[modelName].find());
}

export function getOne(modelName: string, obj: any) {
    return Rxjs.Observable.fromPromise(sails.models[modelName].find(obj))
        .mergeMap(data => responseValidation(data));
}

export function create(modelName: string, obj: any) {
    return Rxjs.Observable.fromPromise(sails.models[modelName].create(obj));
}

export function update(modelName: string, obj: any, reqBody: any) {
    return Rxjs.Observable.fromPromise(sails.models[modelName].update(obj, reqBody))
        .mergeMap(data => responseValidation(data));
}

export function destroy(modelName: string, obj: any) {
    return Rxjs.Observable.fromPromise(sails.models[modelName].destroy(obj))
        .mergeMap(data => responseValidation(data));
}

function responseValidation(res: any) {
    if (res['length'] > 0) {
        return Rxjs.Observable.from(res);
    } else {
        return Rxjs.Observable.throw('No Data Found From The Given Object');
    }
}

test cases:

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

require('../../bootstrap.test');

describe('The UserDetailsController', function () {
    /* test for login */
    it('should get all users', function (done) {
        var agent = supertest.agent(sails.hooks.http.app);
        agent
            .get('/users')
            .set('Accept', 'application/json')
            .expect('Content-Type', /json/)
            .expect(200)
            .end(function (err, result) {
                if (err) {
                    done(err);
                } else {
                    result.body.should.be.an('array');
                    done();
                }
            });
    });

the error I am getting is:

Uncaught `include-all` attempted to `require(/Users/user/geocloud/sails-swagger-poc/api-platform/api/services/dbUtils.ts)`, but an error occurred:: 
Details:/Users/user/geocloud/sails-swagger-poc/api-platform/api/services/dbUtils.ts:1
(function (exports, require, module, __filename, __dirname) { import Rxjs = require('rxjs');
                                                              ^^^^^^
SyntaxError: Unexpected token import
Alino commented 6 years ago

I am successfully using this hook on a sails project where I have my models and controllers written in typescript.

I did install typescript this way https://sailsjs.com/documentation/tutorials/using-type-script not with a gulp.