Unit.js , Mocha example.js fails 0 passed - javascript

I am just learning Unit.js with mocha. I am following the steps at: http://unitjs.com/guide/quickstart.html . I have installed mocha and am running the "example.js" script. The script fails with
$ mocha example.js
0 passing (1ms)
Running the example.js in with -full-trace I get:
$ mocha example.js -full-trace
C:\Users\rball\AppData\Roaming\npm\node_modules\mocha\lib\utils.js:652
throw new Error("cannot resolve path (or pattern) '" + path + "'");
^
Error: cannot resolve path (or pattern) '-t'
at Object.lookupFiles (C:\Users\rball\AppData\Roaming\npm\node_modules\mocha\lib\utils.js:652:15)
at C:\Users\rball\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:326:30
at Array.forEach (native)
at Object.<anonymous> (C:\Users\rball\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:325:6)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:134:18)
at node.js:961:3
I am running this with cygwin64. and running in the directory: /home/rball/test
the example.js is:
// load Unit.js module
var test = require('../node_modules/unit.js');
// just for example of tested value
var example = 'hello';
// assert that example variable is a string
test.string(example);
// or with Must.js
test.must(example).be.a.string();
// or with assert
The unit.js is in the node_modules directory.
The error in the full trace output is baffling since I am not aware of any path with a "-t" in it.
What am I missing?
Thanks for your help

I think you need an extra '-' in front of '-full-trace'.
Example: '--full-trace'
You set your asserter with 'test.string(example)'. You've already passed it the specified variable type, which would be fine if you were validating the string value, length, etc.
Try this instead:
'test.value(example).isString();'
----or----
'test.value(example).must.be.a.string();'
http://unitjs.com/api/value.html#isString

Related

jasmine with node picks up and fails on an unrelated (ie nothing to do with node) package.json file

