my ajax function somehow not covered by istanbul - javascript

I have a question I have this function
authAnonymous: function (callback) {
rest
.post(wso2config.token.host + "/" + wso2config.token.path, {
username: wso2config.clientId,
password: wso2config.clientSecret,
data: {
username: wso2config.anonymousUser.username,
password: wso2config.anonymousUser.password,
grant_type: "password",
redirect_uri: wso2config.redirect_uri,
scope: "somescope:thisisit"
}
})
.on("complete", function (data) {
if (data.error) callback(data, null);
else {
data.anonym = true;
callback(null, {
openid_data: data
});
}
});
},
so i want to code coverage this function and i make some unit test using Jest here's the unit test code
test("do login using authAnonymous", done => {
openID.authAnonymous(function (error, data) {
if (!error) {
expect(data.anonym).toBeTruthy();
} else {
//expect(data.anonym).toBe(false);
}
});
done();
});
somehow this unit test is working as I expected but code coverage says the statement is not covered
this says statement is not covered
as i understand my code works fine and unit test is working as expected why jest code coverage says this statement not covered can anyone explain how this statement covered works ? and why mine is not covered, i believe this is my fault but i don't know what to investigate here

You are calling done() before everything is done. You need it to be called after all changes and checks are finished:
test("do login using authAnonymous", done => {
openID.authAnonymous(function (error, data) {
if (!error) {
expect(data.anonym).toBeTruthy();
} else {
//expect(data.anonym).toBe(false);
}
done(); // <-- at the end of callback
});
});

Related

How do I write a test for this javascript code?

I'm trying to write a plugin for HomeBridge and have run in to a problem. I started out by just writing some code, start up Homebridge and test it. That worked out find in the beginning but after a while as I added more functionality every time I start HomeBridge there's a lot of testing just to ensure that I haven't broken anything. I mainly work with Java and have just started out with JavaScript. I have copied the patten on how the plugin should be designed. Theres very little documentation about how to write a plugin so I'm kind of in the dark here when it comes to best practice and so on. I have simplified the code so it won't take that much space but the structure is intact. So my question is: How do I test this code?
index.js
let Service, Characteristic;
let request = require('request');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-myplugin", "MyPlugin", MyPluginDevice);
};
function MyPluginDevice(log, config) {
this.log = log;
this.url = config['url'];
this.id = config['id'];
}
MyPluginDevice.prototype = {
getSwitchOnCharacteristic: function(callback) {
this.httpRequest(this.url, null, 'GET', function(error, response, body) {
if (error) {
this.log("Fail: %s", error.message);
callback(error);
} else {
const newState = body['state'];
return callback(null, newState);
}
}.bind(this)
);
},
setSwitchOnCharacteristic: function(state, callback) {
this.httpRequest(this.url, requestBody, 'POST', function(error, response, body) {
if (error) {
this.log("Fail: %s", error.message);
callback(error);
} else {
callback();
}
}.bind(this));
},
httpRequest: function(url, body, theMethod, callback) {
request(
{
url: url,
body: body,
method: theMethod,
auth: {
user: 'nexa',
pass: 'nexa',
sendImmediately: false
},
json: true
},
function(error, response, body) {
callback(error, response, body)
})
},
getServices: function() {
let informationService = new Service.AccessoryInformation();
informationService.setCharacteristic(Characteristic.Manufacturer, "My Nexa plugin").setCharacteristic(Characteristic.Model, "My Nexa Plugin Model").setCharacteristic(Characteristic.SerialNumber, "A very special number");
this.switchService = new Service.Switch(this.name);
this.switchService
.getCharacteristic(Characteristic.On)
.on("get", this.getSwitchOnCharacteristic.bind(this))
.on("set", this.setSwitchOnCharacteristic.bind(this));
return [this.switchService];
}
};
After having been at it for a while I can't write a test for this code. I'm having problem with variables being null and however I try to get around it I always end up with some part of the code not initiated. I have tried:
let MyPluginDevice = require('./index');
let myDevice = new MyPluginDevice(homebridgeMock);
but that leaves me with a problem with getSwitchOnCharacteristic and setSwitchOnCharacteristic. My other approach is to access the MyPluginDevice through my homebridgeMock. But that leaves me with getSwitchOnCharacteristic and setSwitchOnCharacteristicas null or not a function.
I'm kind of out of ideas and my skills is not that good so I can spot the problem or of I have implemented the code in a way that it can't be tested. I got no idea how other developers have done when writing a plugin but I would feel much safer if I could have a few tests running.
Help me Stackoverflow, you are my only hope!

How to deal with async calls in nodejs

