Node command doesn't execute asterisk path - javascript

I have a folder Test/Automation contains many test cases, e.g. a.js, b.js, c.js, etc.
I am using WebdriverJs Selenium. I use this command to execute all the tests in this folder:
node Test/**/*.js
But only a.js was executed and then it's done. Anyone knows the reason why? Is there something wrong with this? Thanks

You have to declare that js in server.js.
For example:
var express = require('express'),
bodyParser = require('body-parser'),
modulea = require('./modules/a'),
moduleb = require('./modules/b')

If you have multiple *.js (javascript) files in same directory as well. node wont run multiple files in same command line. eg if you had a.js,b.js,c.js within test and then,
node test/*.js
will return output of a.js only.
Another proposal,
Assuming you doing some form of automation with different scenarios,
try creating a batch executable (or bash script in case of linux) with something like this,
node test/l1/a.js
node test/l1/a1.js
node test/l2/b.js
this will give you additional option to control test/js to execute.

Thanks guys, I have found a solution, I wrapped Js in mocha format, then I can them all with this command :
mocha Test/Automation/*.js
It works as I want.

Related

How to use node Buffer module in a client side browser - detailed explanation required please

First things first. I know that there are other questions that are similar to this e.g. use NodeJs Buffer class from client side or
How can I use node.js buffer library in client side javascript
However, I don't understand how to make use of the reference to use browserify though it is given approval.
Here is my Node code:
import { Buffer } from 'buffer/';
I know this is the ES6 equivalent of require.
I would like a javaScript file implementation of this module so that I can simply use the standard html file reference:
<script src=./js/buffer.js></script>
And then use it as in for example
return new Buffer(temp).toString('utf-8');
This simply falls over with the
Uncaught ReferenceError: Buffer is not defined
no matter how I create the buffer.js file.
So using the browserify idea I've tried using the standalone script (from the https://www.npmjs.com/package/buffer as https://bundle.run/buffer#6.0.3 )
I've created a test.js file and put
var Buffer = require('buffer/').Buffer
in it and then run browserify on it as
browserify test.js -o buffer.js
and many other variations.
I'm not getting anywhere. I know I must be doing something silly that reflects my ignorance. Maybe you can help educate me please.
These instructions worked for me. Cheers!
Here are the instructions you can look at the web section.
https://github.com/feross/buffer
Here is what the instructions state about using it in the browser without browserify. So from what you tried
browserify test.js -o buffer.js
I would just use the version directly that does not require browserify
To use this module directly (without browserify), install it:
npm install buffer
To depend on this module explicitly (without browserify), require it like this:
var Buffer = require('buffer/').Buffer // note: the trailing slash is important!

Is it possible to call npm CLI app's main JS file as if it was ran within a subfolder?

I have a small npm CLI library which fixes errors in changelog.md's sitting at the same directory where CLI is called. I'm trying to write at least one unit test for it's CLI part (its API is a separate library and it's well-tested).
It's easy to trigger CLI's main JS file using execa library, like:
execa.sync('./cli.js')
but how do you trigger cli.js sitting in a root and meant to be looking for a ./changelog.md in a root, to execute on a different test file in a subfolder (/test/changelog.md)?
Mind you, there's real changelog.md file in the root which has nothing to do with unit test.
(Worst case scenario, we could copy cli.js into /test/, call it there via execa and delete later, but this looks not DRY solution)
My current AVA unit test is below. seed is incorrect changelog.md file. intended is correct changelog.md against which we'll compare later. I write seed contents into /test/changelog.md and want to run a CLI on it. Problem: how do I run ./cli.js against /test/package.json? Mind you, I still want my CLI API to be simple, not accept any arguments with paths (what could be a last resort solution — to accept custom path arguments).
My current test is nearly there, but it runs against root, ./package.json contents, not /test/package.json (which is how API would behave for end-user):
import path from 'path'
import fs from 'fs'
import test from 'ava'
import execa from 'execa'
test('01.01 - reads and writes correctly', t => {
var seed = fs.readFileSync(path.join('test', 'seed.md'), 'utf8')
var intended = fs.readFileSync(path.join('test', 'intended.md'), 'utf8')
fs.writeFileSync(path.join('test', 'changelog.md'), seed, 'utf8')
execa.sync('./cli.js') // <<< PROBLEM, this runs on root, not on ./test/changelog.md!
t.deepEqual(
fs.readFileSync(path.join('test', 'changelog.md'), 'utf8'),
intended,
'01.01'
)
})
How do you execute an npm CLI package on a different directory when unit testing, if it's meant to be ran on file present at the same level as CLI's JS file?
Thank you.

Wepback require from variable

I am making console utility which accepts a path to configuration file as a console argument.
F.e: utility -f /path/to/file.js
I need to require this file to read configuration. Is it possible to handle this with webpack? As I understand context can not help me in this situation.
Thanks.
P.S. I'm already using webpack.
P.S Solution is to use something like: eval('require')(dynamicPath)
Webpack can only do a dynamic require like this if the file to be required is available at compile time. For example, if you require a "dynamic" file path, like
require('./assets/images/' + someVariable + '.png')
Then under the hood, Webpack will include all images matching that pattern in the bundled require code. It basically will include all files matching the regex:
/.\/assets\/images\/*.png/
I would probably try putting the config file in a specific folder, and using require on that folder:
require('./configs/' + process.env.CONFIG_NAME);
This way Webpack will only include all files in the configs folder.
The Webpack documentation is horrible but there is more information on the dynamic requires page.
If you are passing in a config file as an argument to a node process, it will be accessible in the process.argv array of command line arguments. I don't know if you are using some other framework (like the excellent commander) to help with making command line programs, but we can just slice the array to find what we need.
To resolve a path from the directory the script is launched in, you can use process.cwd() - this returns an absolute path to the working directory of the node process.
Finally you can use path.resolve(processPath, configPath) (docs) to generate a path that is always guaranteed to resolve to the config. You can then require this path.
You probably need to do this first. The top of your file could look something like this:
/* relevant require() calls for necessary modules */
var path = require('path');
// first two arguments are node process and executed file
var args = process.argv.slice(2);
var configIndex = args.findIndex('-f') + 1;
var configPath = path.resolve(process.cwd(), args[configIndex]);
var config = require(configPath);
/* the rest of your code */

