I am writing the test code for a function that will check for the existence of a username in twitter. I am using the Hapi framework and Lab for testing.
When I run npm test I get the following error:
> NameGen#0.0.0 test /Users/mario/projects/NameGen
> ./node_modules/lab/bin/lab -c
/Users/mario/projects/NameGen/test/test.js:5
Lab.experiment( "Test Username Existence", function() {
^
TypeError: Object #<Object> has no method 'experiment'
at Object.<anonymous> (/Users/mario/projects/NameGen/test/test.js:5:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js [as origLoader] (module.js:474:10)
at Object.require.extensions..js (/Users/mario/projects/NameGen/node_modules/lab/lib/coverage.js:32:26)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at /Users/mario/projects/NameGen/node_modules/lab/lib/cli.js:85:23
at Array.forEach (native)
at Object.internals.traverse (/Users/mario/projects/NameGen/node_modules/lab/lib/cli.js:81:19)
at Object.exports.run (/Users/mario/projects/NameGen/node_modules/lab/lib/cli.js:30:29)
at Object.<anonymous> (/Users/mario/projects/NameGen/node_modules/lab/bin/lab:3:23)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0
My test code is:
var Lab = require("lab");
var server = require('../'); // require index.js
var request = require('request');
Lab.experiment( "Test Username Existence", function() {
// tests
Lab.test( "Test username exists", function(done){
var options = {
url: 'http://twitter.com/BarackObama'
};
request(options, function(error, response, body){
Lab.expect(response.statusCode).to.equal(200);
});
done();
});
Lab.test("Test username does not exists", function(done){
var options = {
url: 'http://twitter.com/jhkhksdhkjahsdfkjhasdf'
};
request(options, function(error, response, body){
Lab.expect(response.statusCode).to.equal(404);
});
done();
});
});
I have the latest version of node installed and all the necessary dependencies in my package.json file. What could be causing this error?
As #Jordonias had stated. However, when I used his example, it still did not work.
I had to change the 2nd line to:
lab = exports.lab = Lab.script()
Per the Lab docs: Test files must require the lab module, and export a test script
test.js
var Lab = require('lab');
var lab = module.exports = Lab.script();
lab.experiment( "Test Username Existence", function() {
lab.test( "Test username exists", function(done){
});
});
Related
I am using mocha to test my node js code. While running on windows machine, I am able to test my application successfully. But when I am trying to test same application project in Ubuntu , I am getting ReferenceError: ert is not defined . So i did "rm -rf node_modes" and "npm i" again . But the issue still persist.
Error in terminal
$ npm test
> myapplication#1.0.0 test /NodeProject/MyApplication
> mocha Test/* --require #babel/register
/NodeProject/MyApplication/node_modules/yargs/yargs.js:1163
else throw err
^
ReferenceError: ert is not defined
at Object.<anonymous> (/NodeProject/MyApplication/Test/app.test.js:1:4)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Module._compile (/NodeProject/MyApplication/node_modules/pirates/lib/in dex.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Object.newLoader [as .js] (/NodeProject/MyApplication/node_modules/pira tes/lib/index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at /NodeProject/MyApplication/node_modules/mocha/lib/mocha.js:330:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/NodeProject/MyApplication/node_modules/mocha/lib/moch a.js:327:14)
at Mocha.run (/NodeProject/MyApplication/node_modules/mocha/lib/mocha.js:8 04:10)
at Object.exports.singleRun (/NodeProject/MyApplication/node_modules/mocha /lib/cli/run-helpers.js:207:16)
at exports.runMocha (/NodeProject/MyApplication/node_modules/mocha/lib/cli /run-helpers.js:300:13)
at Object.exports.handler.argv [as handler] (/NodeProject/MyApplication/no de_modules/mocha/lib/cli/run.js:296:3)
at Object.runCommand (/NodeProject/MyApplication/node_modules/yargs/lib/co mmand.js:242:26)
at Object.parseArgs [as _parseArgs] (/NodeProject/MyApplication/node_modul es/yargs/yargs.js:1087:28)
at Object.parse (/NodeProject/MyApplication/node_modules/yargs/yargs.js:56 6:25)
at Object.exports.main (/NodeProject/MyApplication/node_modules/mocha/lib/ cli/cli.js:63:6)
at Object.<anonymous> (/NodeProject/MyApplication/node_modules/mocha/bin/_ mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm ERR! Test failed. See above for more details.
And here is my app.test.js file
var assert = require('assert')
, expect = require('expect.js'),
W3CWebSocket = require('websocket').w3cwebsocket;
describe('Test Suite', function() {
var client;
beforeEach(function(done) {
client = new W3CWebSocket('ws://localhost:8091/', 'echo-protocol');
client.onopen = function() {
console.log('WebSocket Client Connected');
done();
};
client.onclose = function() {
console.log('echo-protocol Client Closed');
};
});
afterEach(function(done) {
if(client!=null){
client.close();
}
done();
});
it('My App result', (done) => {
if (client.readyState === client.OPEN) {
client.send(`"data":"dummy"}`);
}
client.onmessage = function(e) {
let data = e.data;
expect(data).to.not.equal(null);
expect(data).to.not.equal(undefined);
done();
};
});
});
I faced similar issue. It was because babel version in package.json is different from the one whose specific settings are set in .babelrc file
Try deleting .babelrc file and see if it works.
The ert probably is from
.../node_modules/babel-core/lib/transformation/file/options/option-manager.js:128
log.error or log.ert
If not search whole directory for ert, that should give you some clue
I am trying to run the following code with node.
var webdriverio = require('webdriverio');
var options = { desiredCapabilities: { browserName: 'chrome' } };
var client = webdriverio.remote(options);
client
.init()
.url('http://www.webdriveruniversity.com/')
.click('#login-portal')
.getTitle().then(function(title) {
console.log('Title is: ' + title);
})
.end();
I am getting this error
DEBUG wdio-config: #wdio/sync not found, runn
ing tests asynchronous
C:\Users\Adnan\Desktop\webdriverFramework\loginPortalTest.js:6
.init()
^
TypeError: client.init is not a function
at Object.<anonymous> (C:\Users\Adnan\Desktop\webdriverFramework\l
oginPortalTest.js:6:6)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:7
00:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
How can I resolve this? Previously I had the issue with version. But I upgraded to new version.
If I understand correctly, you lack #wdio/sync and your runner toggles into asynchronous mode. So webdriverio.remote(options) returns promise that has not .init() method.
I'm still new at node.js and I'm trying to get my small API to return data. As you can see here
the code works perfectly on this online editor, but locally while running node it gives an error.
My code:
var http = require('http');
var options = { method: 'GET',
url: 'https://eftelingapi.herokuapp.com/attractions',
headers:
{'cache-control': 'no-cache'};
request(options, function (error, response, body) {
if (error) throw new Error(error);
var jsonData = JSON.parse(body);
console.log(jsonData.AttractionInfo[0]);
});
The error:
ReferenceError: request is not defined
at Object.<anonymous> (/Users/ricky/Desktop/eftelApp/app.js:9:1)
at Module._compile (internal/modules/cjs/loader.js:707:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
at Module.load (internal/modules/cjs/loader.js:605:32)
at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
at Function.Module._load (internal/modules/cjs/loader.js:536:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:760:12)
at startup (internal/bootstrap/node.js:308:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:878:3)
Hopefully anyone knows what's wrong in my code or what I can fix on my local node? I've installed the "request" package already, but that didn't seem to do it
Try this
http.request()
check this for more info.
I have node.js successfully installed. I have created the file
var http = require('http');
var url=require('url');
var fs=require('fs');
var io = require('socket.io');
http.createServer(function (req, res) {
fs.readFile('/var/www/nodeJS/client.html' ,
function ( err, data ) {
if ( err ) {
console.log( err );
res.writeHead(500);
return res.end( 'Error loading client.html' );
}
res.writeHead( 200 );
res.end( data );
});
}).listen(8124, '127.0.0.1');
io.listen(http);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
The error occurs whenever I use the io object. Without io it works fine.
Error: Cannot find module 'zeparser'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/var/www/nodeJS/node_modules/socket.io/node_modules/socket.io-client/node_modules/active-x-obfuscator/index.js:1:78)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
After this I have also separately installed module zeparser. My default npm installation directory seems to be /usr/local/lib/node_modules.
I have set NODE_PATH as
export NODE_PATH="/usr/local/lib/node_modules"
but get the same error.
Then I tried to copy zeparser module to /var/www/nodeJS/node_modules. Then the error changes to
/var/www/nodeJS/node_modules/socket.io/lib/manager.js:104
server.on('error', function(err) {
^
TypeError: Object #<Object> has no method 'on'
at new Manager (/var/www/nodeJS/node_modules/socket.io/lib/manager.js:104:10)
at Object.exports.listen (/var/www/nodeJS/node_modules/socket.io/lib/socket.io.js:78:10)
at Object.<anonymous> (/var/www/nodeJS/app.js:35:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:903:3
On your root project, do:
$ sudo npm install zeparser
It resolved the problem for me.
I followed this tutorial to install the node.js on ubuntu and installation is successful. But when i type node echo-server.js i am getting following error:
module.js:340
throw err;
^
Error: Cannot find module '../../lib/ws/server'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/home/jci/node/joyent-node-283d735/echo-server.js:2:10)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:487:10)
echo server code:
var sys = require("util")
, ws = require('../../lib/ws/server');
var server = ws.createServer({debug: true});
// Handle WebSocket Requests
server.addListener("connection", function(conn){
conn.send("Connection: "+conn.id);
conn.addListener("message", function(message){
conn.send("<"+conn.id+"> "+message);
if(message == "error"){
conn.emit("error", "test");
}
});
});
server.addListener("error", function(){
console.log(Array.prototype.join.call(arguments, ", "));
});
server.addListener("disconnected", function(conn){
server.broadcast("<"+conn.id+"> disconnected");
});
server.listen(8000);
But lib/ws/server is missing. But where do i get it from??
I don't have web socket server in my node.js installation i guess.
Thanks
Sneha