I'm new to node.js and I'm having a hard time trying to understand the concept of event-based async programming.
I'm implementing a restful API web service, so consider the following simple (synchronous!) API method addStuff(), which inserts stuff to an elasticsearch db:
var client = new elasticsearch.Client({ host: 'localhost:9200' });
function indexStuff(stuff) {
return client.index({
index: 'test_idx',
type: 'test',
id: stuff.id,
body: stuff
});
}
function addStuff(req, res, next) {
let stuff = processRequest(req);
indexStuff(stuff).then(
function (body) {
return true;
},
function (error) {
res.status(error.status).send({ message: error.message });
}
);
}
So far, so good.
Now during testing I wanted to avoid inserting already existing stuff to the db.
So I'd like to add something like:
function stuffAlreadyInDB(id) {
... // returns true/false
}
function addStuff(req, res, next) {
if (stuffAlreadyInDB(req.id))
{
res.status(409).send({ message: 'stuff with id ' + req.id + ' already in DB' });
return;
}
var stuff = processRequest(req);
...
}
Unfortunately, the call to the elasticsearch db is asyncronous, which means, I can't just return a boolean in a sync function. Instead, I have to refactor the whole shabang to something (argueably less easy to read) like this:
function getStuffByID(id) {
return client.get({
id: id,
index: 'test_idx',
type: 'test',
ignore: 404
});
}
function addStuff(req, res, next) {
getStuffByID(req.id).then(
function(resp) {
if (resp.found) {
res.status(409).send({ message: 'stuff with id ' + req.id + ' already in DB' });
return;
}
else {
var stuff = processRequest(req);
indexStuff(stuff).then(
function (body) {
res.writeHead(200, {'Content-Type': 'application/json' });
res.end();
},
function (error) {
res.status(error.status).send({ message: error.message });
}
);
}
},
function(error) {
res.status(error.status).send({ message: error.message });
}
);
}
At least, I haven't found any better solution. I tried to find out how to make the async call to the db a sync call, but basically everybody was saying: just don't do it.
So how am I supposed to do it right if I don't want to refactor everything and back-factor it when I finished testing and don't need this extra db check anymore?
Oh... and if you downvote my question: leave a comment why you do so.
Because I have the feeling that many people struggle with this issue, but I haven't found a satisfying answer yet.
you could use async\await syntax to make your code readable.
for example you could do this:
async function getStuffById(){
//return true or false; }
and in the "add stuff" function you could write:
if ( await getStuffById() ){
//do some more stuff }
please notice that you have to make "add stuff" async as well in order to use await syntax.
more on async \ await can be found here

Writing JEST unit test for a simple function

Basically this is my react code
getDetails: function () {
var apiUrl = ConfigStore.get('api')
request
.get(apiUrl)
.set('X-Auth-Token', AuthStore.jwt)
.set('Accept', 'application/json')
.end(function (err, response) {
if (!err) {
if(response.text.indexOf("string") > -1){
this.dispatch('COMMAND1', response);
}
else {
this.dispatch('COMMAND2', response.body.options);
}
}
else {
this.dispatch('COMMAND3', response && response.body);
}
}.bind(this));
}
I've written an unit test for the above function of COMMAND1
it('Getting Latest Details', () => {
let eventSpy = sinon.spy();
require('superagent').__setMockResponse({
body: {
firstName: 'blah',
lastName: 'm ',
username: 'blah',
text: {
text : jest.fn()
}
}
});
let dispatchListener = AppDispatcher.register((payload) => {
if (payload.action.type === 'COMMAND1') {
eventSpy(payload.action.payload);
}
});
AuthStore.loggedIn = jest.genMockFunction().mockReturnValue(true);
AuthStore.getToken = jest.genMockFunction().mockReturnValue('545r5e45er4e5r.erereere');
MedsAlertsActions.getDetails();
expect(eventSpy.called).toBe(true);
dispatch('COMMAND1', data);
AppDispatcher.unregister(dispatchListener);
});
When i run
npm test myfile.test
I'm getting
TypeError: Cannot read property 'indexOf' of undefined
So how do i put the indexOf the response in my body? How to resolve the type error
How to write test cases for command2 and command3 as well.
I can see you're using sinon. You can create a sandbox and a fake server in it who return the response expected for each test case. Something like this, for example:
describe('your test suite', () => {
let sandbox;
let server;
beforeAll(() => {
sandbox = sinon.sandbox.create();
server = sandbox.useFakeServer();
});
it('Calls COMMAND1', () => {
//Sinon takes some ms to respond, so you have to use a setTimeout
setTimeout(
() => server.respond([200, { 'Content-Type': 'text/html' }, 'some string']),
0
);
// Put here your assertions
});
});
You can use server.restore() and sandbox.restore() to clean each one when you needed. Besides, you can access the requests made with sandbox.requests
Here's a great post that may help you: https://medium.com/#srph/axios-easily-test-requests-f04caf49e057, it's about axios, but you can implement it in the same way.
Also, you can know more about it at the official sinon documentation for sandboxes: http://sinonjs.org/releases/v1.17.7/sandbox

Cannot insert to collection from NPM using Meteor 1.3

I am using the imap-simple NPM package to check emails, and I am having trouble getting the insert to work properly.
I have already read through this page: https://guide.meteor.com/using-npm-packages.html#async-callbacks - and I have tried the suggestions but none of them are working!
I've also simplified the code a bit just to try to get it working, but still have no luck.
The problem should be very easy to reproduce - meteor npm install imap-simple, throw the above code on the server, add some email credentials, and call the method.
Here is my code:
var imaps = require('imap-simple');
var config = {
imap: {
user: '<removed>',
password: '<removed>',
host: 'imap.gmail.com',
port: 993,
tls: true,
authTimeout: 3000
}
};
Meteor.methods({
api_connectEmail: function () {
console.log('Received call to connect email');
imaps.connect(config).then(function (connection) {
return connection.openBox('INBOX').then(function () {
var searchCriteria = [
'UNSEEN'
];
var fetchOptions = {
bodies: ['HEADER', 'TEXT'],
markSeen: true
};
return connection.search(searchCriteria, fetchOptions).then(function (results) {
results.map(function (res) {
var subject = res.parts.filter(function (part) {return part.which === 'HEADER';})[0].body.subject[0];
console.log("Subject: " + subject);
// insert
var attributes = {
subject: subject
};
console.log("Attempting to insert to collection...");
var newData = TempEmailCollection.insert(attributes);
console.log("New Database Entry ID: " + newData);
});
});
});
})
}
});
The console.log with the subject is working. The insert is not working. No error, no console.log post insert, nothing.
I've tried both strategies recommended in the guide, neither work.
The problem is that you are calling a Meteor function inside asynchronously called Promise handlers.
However, all Meteor functions that are called on the server have to run in a fiber.
Meteor actually throws an error in this case but you are ignoring it because you haven't specified catch functions for the Promises.
Consider the following simplified example (it just connects to the server and tries to insert a new document):
import { Meteor } from 'meteor/meteor';
import imaps from 'imap-simple';
const Storage = new Mongo.Collection('storage');
const config = {
imap: {
…
}
};
Meteor.methods({
connect() {
console.log('Method called');
imaps.connect(config).then(function(connection) {
console.log('Connected');
Storage.insert({
value: 'success'
});
console.log('Document inserted');
})
.catch(function(err) {
console.error(err);
});
}
});
The following message will arrive in the catch function:
[Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.]
You could do something like this to wrap the insert call:
Meteor.methods({
connect() {
console.log('Method called');
const insert = Meteor.bindEnvironment(function() {
Storage.insert({
value: 'success'
});
});
imaps.connect(config).then(function(connection) {
console.log('Connected');
insert();
console.log('Document inserted');
})
.catch(function(err) {
console.error(err);
});
}
});
Then the document will be inserted as expected.

Why is my Meteor app logging to server but not client?

I'm building a meteor app that hooks into the twitter api and I've had no luck so far getting it to work. I'm using the twit package to make the call on the server side, and it logs the data to the server console, but when the client console goes to log it there is no data.
The client doesn't throw an error, it runs the console.log in the else statement for the result parameter, but it comes through as undefined. It's as if the result callback runs before the data comes back, but my understanding of the Meteor.call method is that it's supposed to wait until it hears back from the server before it runs.
What am I doing wrong here?
if (Meteor.isClient) {
Template.hello.greeting = function () {
return "Welcome to testing.";
};
Template.hello.recentFollows = function () {
return Session.get("recentFollows");
};
Template.hello.events({
'click #fetchButton': function () {
console.log("Recent tweets from stream!");
userName = "josiahgoff";
Meteor.call('getBananaTweets', function(err, result) {
if(err) {
console.log("error occurred on receiving data on server. ", err);
} else {
console.log("result: ", result);
Session.set("recentFollows", result);
}
});
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
Twit = new TwitMaker({
consumer_key: '******',
consumer_secret: '******',
access_token: '******',
access_token_secret: '******'
});
});
Meteor.methods({
getBananaTweets: function () {
Twit.get('search/tweets', { q: 'banana since:2011-11-11', count: 1 }, function(err, result) {
if (err) {
console.log("Error", err);
return err;
} else {
console.log(result);
return result;
}
});
}
});
}
You are using return in your server code in a place where it must not be used: in an asynchronous call-back. The Twit.get call returns immediately and the function ends (with no return value). So the client doesn't receive anything. Some time later the Twit.get comes back, but the return in that case goes nowhere.
This is a pretty common question. The solution is to wrap your Twit.get call into a fiber in some shape or form to make it synchronous. See for instance this answer: Iron Router Server Side Routing callback doesn't work

Categories

Resources