Karma - Instanbul - console.log being counted as test? - karma-runner

I noticed that Karma/Istanbul is marking console.log() as covered testcase?
Is there anyway we can make Istanbul to skip console.log()?
I could just remove or rename the console.log, but I am interested if there are such options in Istanbul?
Here is an example:

If you want to ignore some statements from coverage report use special formed comments:
https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md

Related

How to run a snippet on breakpoint in developer tool

I'm gonna ask a weird question:
Is there a way to run a code snippet on a breakpoint set on the DOM?
Use example:
- Visit a website with a table.
- Set a Break On Subtree Modification
- Run MySnippet when the breakpoint hits.
What you can do though is set a conditional breakpoint with an eval('...') statement.
for example the following with log to the console:
test 11:59:51:326
Good for checking timing issues...
eval('let current=new Date();console.log("test",current.getHours()+":"
+current.getMinutes()+":"+current.getSeconds()+":"
+current.getMilliseconds());')
you can also print a local object, e.g if localVar is defined:
eval('let current=new
Date();console.log("test”,localVar,current.getHours()+":"
+current.getMinutes()+":"+current.getSeconds()+":"+current.getMilliseconds());')
DevTools doesn't have this feature. After your DOM breakpoint triggers you can run a Snippet though. You can run Snippets from the Command Menu.

Ignore or bypass errors phpcs

How to bypass or ignore specific errors/warnings in vscode?, I am using phpcs.
What you are looking for is to ignore the warning and/or errors, that are notified by the phpcs in the console of the vscode.
For Warnings
Use the following config in your settings.json
"phpcs.showWarnings": false,
this will remove all the warnings displayed in the output console.
For Errors
You should go trough the DOCS for complete details but to remove the errors related to the Doc block and the formatting standards you can set the
"phpcs.errorSeverity": 6,
Although it is mostly used for testing or code reviews to check for total warnings and errors by setting different values for both, but for development i dont do that and keep it to the default value that is 5 but you can get rid of those errors above in your image.
The vscode-phpcs refers to the GitHub project squizlabs/PHP_CodeSniffer, which integrates PHP_CodeSniffer into VSCode.
Its readme mentions the setting phpcs.ignorePatterns:
An array of glob patterns to skip files and folders that match when linting your documents.
{
"phpcs.ignorePatterns": [
"*/ignored-file.php",
"*/ignored-dir/*"
]
}
That refers to PHP CodeSniffer --ignore option.
That is not what you want exactly, since it ignores all errors on a given set of file.
But you could use PHP CodeSniffer syntax to ignore errors:
Ignoring Parts of a File
Some parts of your code may be unable to conform to your coding standard. For example, you might have to break your standard to integrate with an external library or web service.
To stop PHP_CodeSniffer generating errors for this code, you can wrap it in special comments. PHP_CodeSniffer will then hide all errors and warnings that are generated for these lines of code.
$xmlPackage = new XMLPackage;
// phpcs:disable
$xmlPackage['error_code'] = get_default_error_code_value();
$xmlPackage->send();
// phpcs:enable
Again, not exactly what you want, since you have to specify that on a file-by-file basis
You can disable multiple error message codes, sniff, categories, or standards by using a comma separated list.
You can also selectively re-enable just the ones you want.
The following example disables the entire PEAR coding standard, and all the Squiz array sniffs, before selectively re-enabling a specific sniff. It then re-enables all checking rules at the end.
// phpcs:disable PEAR,Squiz.Arrays
$foo = [1,2,3];
bar($foo,true);
// phpcs:enable PEAR.Functions.FunctionCallSignature
bar($foo,false);
// phpcs:enable

Wrong coverage report in karma

I've configured my karma.conf.js with enabled preprocessing to get a report about the code coverage of my tests. I've added this line to the preprocessors section.
preprocessors: {
'public/js/app.js': ['coverage'],
'public/js/filters.js': ['coverage'],
'public/js/directives.js': ['coverage'],
'public/js/services/*.js': ['coverage'],
'public/js/controllers/*.js': ['coverage'],
},
What I'm get is a report that is totally wrong. I know that I've written tests for each modules and the function within. But the coverage report shows me only the tests for the services correctly.
For instance the tests for directives. I know that I've written some tests and the tests will also be executed. But the report shows me that I've just tests for 36% of my code lines.
What could be the reason for this strange behavior?
Update:
I see this output from the spec reporter:
Directives:
bsTooltip:
when the element was created:
PASSED - should call the popup function
bsSwitchtext:
when the model isBusy changes to true:
PASSED - should call the button method with loading
when the model isBusy changes to false changes:
PASSED - should call the button method with loading
So I think that my test will all be executed.
Looks like there's issue with Typescript & Jasmine which is used by Angular. Enabling source map for test build appears to fix this issue.
I enabled source map in Angular 6.1 as follows,
Go to angular.json and in main project, find test, and add sourceMap:true to enable source map for the test run.
to enable that in CLI, run with command --source-map or --sm=true
Github issue links
Code coverage report issue with branch coverage (if path not taken)
ng test --code-coverage in 6.1 improperly detecting branches
I'm obligated to write this answer as I had the same issue, and this was the first question on Google search.
Try adding a console.log('testing123'); at one of the points which shows as not covered. If it shows up when you run the tests you know that something is going wrong with Istanbul.
However my guess would be that either there is something wrong with your configuration, and those tests are not running at al, or the tests are not executing the code as you think they are.
Try changing what you have in preprocessors to:
preprocessors: {
'**/public/js/**/*.js': ['coverage']
},
I was not able to the report to work unless I followed the specific syntax with **/ before directors in the preprocessors object.
The karma-coverage documentation includes the preceding **/ before directories listed in preprocessors.
Based on this SO answer.

phpThumb is not setting parameter – fltr[] usm

I am using Brett's Mr. PHP thumb caching script along with phpThumb to create my thumbs. It works extremely well, except for one thing... I cannot get it to set and process the UnSharpMask filter. The relevant part of the script looks like this:
// generate the thumbnail
require('../phpthumb/phpthumb.class.php');
$phpThumb = new phpThumb();
$phpThumb->setSourceFilename($image);
$phpThumb->setParameter('w',$width);
$phpThumb->setParameter('h',$height);
$phpThumb->setParameter('q','95'); // set the quality to 95%
$phpThumb->setParameter('fltr[]','usm|80|0.5|3'); // <--- THIS SHOULD SET THE USM FILTER
$phpThumb->setParameter('f',substr($thumb,-3,3)); // set the output file format
if (!$phpThumb->GenerateThumbnail()) {
error('cannot generate thumbnail');
}
I'm guessing there's a problem with my syntax, since the fltr[] parameter requires brackets. I have tried escaping the brackets like so: 'fltr[]' but that didn't work either.
I've used several other possible parameters with good success (zoom cropping, max height, max width, etc...) Everything seems to work except the filters (including usm - UnSharpMask).
I don't get any errors. It spits out thumbs all day long. They're just not sharpened.
For reference, here's the official phpThumb readme file that discusses all the possible settings and filters.
Thanks for looking.
Well after much trial, error, and aggravation, I finally discovered the solution buried in one of the demo files that come with phpThumb.
It's a matter of removing the brackets all together. Basically changing this line:
$phpThumb->setParameter('fltr[]','usm|80|0.5|3');
to this:
$phpThumb->setParameter('fltr','usm|80|0.5|3');
After that, it's only a matter of tweaking the settings to get the desired amount of sharpness.

When running multiple tags with NUnit Console Runner and SpecFlow I get incorrect results

This is a follow up to my earlier questions on setting up tags: Can I use tags in SpecFlow to determine the right environment to use? and setting up variables from those tags: How to set up a URL variable to be used in NUnit/SpecFlow framework
I've set up some variables to aid in populating my NUnit Tests, but I find that when the NUnit runner finds the test that fits the first tag the test runs it with the settings of the second tag. Since the tags are important to me to not only know what test to run, but what variables to use, this is causing me problems.
So if I have the following tags:
#first
#first #second
#second
If I run #second everything is fine. If I run #first I get any scenario that has only #first fine, but when it comes to scenarios where I have both #first #second the scenario is run, because #first is there, however, it uses the parameters for #second. Since I am running the DLL through the NUnit-Console and the Tests are written through SpecFlow I am not sure where the issue may lie.
Does anyone have advice on setting up tests to run like this?
You've not been very specific, but it sounds like you have a feature file like this:
#first
Scenario: A - Something Specific happens under the first settings
Given ...etc...
#second
Scenario: B - Something Specific happens under the second settings
Given ...etc...
#first #second
Scenario: C - Something general happens under the first and second settings
Given ...etc...
It looks like you are selecting tests to run in NUnit by running all the tests in the "first" category.
If you set up event definitions like this:
[BeforeFeature("first")]
public static string FirstSettings()
{ ... }
[BeforeFeature("second")]
public static string SecondSettings()
{ ... }
If you execute scenario C then FirstSettings() and SecondSettings() will be executed before it. This is regardless of whether you used the #second category to select the test to run under NUnit.
This is almost certainly the reason that you are seeing the second settings applied to your test with both tags - I expect the second settings overwrite the first ones, right?
My only advice for setting up tests like this, is that binding events and so on to specific tags can be useful but should be used as little as possible. Instead make your individual step definitions reusable, and set up your test environment, where possible, with Given steps.