I am creating a unit test, this test consists of using the twitter API to obtain data of any profile (Name, number of tweets, followers, date of registration, etc). The point is that I'm using the Mocha library for this task, but I run into the problem, that when executing the assert, the twitter request takes more than 2 seconds (Mocha's default timeout), therefore my test always fails. Could someone give me a hand? What should I do, so that the comparison of the assert is executed after receiving all the data of the request to twitter?
code.js
class extractCode {
verificarUsername(a) {
if (a) {
var nombre;
var Twitter = require('twitter');
var client = new Twitter({
consumer_key: 'XSpj4nEB5IOLgIvyZXiDAhBLt',
consumer_secret: 'dPjYUTih6n0ynt1y9C7bE0g0gyx6KSJgrGTeDEa3yH5flsdJPL',
access_token_key: '900532686-4sOwDfOFZm1fKmtZZhSMPH04REXMjqnugTOn3o1j',
access_token_secret: 'ghNtHt7VyjwNHXvXnZM5hFKaDH62bX7LEFqUBZ9SSb5Lg'
});
client.get('users/show', {screen_name: a}, function(error, response) {
if(error) throw error;
if(response.name) {
nombre = true
} else {
nombre = false
};
console.log(`
Nombre: '${response.name}'
ID: ${response.id_str}
Localidad: ${response.location}
Descripción del perfil: ${response.description}
Seguidores: ${response.followers_count}
Sigue a: ${response.friends_count}
Perfil creado el: ${response.created_at}\n`);
});
return nombre;
}
}
}
module.exports = extractCode;
test/prueba.js
var assert = require('assert');
var extractCode = require('../code.js');
describe('Pruebas de perfil de Twitter', function() {
this.timeout(5000);
var c = new extractCode();
it('Verifica si se le pasó un username válido', function(done) {
assert.equal(c.verificarUsername('pedrofumero'),true, 'El username proporcionado no es válido');
done();
})
})