0

I wrting API test in Mocha JS, have two files: 00_auth.js and utils.js I want to create function in utils which is available in other files from same folder

utils.js

utils = {
   randomStringGenerator: function (){
    return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 7);
  }
};

and test js

const chai = require('chai');
const mocha = require('mocha');
const config = require('../config');
const request = require('supertest')(url);
const assert = chai.assert;
const utils= require('./utils')



randomFirstName=utils.randomStringGenerator();
//randomLastName=stringGenerator();
randomMail=randomFirstName+'@'+randomLastName+'.pl';
auth_token = ''



describe('0_auth', () => {
    it('should return token for unauthorized user', function(done) {
    request
      .post('/rest/v1/auth/get-token')
      .set(config.headers) 
      .send({
          "deviceUuidSource": "DEVICE",
          "source" : "KIOSK",
          "deviceUuid" : "uniquedeviceuuid"
      })
      .end(function(err,res){
        assert.equal(res.status,200)
        assert.property(res.body, 'token')
        assert.isString(res.body.token)
        auth_token=res.body.token
        console.log('unathorized token: '+auth_token) 
        done(err);
      });       
   });
});

I took this solution from Global functions in javascript, but it's not working, gives me Type Error :utils.randomStringGenerator is not a function

1 Answers1

0

OK, got it, thanks

module.exports.randomStringGenerator = function (){
    return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 7);
  }