mocha test problem in Ubuntu:ReferenceError: ert is not defined - node.js

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

Related

Node.js Mocha Error: ReferenceError: test is not defined

I've got an error after running test scripts.
app.test.js
const { app } = require('../src/app');
describe('Basic functionality', () => {
test('App should receive a command to run', () => {
expect(app('1'))
.toEqual('Run command 1');
});
test('Should return instructions when no command is given', () => {
expect(app())
.toEqual('Usage: node index.js <COMMAND>');
});
test('Should throw an error when no such command exists', () => {
expect(() => {
app('100')
})
.toThrow('No such command exists');
});
});
package.json (I've installed mocha in my project. I've tried to install mocha globally, but the result was same)
"scripts": {
"test": "mocha __tests__/app.test.js"
},
"devDependencies": {
"mocha": "^8.0.1"
}
I've got an error after running
yarn run test
Below is the error code
ReferenceError: test is not defined
at Suite.describe (/test/__tests__/app.test.js:4:2)
at Object.create (/test/node_modules/mocha/lib/interfaces/common.js:143:19)
at context.describe.context.context (/test/node_modules/mocha/lib/interfaces/bdd.js:42:27)
at Object.<anonymous> (/test/__tests__/app.test.js:3:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789: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 Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.exports.requireOrImport (/test/node_modules/mocha/lib/esm-utils.js:20:12)
at Object.exports.loadFilesAsync (/test/node_modules/mocha/lib/esm-utils.js:33:34)
at Mocha.loadFilesAsync (/test/node_modules/mocha/lib/mocha.js:421:19)
at singleRun (/test/node_modules/mocha/lib/cli/run-helpers.js:156:15)
at exports.runMocha (/test/node_modules/mocha/lib/cli/run-helpers.js:225:10)
at Object.exports.handler (/test/node_modules/mocha/lib/cli/run.js:366:11)
at innerArgv.then.argv (/test/node_modules/yargs/lib/command.js:241:49)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
Please teach me where I went wrong.
I solved it by specifying the route:
Like this:
npx mocha "src/testing-examples/index.test.js" --recursive --require #babel/register
I hope you find it useful :)

Node mocha test case is not running

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.

gulp + mocha + typescript unit test

I have an error when I create new instance on test file.
This is my test:
/// <reference path="../typings/globals/mocha/index.d.ts" />
import Person from '../src/person/person';
describe('Person', () => {
let person: Person;
beforeEach(() => {
person = new Person();
});
describe('getName', () => {
it('return name', () => {
});
});
});
And my gulp task:
var gulp = require("gulp");
var ts = require("gulp-typescript");
var mocha = require("gulp-mocha");
var tsProject = ts.createProject("./tsconfig.json");
gulp.task('watch', function () {
gulp.watch('./src/**/**/*.ts', ['ts']);
});
gulp.task('ts', function () {
return tsProject.src()
.pipe(tsProject())
.js.pipe(gulp.dest("dist"));
});
gulp.task('test', function () {
return gulp.src('./tests/*.spec.ts',
{
base: '.'
})
/*transpile*/
.pipe(tsProject())
/*flush to disk*/
.pipe(gulp.dest('.'))
/*execute tests*/
.pipe(mocha())
.on("error", function(err) {
console.log(err)
});
});
gulp.task("default", ['watch', 'ts']);
So, when I launch the test an error occured, but if I comment person = new Person() then everything works.
Somebody knows what I am doing wrong?
EDIT:
Sorry, this is the error:
module.js:472
throw err;
^
Error: Cannot find module '../src/person/person'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/emesislol/projects/persons/tests/person.test.js:4:21)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at /Users/emesislol/projects/persons/node_modules/mocha/lib/mocha.js:222:27
at Array.forEach (native)
at Mocha.loadFiles (/Users/emesislol/projects/persons/node_modules/mocha/lib/mocha.js:219:14)
at Mocha.run (/Users/emesislol/projects/persons/node_modules/mocha/lib/mocha.js:487:10)
at Object.<anonymous> (/Users/emesislol/projects/persons/node_modules/mocha/bin/_mocha:459:18)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:420:7)
at startup (bootstrap_node.js:139:9)
at bootstrap_node.js:535:3

Error: Cannot find module 'zeparser'

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.

runnuning echo server on Ubuntu

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

Resources