I am trying to run some Mocha tests within my Node.js app.
Here is my folder structure:
compute/
folder1/
app/
tests/
mytest.js
folder2/
app/
tests/
mytest2.js
I got a package.json in both with mocha installed.
When I try to start a test with yarn test, I got an error
Warning: Could not find any test files matching pattern: test
No test files found
How can I manage to run it?
Because my folders' names are "tests" and not "test" as in the default and they are not at source.
When you don't use the default folders, you need to specify them as arguments. An example follows:
mocha "folder{1,2}/**/tests/*.js"
If you want to run with only npm test/yarn test, then you need to update your package.json file like following:
"scripts": {
"test": "mocha \"folder{1,2}/**/tests/*.js\"",
},
Related
I am developing an extension for vscode using typescript. I set up a GitHub Action which is executing my test cases. To gather the test coverage I am using nyc.
These are my node scripts:
"compile": "tsc -p ./",
"test": "npm run compile && node ./out/test/runTest.js",
"coverage": "nyc npm run test"
When I run npm run coverage I see all my test cases are getting executed (they are not stored in the file runTest.js), but only the coverage for the test file runTest.js is gathered. The relevant classes lib.ts and extension.ts are not shown in the coverage.
Why is this so? What am I doing wrong?
See my package.json and .nycrc.json for configuration.
I could fix this problem by sticking to the great blogpost which I found posted on a similar Question.
The problem is, that NYC is a command line tool. When executing the test I focused on my configuration in the .nycrc file. Fact is, that the visual studio code test runner is not using this configuration and has to be configured inside the test runner.
I fixed the broken test coverage generation inside this commit.
I want to run lint check all the file *.js without node_modules
Here my command: "lint": "eslint **/*.js",, My folder structure look like: app > module_name > module.js but in the app folder still having some files common like helpers.js server.js and I also want to check them.
In .eslint config I added "ignorePatterns": "node_modules" and tried added a file .eslintignore and add node_modules but it's not working.
When I run npm run lint it always throw error:
Oops! Something went wrong! :(
ESLint: 7.32.0
You are linting "node_modules/bignumber.js", but all of the files matching the glob pattern "node_modules/bignumber.js" are ignored.
If you don't want to lint these files, remove the pattern "node_modules/bignumber.js" from the list of arguments passed to ESLint.
If you do want to lint these files, try the following solutions:
* Check your .eslintignore file, or the eslintIgnore property in package.json, to ensure that the files are not configured to be ignored.
* Explicitly list the files from this glob that you'd like to lint on the command-line, rather than providing a glob as an argument.
You could add node_modules/ in your .eslintignore file, and update the package.json file with script: "lint": "eslint --ext .js app/", before running npm run lint.
You could add node_modules/ in your .eslintignore file, and update the package.json file with script:
if u r using ubuntu/linux
"lint": "eslint '**/*.js'"
if project structure like these folder/file.js
if u r using windows
"lint": "eslint **/*.js"
if project structure like these folder/file.js
This is my folder structure
src/
a.js
b.js
test/
a.spec.js
b.spec.js
and I've tried running
~/.../src $ mocha
~/.../src $ mocha test
~/.../src $ mocha test/
~/.../src $ mocha ./test/
~/.../src $ mocha ./test/*.js
but non worked... all I get is just one test file running and the rest are ignored.
The docs say that (1) should do the job but well it doesn't.
Well... I'm an idiot. I had it.only in one of my files and I forgot it. Removing the .only was the answer. All the test files are running now.
Sorry for that, shame on me...
It's built like this so you can run your test suite on just a file or two if you're doing development on them. If you want to run mocha throughout your project just specify --recursive
You can see some documentation here.
Better yet, you can just specify it in your package.json for CD. First do npm install mocha --save-dev and then put this in your package.json file then run npm test
{
"scripts": {
"test": "./node_modules/.bin/mocha --recursive",
}
}
Create a moch.opts file under test directory and add recursive option
--recursive
Run ~/.../src $ mocha command; it should work.
I've decided to rewrite my generator from ES5 syntax to ES6. But I faced the issue with transpiling generator on pre-publish.
Issue: As we know Yeoman generators have templates folder where different files is located. When Babel is traspiling all the generators sources via babel src --out-dir generators it skips the templates files or breaks the traspiling with error.
My Attepmts: I was trying to make something like babel src --out-dir generators && cp -rn src/ generators/ but I don't like this solution.
Question: How can I make old structure generators/sub-generators but in ES6 syntax, not in ES5.
Thanks.
UPD: I'm hoping that Yeoman has something like Mocha has --require babelhook.js.
I've found the solution for this case.
First of all, I've created src folder where generator sources in ES6 syntax is located. Structure of this folder is the same as in usual ES5 generator.
When I want to compile these sources I just need to copy them and compile in generators folders.
So I've written the following scripts in package.json;
"scripts": {
"clean": "rm -rf ./generators",
"compile": "npm run clean && cp -r src/ generators/ && babel src --out-dir generators",
"prepublish": "npm run compile",
"test": "istanbul cover _mocha"
}
And last thing that I've done is add ignore field in .babelrc file. So I'm sure that templates will be just copied but not traspiled.
{
"stage": 0,
"ignore": [
"app/templates",
]
}
I'm using the scripts section inside the package.json file to store some commands I have to run regularly.
"scripts": {
"test": "./test/phantomjs ./test/js/run_jasmine_test.coffee ./test/index.html",
"rjs": "r.js -o ./js/app.build.js",
"less": "lessc -x ./css/app.less > ./css/app.css"
}
in every command I have got a ./ at the beginning of the path - this is why I can only call npm run-script rjs from the project's root directory.
is there a way to reference the project's root directory inside the package.json so that I can run e.g. npm test from anywhere in my project?
I think it depends on the way you organize your project.
I created a sample project just to demonstrate. This is my sample project directory tree:
.
├── dir1
│ └── dir2
├── package.json
├── src
│ └── index.js
└── test
└── test.js
NOTE: ./dir1/dir2 are just two empty directories for demonstration.
My package.json file looks like this:
{
"name": "sample",
"version": "1.0.0",
"description": "sample",
"main": "index.js",
"scripts": {
"test": "node test/test.js",
"start": "node src/index.js"
},
"author": "yaniv",
"license": "ISC"
}
Focus on the scripts section, those are two simple line, without considerations about my location.
If I change directory to /dir1/dir2 and execute npm test and the script I wrote on test property is executed properly without the use of ./ (which defines the current directory...)
Bottom line - I suppose if you re-organize your project and remove ./ will do the job in your case.
INIT_CWD
We can reference the root directory of the project throught the environment variable INIT_CWD that npm set for us!
From the doc :
Scripts are run from the root of the module, regardless of what your current working directory is when you call npm run. If you want your script to use different behavior based on what subdirectory you’re in, you can use the INIT_CWD environment variable, which holds the full path you were in when you ran npm run.
Example:
"tas:adminApp:build": "cd src/KidoService/AdminApp && npm run build && cd $INIT_CWD && gulp build_copyAdminApp",
And just because i love to think too much!
What if we didn't have such a variable set for us!
We can execute an env variable set command at all start! export ROOT_DIR=$PDW !
And then when we need it ! We can use it!
The above example will become
"tas:adminApp:build": "export ROOT_DIR=$PDW && cd src/KidoService/AdminApp && npm run build && cd $ROOT_DIR && gulp build_copyAdminApp",
You can see it's a nice technique ! We can use that to reference some directories that we want to go back to!
For windows we use set VAR=VALUE
considered creating a shell file that runs?
for example create a .sh file make it executable (chmod 777 file.sh)
and then cd to your nodejs project root, run the npm command, and then cd back into the directory you have just left?
adding the script to your environment will make it executable from anywhere...
alternatively will hard coding the full path be ok? (so rather than using ./ - put /home/user/username/yourproject/phantomjs)