Single Mocha tests output - node.js

I'm looking for a way to provide a single boolean output from my mocha tests instead of the standard test list. If all tests passed - it should return true, otherwise false. It's fine if it's written in a separate file on a server, we don't even need to suppress the logs, but I couldn't find a way to use the after() function to get a list of all tests that had been run and their results.
Any ideas?

Related

Explain the interaction between Jest, --verbose, and console.log

I am trying to figure out how to use Jest properly. The interaction of Jest and console.log is very poorly documented (and apparently very inconsistent according to lots of tickets).
The Jest documentation is terrible.
When is console.log output supposed to show up? Always? Sometimes?
Seems to me the desired behavior would be for Jest to show console.log output for the tests that failed. I don't see anything about that in the documentation. What I do see is that console.log output seems to correlate somehow being either before or after tests.
Can you explain?
By default - Jest will print all console log results on the terminal which the tests are executed from. Instances when users do not see their logs are usually caused by the silent property being set to true in their config file or the --silent flag being added to their cli execution, which is not default Jest behaviour.
The ---verbose flag causes the results of all individual it unit tests to be printed in the terminal (whereas by default the terminal would only print the names of the test suites that have passed and only unique unit tests (it) that have failed). Additionally each individual test will also display the length of time they took to execute if they they take longer than 1ms. Another interesting fact is that --verbose will also print logs of console.log executions that are run during the evaluation of a module (when a module is imported - e.g. a console statement that simply exists in the body of a javascript file or an implicitly invoked function) even though it is not used (e.g. the tests that uses it are not executed due to the xdescribe keyword in their test suite).
The ability to print console log results exclusively for failed tests is not present in Jest.

Using AfterEach with different file module in NodeJS getting different values

I'm quite new in using Node JS, and I have been working on a test script that take screenshots whenever a test fails. And I'm trying to do this without the use of Jasmine reporter. I tried to use this approach instead Check if test failed in 'afterEach' of Jest without jasmine, however, I'm working with different files I have a file fail_test.spec.js that is used as my main file, and a test_fail1.js as another testscript file. Here is what happening, my test on fail_test.spec.js works fine with the use of AfterEach, just like in the link, it gives me "true" value if the test passed and "false" value when the test fails and it performs screenshot. The problem is the test_fail1.js is also being check by the AfterEach and it constantly gives of a "false" value even if the test passed. I do intend to use AfterEach with the test_fail1.js and on other tests in the future. So my questions are:
Why does the test_fail1.js only gives of constant "false" value?
Is there any work around with this? Because I just only need to know the status of the test in every testscripts within or with other files (ex.fail_test1.js, fail_test2.js, and so on)

Fail code coverage if no tests exist for code

I have a simple Node JS application and am using Istanbul with Mocha to generate code coverage reports. This is working fine.
If I write a new function, but do not create any tests for it (or even create a test file) is it possible to check for this?
My ultimate goal is for any code which has no tests at all to be picked up by our continuous integration process and for it to fail that build.
Is this possible?
One way you could achieve this is by using code coverage.
"check-coverage": "istanbul check-coverage --root coverage --lines 98 --functions 98 --statements 98 --branches 98"
Just add this in your package.json file, change the threshold if needed. If code is written but no test then the coverage will go down.
I'm not sure if this is the correct way to solve the problem but by running the cover command first and adding the parameter --include-all-sources this then reported on any code without a test file and added them to the coverage.json file it generated.
Then running the check-coverage would fail which is what I'm after. In my CI process I would run cover first, then check-coverage
Personally I find the documentation on Istanbul a little bit confusing/un-clear which is why I didn't see this at first!

Searching all Business components by functions they use, in UFT

Is there an easy way to see all the Business components that are using a specific function in UFT or ALM?
There is a function which was scripted in my library and now i want to delete that function from my library.
If i delete the function from my script without removing it from all the business components that use it, my test could fail.
I'd add some code to the function like so:
Dim oUft : Set oUft = CreateObject("QuickTest.Application")
Reporter.ReportEvent micWarning, _
"<YourFunctionName>", _
oUft.Test.Name & " uses function <YourFunctionName>"
That means that every time this function is called, it will output the test name to the test results. Run with this for a week, or however long your test cycle is to make sure you have executed a full suite of tests, then scan the reports to identify all the tests using it.
All of my UFT scripts write their own text log files as I don't use the Test Results option - if you do similarly, then you could simply grep the log files for the message.

Bail suite on mocha (sub)suite error but continue next?

I'm using mocha in node.js with have bdd-style specs.
Is it possible to bail a sub-suite after the first error but continue it's parent/sibling suites?
Say I test different routes to access an api, then I want to abort a specific route if it's connection fails because there's no use hammering with calls if first action failed, but it can still attempt to check other things.
If a high level tests sees a server is completely down or misconfigured then I could abort instead of having to wait all the failing tests to timeout and fill the report with unnecessary mayhem.
I see the following answer but that's not what I want, it bails everything, which is too much. I want something to only bail a branch in the spec tree if an assertion fails.
Skip subsequent Mocha tests from spec if one fails
If you want mocha to continue processing other test files after failing on one, you could use find to run a separate instance of mocha on each file:
find test/ -name "*.js" -exec mocha {} \;
It sounds like mocha-steps may work for this:
Global step() function, as a drop-in replacement for it(). Any failing step will abort the parent describe immediately. This is handy for BDD-like scenarios, or smoke tests that need to run through specific steps.

Resources