How to run npm {bin: script.js} with parameters

I am planing to make something similar as lodash custom builds. So in general I want to let user write command like:
lodash category=collection,function
Which create custom module just with category i specified
I read few tutorials how to run scripts with npm bin. Just in case I understand something wrong I write it what i think.
So if I have package.json with this part:
"main": "bin/index.js",
"bin": {
"snippet": "bin/index.js"
},
and I npm install -g console should listen for command snippet and when i write it it run the script index.js in folder bin.
This part looks it works correctly for me. When i have something simple in my index.js i.e. console.log('It Works').
In standard situation you want to let user pass parameters to script. So i found out that all parameters should be in variabile process.argv.
The process.argv property returns an array containing the command line
arguments passed when the Node.js process was launched. The first
element will be process.execPath. The second element will be the path
to the JavaScript file being executed. The remaining elements will be
any additional command line arguments.
So i simply console.log it and run script.
If I run script via command snippet -f -a
Output is : [ 'node', 'path/to/file' ]
If i run script via node bin/index.js -f -a
Output is: [ 'node', 'path/to/file', '-f', '-a' ]
I dont understand that, its same script but different output. However I try it looks like when i call script via bin command it never pass parameters.
Is here someone who have experience with this? And advise me what i am doing wrong?
Or alternativly is there some other way how to make this?
Thanks for any advise.
It take a time however I have a solution now so hope it help someone later.
Where was a problem:
I noticed that my windows has default program to run .js file set to NODE.js and because it's default setting of course all .js files are opening without parameter.
So in my case every .js file open with NODE no matter what, I try to changed it to open with something like PSPAD or similar but this basicly open editor instead of execute file.
How did I fix it:
Instead of using executing .js directly with something I make my ./bin/index.js binary file (basicly removed .js suffix)
Added #!/usr/bin/env node on top of index file
Goes to package.json and changed all dependency on ./bin/index.js to ./bin/index
Woala! it works :)
p.s. As I mentioned at start I believe there is an option to run this with .js as well but I wasn't able to find it. So please if anyone will find it let me know.
Thanks

Testing in node.js with mocha - Newbie

I have started implementing TDD in my JS project. I've implemented mocha for that purpose. As these are my first steps what I did:
Installed node.js
Installed mocha globally and locally to my project.
Wrote package.json setting dependencies.
Wrote makefile.
Wrote .gitignore to avoid uploading node_modules folder.
Folder structure
project
-- js
----filetotest.js
-- test
---- test.js
What I want to do is to run the command make test in order to run the tests inside test.js that tests the filetotest.js file.
I read about the node.js approach using exports. But is there some way to include the file in the test suite?
I'm stuck here, and I think that my doubt is more about the concept than the tech thing. Will appreciate a lot your help.
To clarify a little bit what I would like to do:
https://nicolas.perriault.net/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/
I would like to get a similar result through the command line.
Thanks so much,
Guillermo
You are doing it right.
Now export your function from filetotest.js, like this:
var f1 = function(params) {
// ...
}
exports.f1 = f1
In test.js, require this file
var f1 = require("./filetotest.js").f1
// test f1
Btw, if you will put your tests in /test directory, mocha will execute them automatically (given that it will be executed from the root of your project)

Categories