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

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.

Related

Single Mocha tests output

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?

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)

How to stop the whole test execution but with PASS status in RobotFramework?

Is there any way I can stop the whole robot test execution with PASS status?
For some specific reasons, I need to stop the whole test but still get a GREEN report.
Currently I am using FATAL ERROR which will raise a assertion error and return FAIL to report.
I was trying to create a user keyword to do this, but I am not really familiar with the robot error handling process, could anyone help?
There's an attribute ROBOT_EXIT_ON_FAILURE in BuiltIn.py, and I am thinking about to create another attribute like ROBOT_EXIT_ON_SUCCESS, but have no idea how to.
Environment: robotframework==3.0.2 with Python 3.6.5
There is nothing built-in to support this. By design, a fatal error will cause all remaining tests and suites to have a FAIL status.
Just about your only choice is to write a keyword that sets a global variable, and then have every test include a setup that uses pass execution if to skip the test if the flag is set.
If I understood you correctly, you need to pass the test execution forcefully and return green status for that test, is that right? You have a built in keyword "Pass Execution" for that. Did you try using that?

Jest snapshot is redundant

I am writing snapshot tests using Jest for a node.js and React app and have installed snapshot-tools extension in VS code.
Some of my tests are displaying this warning in the editor:
[snapshot-tools] The snapshot is redunant
(Presumably it is supposed to say redundant)
What does this warning mean? I am wondering how I can fix it.
I was having the same problem, so I took a look at the "snapshot-tools" code. It marks a snapshot section as redundant, if it doesn't see a corresponding test in the test file that has a matching name and that calls "expect().toMatchSnapshot()" or something similar.
The problem is (as it says on the "Limitations" section of the plugin's marketplace page), it does a static analysis of the test file to find those tests that use snapshots. And the static analysis cannot detect tests that have dynamically generated names, or that don't directly call "expect().toMatchSnapshot()" in the test's body.
For example, I was getting false positive "redundant" warnings, because I had some tests that were doing "expect().toMatchSnapshot()" in their "afterEach()" function, rather than directly in the test body.
This could indicate that the snapshot is no longer linked to a valid test - have you changed your describe/it strings without updating the snapshots? Try running the tests with -- -u appended (eg: npm test -- -u). If that doesn't work, have a look at your snapshots file and compare the titles to your test descriptions.

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