I have a following structure
root/Test/runtests.js
root/Test/package.json # npm
root/packages/local/foo/package.json # not npm
root/packages/local/foo/spec/ # root for tests
runtests.js:
var Jasmine = require('jasmine');
var jasmine = new Jasmine();
var spec_files = process.argv[2] + '/**/*Spec.js';
jasmine.loadConfig({
spec_dir: '../',
helpers: [],
spec_files: [
spec_files
],
stopSpecOnExpectationFailure: false,
random: true
});
jasmine.execute();
And I run it from inside root/Test with node runtests.js packages/local/foo/spec. Whatever I try, it keeps failing by trying to parse packages/local/foo/package.json. It's not surprising it fails, because that json file isn't actually valid json and uses /* syntax for block comments, but that's beside the point -- why is it being parsed to begin with? How can it be excluded? I tried using ! syntax in spec_files definition, to no avail. Here is what stacktrace look like
(node:48272) UnhandledPromiseRejectionWarning: SyntaxError: Error parsing root\packages\local\foo\package.json: Unexpected token / in JSON at position 7
at JSON.parse (<anonymous>)
at readPackage (internal/modules/cjs/loader.js:239:25)
at readPackageScope (internal/modules/cjs/loader.js:263:19)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:970:17)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Loader.requireShim [as require_] (root\Test\node_modules\jasmine\lib\loader.js:35:3)
at root\Test\node_modules\jasmine\lib\loader.js:28:12
I'm using jasmine ^3.9.0

Running a JavaScript program requiring jsfeat

I have downloaded jsfeat from here, and unzipped the downloaded file.
Inside the directory, I wrote the following:
require("./build/jsfeat.js");
var matrix = new jsfeat.matrix_t(2,2, jsfeat.U8_t | jsfeat.C1_t);
matrix.data[1] = 4;
console.log(matrix.data[1]);
I ran the program as follows:
$ node matrix.js
But, got the following error:
/Users/abc/Desktop/JavaScript/inspirit-jsfeat-59cc928/matrix.js:2
var matrix = new jsfeat.matrix_t(2,2, jsfeat.U8_t | jsfeat.C1_t);
^
ReferenceError: jsfeat is not defined
at Object.<anonymous> (/Users/abc/Desktop/JavaScript/inspirit-jsfeat-59cc928/matrix.js:2:19)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:136:18)
at node.js:972:3
Why is that? How can I solve the issue?
Thanks.
You need to assign the imported module to a variable:
var jsfeat = require("./build/jsfeat.js");
If you prefer you could install the module with NPM to save downloading it manually:
$ npm install jsfeat
The module will be saved to ./node_modules/jsfeat. You can then require it with var jsfeat = require('jsfeat').

Cannot access child_process API

I have the following tiny program.js which tries to execute a binary file:
var childProcess = require('child_process');
var path2Binary = '/home/myuser/myproj/bins/mybin';
var par = '--file=' + '/home/myuser/myproj/files/myfile.txt';
var ret = childProcess.execFileSync(path2Binary, [par]);
if (!ret) throw 'Error invoking process!';
var cnt = ret.stdout;
if (!cnt) throw 'Error retrieving output!';
console.log(cnt);
The program tries to execute a binary file and passes it a parameter (a file). The output of this process will be then displayed.
I try to run this: node program.js, but get the following
var ret = childProcess.execFileSync(path2Binary, [par]);
^
TypeError: Object #<Object> has no method 'execFileSync'
at Object.<anonymous> (/home/myuser/myproj/program.js:6:24)
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:929:3
More information
I am running on CentOS, Node version is v0.10.36.
I tried running sudo yum install nodejs, but it tells me it is already installed so Node installation looks kinda good.
What's the problem?
On a side note...
If I replace childProcess.execFileSync with childProcess.spawn I get the same.
If I change the first line into the following:
var exec = require('child_process').execFileSync;
Then I get an undefined exception on exec.
Synchronous child processes aren't supported in node v0.10.36 - https://nodejs.org/docs/v0.10.36/api/child_process.html
Looks like it may have been introduced in 0.12.

Import a Javascript file into a REPL session

I'm on Windows 10 TP build 9926, using nodejs. I want to be able to import a Javascript into a current session of nodejs that is running on Windows command prompt so that functions from the imported script can be called from within the REPL. How can I achieve that? My attempt of "import" and "require" did not succeed.
I tried the following in a nodejs session running from the directory that has the "learn.js" javascript;
var n = require("learn.js")
Then got the following error message:
Error: Cannot find module 'learn.js'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at repl:1:9
at REPLServer.defaultEval (repl.js:132:27)
at bound (domain.js:254:14)
at REPLServer.runBound [as eval] (domain.js:267:12)
at REPLServer.<anonymous> (repl.js:279:12)
at REPLServer.emit (events.js:107:17)
The learn.js file needs to look like the following:
"use strict";
module.exports.myfunc1 = function() {
console.log("Hi there.");
}
module.exports.myfunc2 = function() {
console.log("Goodbye.");
}
Also, you must require it like this:
var n = require('./learn.js');
or
var n = require('./learn');
You must make the path relative to where you are running the REPL. Node won't automatically check the local directory.

Getting "cannot set property Error of undefined" when trying to run the .js output of a simple hello world clojurescript

I'm compiling:
(ns example.hello)
(js/console.log "Hello from ClojureScript!")
With this configuration:
(defproject lein-cljsbuild-example "1.2.3"
:plugins [[lein-cljsbuild "0.2.9"]]
:cljsbuild {
:builds [{
:source-path "src-cljs"
:compiler {
:output-to "war/javascripts/mainz.js" ; default: main.js in current directory
;:optimizations :simple
:target :nodejs
;:pretty-print true
}}]})
Which outputs a file that is too big to put here, but gives the error:
goog.debug.Error = function(opt_msg) {
^
TypeError: Cannot set property 'Error' of undefined
at Object.<anonymous> (/Users/myuser/Clojure/cljstest/war/javascripts/mainz.js:503:18)
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:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Well, in the project config you give, your :optimizations :simple clause is commented out. This means that it will not have any Google Closure optimizations, meaning that the output JavaScript will not be in one sufficient file, but broken into many files. Which also means you must explicitly include base.js from the Google Closure library.
It looks like that's what's happening here, although there might be other stuff going on as well... I'm actually not that familiar with the node.js output for ClojureScript.
The error has been solved by reinstalling leiningen and doing a clean build.

Categories