Run a single test file using Node js Mocha - node.js

All my tests are under test folder and when I give npm test all the tests are getting executed. Now I am trying to run my test scripts by file or by it's describe. I have the following in my package.json
"scripts": {
"test": "set NODE_ENV=test&& nyc --reporter=html --reporter=text mocha 'test/**/*.js' --exit --timeout 7000"}
When I give npm test --grep 'filename / describe / it' it is not picking up the given input instead I'm getting the following warning Warning: Cannot find any files matching pattern "given filename / describe /it" even though given input matches.
My filename pattern is filename.test.js and the folder structure is like test\***\filename.test.js
Can anyone guide me on what I'm missing on this.

You need to include the grep option within the npm command:
"scripts": {
"test": "set NODE_ENV=test&& nyc --reporter=html --reporter=text mocha 'test/**/*.js' --exit --timeout 7000 --grep"
}
Then call it with:
npm run test <name of test>

Related

Unit test count is not shown on the sonarqube dashboard

I am using nodejs Project repository and the test cases are written in the Jasmine framework. On sonarqube dashboard, I cant see the unit test count block.
Sonar-template-configurations was written as below:
sonar.projectKey={{sonar_projectKey}}
sonar.projectName={{sonar_projectName}}
sonar.host.url={{{sonar_host_url}}}
sonar.javascript.environments = node
# Source code for analysis
sonar.sources=.
sonar.tests = .
sonar.test.inclusions = **/*.spec.js
# Exclusions
sonar.exclusions=test/**
# Reports
sonar.ts.tslint.configPath=tslint.json
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.javascript.coverage.lcovReportPath=coverage/lcov.info
sonar.junit.reportsPath= unit_test_results.xml
sonar.testExecutionReportPaths=unit_test_results.xml
sonar.log.level=DEBUG
sonar.verbose=true
Script used in package.json as below for generating report and running test cases:
"scripts": {
"start": "nodemon --verbose --ignore '/meta.conf/**' --ignore '/data/**' app.js",
"test": "nyc ./node_modules/jasmine-xml-reporter/bin/jasmine.js --junitreport --filePrefix=unit_test_results",
"coverage": "nyc --reporter=lcov --reporter=text-lcov npm test"
},
Does anyone know the solution for it, to show the Unit tests block on the sonarqube dashboard?
Thanks.

Yarn "posttest" script does not work if "test" script fails

Well, I have these scripts in my package.json for testing some code in NodeJS.
"scripts": {
"pretest": "env NODE_ENV=test sequelize db:migrate",
"test": "jest",
"posttest": "env NODE_ENV=test sequelize db:migrate:undo:all"
}
When the tests go clear, the "posttest" runs, but when the tests fail, I receive a
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
in VS Code. There is nothing usefull on the link about the problem, neither on the internet.
So I found this link about NPM:
https://github.com/npm/npm/issues/5493
The guy said:
In the vast majority of cases, users are going to be unpleasantly
surprised if posttest runs after test failures (you don't want your
test environment being cleaned up or new versions being published if
there were test failures, for instance). As such, this behavior isn't
going to change. Putting something like "test":"npm run-script
test-failing || npm run-script mandatory-cleanup" into your
package.json will give you what you want.
This did not solve my problem. With more research I found this:
npm posttest doesn't trigger if npm test fails
The solutions did not work for me either.
So how can I run the "posttest" script even if the tests fail?
Well, with the conversation above, I got to this solution:
This is my scripts in package.json:
"scripts": {
"dev": "nodemon src/server.js",
"pretest": "env NODE_ENV=test sequelize db:migrate",
"run-tests": "jest",
"run-after-tests": "env NODE_ENV=test sequelize db:migrate:undo:all",
"test": "npm-run-all run-tests run-after-tests --continue-on-error"
},
I installed the npm-run-all package and it runs the run-after-test script even if the tests fail. Now I get the errors
error Command failed with exit code 1.
from the test script, and
ERROR: "test" exited with 1.
error Command failed with exit code 1.
from run-after-test, but in the end my problem got solved. If someone has a better solution with no errors at the end of the scripts, please share with us.

How to use mochawesome and mocha-sonarqube-report in a single command?

I have the following script in my package.json file to check the code coverage of the UT:
"coverage": "nyc --reporter=lcov --reporter=cobertura mocha ./test/apis_new/* --reporter mocha-sonarqube-reporter --reporter-options output=xunit.xml mocha ./test/apis_new/* --reporter mochawesome --exit"
When I run the above command, only mochawesome-report folder is getting generated but there is no such file as xunit.xml.
If I run the bellow command where I have changed the order of mochawesome and sonarqube, xunit.xml is geting generated but not mochawesome-report folder:
"coverage": "nyc --reporter=lcov --reporter=cobertura mocha ./test/apis_new/* --reporter mochawesome mocha ./test/apis_new/* --reporter mocha-sonarqube-reporter --reporter-options output=xunit.xml --exit"
What am I doing wrong?
Is this the right way to use both together in a single script?
Natively, mocha supports only one reporter, so all behaves as expected only one of reporters (possibly first) is used.
Check out mocha-multi-reporters. I use it to generate xunit and spec output.

How to run test with local.example.js ENV

I have 2 env:
dev
local.example.js
enter image description here
i run test with this command "mocha specs/* --opts ./mocha.opts" and default run dev ENV.
What command I can use for a run with local.example.js ENV ?
Like "mocha specs/* --opts ./mocha.opts --env local.example.js"
If you are using mocha version 6+, you might consider defining configuration in a *.js file instead of legacy *.opts file.
Then you will be able to run mocha tests with
$ mocha specs/* --config path/to/config.js
More information here: https://mochajs.org/#-config-path

How to get combined coverage on both unit test and integration test using NYC

I want to get a combined coverage of both unit-test and integration-test.
I have seperate folders for both unit-test and integration-test. In my pipeline ci i have a seperate stage for unit-test and integration-test becasue the integration-test requires a dependency.
package.json
{
"unit-test": "nyc mocha --timeout 5000000 -r ts-node/register --project tsconfig.json test/unit/*.test.ts --exit",
"integration-test": "nyc mocha --timeout 5000000 -r ts-node/register --project tsconfig.json test/integration/*.test.ts --exit",
}
Right now i get coverage report for both of them seperatly, but what i can see is that its not a true reflection of all the coverage.
Is there someway i can combine the coverage summary. It might mean that i would need to combine the 2 stages in my pipeline?
There is a nyc merge feature, that is able to merge multiple coverage reports that are produced by different test runs. You can read about it in the nyc docs.
Other way is to run both tests in same command (as mocha is able to handle multiple input paths), like following:
"test": "nyc mocha --timeout 5000000 -r ts-node/register --project tsconfig.json 'test/unit/*.test.ts' 'test/integration/*.test.ts' --exit"

Resources