What is the command for running the test with mocha expressjs - node.js

My test file,
describe('a basic test',function(){
it('it should pass when everything is okay',function(){
console.log('hi')
})
})
My test result with the command 'mocha'
hi
․
1 passing (9ms)
But how can i get results like below,
a basic test
it should pass when everything is okay
hi
With tick mark as success,can any one help me.

The default reporter for Mocha is spec, which outputs the report like you want:
a basic test
hi
✓ it should pass when everything is okay
1 passing (6ms)
So the question is why Mocha isn't using the default reporter in your case (instead, it's using dot). The most likely reason for that is that you have a file called ./test/mocha.opts that contains this line:
--reporter dot
If you don't want dot as the default, just remove the line. If you want dot to be the default, but occasionally want to override it, pass another reporter on the command line:
mocha --reporter spec
# or shorter:
mocha -R spec

Related

Generate report for mocha with NodeJs

I am using mocha with NodeS but can't understand how i can generate report and show to the team what test cases i have created and which are not runnig also how many test cases should i write how to decide this ?
Mocha reporter documentation here
By default, the reporter is the console output. This output can be modified (as shown in the documentation).
I use mochawesome reporter to see results in a browser.
Quoting the documentation
Mochawesome is a great alternative to the default HTML reporter.
The usage is quite simple: npm i mochawesome and you can execute your test with the parameter --reporter mochawesome.
I use the following line into package.json.
"test": "cross-env NODE_ENV=testing mocha tests --timeout 4000 --reporter mochawesome --exit",
When you execute your test, results will be in a folder named mochawesome-report where will be an .html file.
And that's all.
Exists more third party reporters, even you can create your own reporter.

Separation of unit tests and integration tests

I'm interested in creating full mocked unit tests, as well as integration tests that check if some async operation has returned correctly. I'd like one command for the unit tests and one for the integration tests that way I can run them separately in my CI tools. What's the best way to do this? Tools like mocha, and jest only seem to focus on one way of doing things.
The only option I see is using mocha and having two folders in a directory.
Something like:
__unit__
__integration__
Then I'd need some way of telling mocha to run all the __unit__ tests in the src directory, and another to tell it to run all the __integration__ tests.
Thoughts?
Mocha supports directories, file globbing and test name grepping which can be used to create "groups" of tests.
Directories
test/unit/whatever_spec.js
test/int/whatever_spec.js
Then run tests against all js files in a directory with
mocha test/unit
mocha test/int
mocha test/unit test/int
File Prefix
test/unit_whatever_spec.js
test/int_whatever_spec.js
Then run mocha against specific files with
mocha test/unit_*_spec.js
mocha test/int_*_spec.js
mocha
Test Names
Create outer blocks in mocha that describe the test type and class/subject.
describe('Unit::Whatever', function(){})
describe('Integration::Whatever', function(){})
Then run the named blocks with mochas "grep" argument --grep/-g
mocha -g ^Unit::
mocha -g ^Integration::
mocha
It is still useful to keep the file or directory separation when using test names so you can easily differentiate the source file of a failing test.
package.json
Store each test command in your package.json scripts section so it's easy to run with something like yarn test:int or npm run test:int.
{
scripts: {
"test": "mocha test/unit test/int",
"test:unit": "mocha test/unit",
"test:int": "mocha test/int"
}
}
mocha does not support label or category. You understand correctly.
You must create two folders, unit and integration, and call mocha like this
mocha unit
mocha integration

Coverage report with istanbul and mocha

I'm a newbie on Node.js. I have to set up some tests in my application and I'm getting really mad trying to generate a back-end code coverage report with mocha and istanbul in my loopback application.
Searching through thousand of dab explained articles on Github I found some good articles and then I figured out that I had to use something like this:
istanbul cover _mocha -- [path/to/test/files] -R spec
I was happy because it says: "What you are essentially doing is passing the command to run your tests to Istanbul which, in turn, will run those tests on your behalf." However, every time I try to run Istanbul, I get this error:
No coverage information was collected, exit without writing coverage information
C:\...\proj-name\node_modules\.bin\_mocha:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
^^^^^^^
SyntaxError: missing ) after argument list
My working test file is:
var userService = require('../TestBusinessLogic.js');
var should = require('chai').should();
describe('API Utenti', function() {
it('should throw Exception on missing UserName', function() {
(function() {
userService({ Name: 'Pippo', Surname: 'Baudo' });
}).should.Throw(Error);
});
});
Is this command good to use? If not, could someone please explain me how to make a coverage report using istanbul with mocha?
Figured that I was running node_modules\.bin\_mocha instead of node_modules\mocha\bin\_mocha and this solved my problem.
When running istanbul from the command line you need to run it from the root of your project directory, it by default looks for the files to run the coverage reporting for at the root of your directory.
Additionally make sure your path to your test folder is relative to your project directory.
So you should navigate to your project directory using cd and then when inside your project directory then run
istanbul cover _mocha -- ./path-to/test.js -R spec

Sails.js testing with mocha only runs one test not two

I followed the sails.js testing example at http://sailsjs.org/#!/documentation/concepts/Testing . I have got it to run, but it only runs one test. The command line in package.json script.test is:
mocha test/bootstrap.test.js test/unit/**/*.test.js
"test/unit/**/*.test.js" should be catching 2 tests, UsersControllers.test.js and Users.test.js . It is only running Users.test.js . And yes, both tests are in the test/unit/ directory.
What am I doing wrong here ?
After copying the documented test cases from the sails js docs, make sure to change the following line in the User.test.js file
describe.only('UsersModel', function() {
to
describe('UsersModel', function() {
then you should see the controller test as well.

Mocha with Node: Only show logging for tests that fail

I'm using node with mocha and winston. Is there a way to set it up so it only shows logs for failing tests?
If you run with the min reporter you will only get full output on failed tests: mocha -R min or, if you prefer the verbose option, mocha --reporter min.
As of writing (in 2022), there's now an npm package that does exactly this:
https://www.npmjs.com/package/mocha-suppress-logs
I like it because I like the output from the default mocha reporter. It keeps all that, but hides console output for succeeding tests.
Possible to use
if (!expect(next.called).to.be.true) {
console.log("... further information")
}

Resources