How does this pattern work: 'test/e2e/**/*.spec.js'? - javascript

I saw this pattern used in a configuration file for protractor.
specs: [
'test/e2e/**/*.spec.js'
]
To mean it means "all files inside test/e2e". What kind of pattern is this? I think it's not regex because of those unescaped slashes. Especially, why is there ** in the middle, not just test/e2e/*.spec.js?
I tried using the search engine, but did not find anything useful probably because the asterisks don't work very well in search engines.

What kind of pattern is this?
It is called "glob". The module glob is a popular implementation for Node, and appears to be the one used by Protractor.
Especially, why is there "**" in the middle, not just "test/e2e/*.spec.js"?
** means it can match sub-directories. It's like a wildcard for sub-directories.
For example, test/e2e/*.spec.js would match test/e2e/example.spec.js, but not test/e2e/subdir/example.spec.js. However test/e2e/**/*.spec.js matches both.

It is called "glob" syntax. Glob is a tool which allows files to be specified using a series of wildcards.
*.js means "everything in a folder with a js extension.
** means "descendant files/folders.
**/*.js means "descendant files with a js extension in descendant folders."
test/e2e/**/*.spec.js' means the above, starting in the test/e2e/ directory.
So, for example, given this file system:
test/e2e/foo/a.spec.js <-- matches
test/e2e/foo/butter.js <-- does not include "spec.js"
test/e2e/bar/something.spec.js <-- matches
test/other/something-different.spec.js <-- not in /test/e2e
The final pattern would match:
test/e2e/foo/a.spec.js
test/e2e/bar/something.spec.js

It's a globbing pattern. Most javascript things using globbing patterns seem to be based around the glob npm package. It's worth taking a look at the documentation as there are some handy hints in there for when you have more complex situations.
The path you are asking about will match any file ending .spec.js in any subdirectory under test/e2e

Related

What does /** means in js with respect to files, and not to comments

I have been stuck at a code and unable to find the meaning of /** common to package.json files field.
E.g. here
"files":[
"./*",
"./**",
"icon.*"
]
I am unable to know the meaning of any of the element of files in the above code taken from package.json
It's directories wildcards:
./* - Any file in current directory.
./** - Any file in current directory and sub directories.
icon.* - Any file with name icon.
Read more about package.json from NPM documentation
Those are known as globs, see https://www.npmjs.com/package/glob for instance:
* Matches 0 or more characters in a single path portion
** If a "globstar" is alone in a path portion, then it matches zero or more directories and subdirectories searching for matches. It does not crawl symlinked directories.
Not exactly sure who created them in the very first place though to be honest.
EDIT: actually, from Wiki:
The glob command, short for global, originates in the earliest versions of Bell Labs' Unix.

Match file within nested witin specific folder

I am trying to match viable package.json files to get all modules in a directory, including nested ones. I ran into an issue where someone has a two package.json files in their module. One is in the root and one is nested. I'd like to only match the regex with the first line and not the second. Do I have to parse the strings into arrays with path.sep or can I do this solely with regex?
/Users/thomas/Desktop/exmaple/node_modules/stream-http/package.json
/Users/thomas/Desktop/exmaple/node_modules/stream-http/test/browser/package.json
Pattern
^.+\/node_modules\/.+\/package.json$
https://regexr.com/45qvr
Using .+ greedily catches any characters, so it will always match as much as possible to find a package.json at the end. If you use [^\/]+ instead, it will only match characters that are not /, making sure that the package.json is only matched after exactly one directory under the node_modules/.
^.+\/node_modules\/[^\/]+\/package.json$

Jasmine - what connects a source to spec file?

Just getting started with Jasmine. I understand you put (like Rspec) the spec file in the javascripts folder with a naming convention of <file_name_to_be_tested>_spec.js
That's great!
So I have spec/javascripts/orders_new_spec.js
I've discovered that I can use the above to test EITHER of the files below:
app/assets/javascripts/orders/new.js
app/assets/javascripts/orders_new.js
But for someone like me who does have a somewhat complex file directory... I need a way to differentiate the two... Right now of course the jasmine.yml looks like this:
src_files:
- assets/orders/new.js
- assets/orders_new.js
But since there's ONE yml file for all specs, it doesn't serve as a differentiator.
Is there some way to, in the spec file itself do it? E.g.,
# /spec/javascripts/orders/new_spec.js
require 'assets/orders/new.js` # and ignore jasmine.yml
describe...
# /spec/javascripts/orders_new_spec.js
require 'assets/orders_new.js` # and ignore jasmine.yml
describe...
The formal answer is no. Of course there are ways of hacking around it. From the Jasmine Github mods:
Jasmine (especially in the browser) doesn't have any relationships between which spec file tests which implementation file. If you have a subset of files that can't be loaded in the same page as some other subset of files, you probably want to have multiple jasmine.yml files and use the JASMINE_CONFIG_PATH environment variable to pick the right one when running your tests. This would mean you need to run two (or more) jasmine:ci tasks to run all of your specs.
The other option would be to refactor/rework your implementations so that all of the files can be loaded into the browser at the same time and just instantiate the underlying objects in your specs.

Exclude everything but Javascript files in RequireJS Optimizer

I am using RequireJS optimizer in a gulp recipe to compile and concatenate my Modules but redundant 3rd party library files like bower.json and *.nuspec files are being copied to my output directory.
I have successfully managed to exclude full directories using fileExclusionRegExp in the requirejs.optimize options object with the following expression:
/^\.|^styles$|^templates$|^tests$|^webdriver$/
However, I cannot figure out how to exclude everything but .js file extensions. I could use the following:
/^\.|.json$|.nuspec$|^styles$|^templates$|^tests$|^webdriver$/
to exclude specific extensions but if a new type were to appear later, I would have to notice and then change the regex. Also, the regex would probably become unruly and hard to maintain with time. I have tried to use the following expressions:
/^\.|!js$|^styles$|^templates$|^tests$|^webdriver$/
/^\.|!.js$|^styles$|^templates$|^tests$|^webdriver$/
/^\.|^.js$|^styles$|^templates$|^tests$|^webdriver$/
/^\.|[^.js$]|^styles$|^templates$|^tests$|^webdriver$/
/^\.|[^.js]$|^styles$|^templates$|^tests$|^webdriver$/
The results ranged from doing nothing (the first 3, to breaking the build, last 2) any help anyone could provide would be appreciated.
Thanks
Try this regex:
^\.|\.(json|nuspec)$|^(styles|templates|tests|webdriver)$

Node path prefixes for modules

This should be a very simple question. I think it might be as simple as just being the convention, but I would like to check as I have no idea if there is anything else behind it or what phrases to even search regarding it.
var hello = require('./hello');
hello.world();
Imagine the above code. The require path is prefixed by a ./ this is always present for files in the same folder. Why not just the file name?
Comparitively the common use
var http = require('http');
Is not prefixed by a ./ I am currently assuming this is due to the http file being a "native" module. Therefore would I be right in saying that anything without ./ is looking in the native Node namespace and anything with a ./ is looking for a local file?
Also would a file in a higher directory like in PHP it would be ../
In Node would it be .././ or ./../
Yeah, this is a simple convention used in node. Please see the module.require docs
And for what it's worth, you won't always be using require("./hello"). Sometimes you'll be using require("../../foo") or require("../").
Simply put,
You use a path for requiring files within your module
You use a string identifier for including other modules

Categories