In Jest, is there any way to ignore code for test coverage?
I tried using
/* istanbul ignore next */
But it doesn't seem to work.
It works.
(function(global) {
var defineAsGlobal = true;
/* istanbul ignore next */
if(typeof exports === 'object') {
module.exports = lib;
defineAsGlobal = false;
}
/* istanbul ignore next */
if(typeof modules === 'object' && typeof modules.define === 'function') {
modules.define('lib', function(provide) {
provide(lib);
});
defineAsGlobal = false;
}
/* istanbul ignore next */
if(typeof define === 'function') {
define(function(require, exports, module) {
module.exports = lib;
});
defineAsGlobal = false;
}
/* istanbul ignore next */
defineAsGlobal && (global.lib = lib);
})(this);
Sample project https://github.com/ilyar/sandbox/tree/master/jest
Update for anyone that finds this at a later date.
/* istanbul ignore next */
Will work but as read from The Jest Official Documentation:
coveragePathIgnorePatterns seems to not have any effect.
Make sure you are not using the babel-plugin-istanbul plugin. Jest
wraps Istanbul, and therefore also tells Istanbul what files to
instrument with coverage collection. When using babel-plugin-istanbul,
every file that is processed by Babel will have coverage collection
code, hence it is not being ignored by coveragePathIgnorePatterns.
The documentation can be found here: Documentation
So in order to fix this issue uninstall babel-plugin-istanbul:
If it is a library based only on javascript, than you can just run npm uninstall --save babel-plugin-istanbul or npm uninstall --save-dev babel-plugin-istanbul
If you've installed a library with native content that requires linking, and you've linked it with rnpm then you can do: rnpm unlink package_name then follow step 1 - Aakash Sigdel
This quote was from Aakash Sigdel found here: quote
Found a workaround (spaces before and after the comment seem to be necessary):
class Foo {
bar /* istanbul ignore next */ () {
return 'biu~';
}
}
Just for anyone finding this who uses the v8 provider:
the docs now state how to ignore lines with different providers, and link to more details. But basically either /* c8 ignore next */ or /* c8 ignore start */ + /*c8 ignore end */ should work well with the v8 provider.
Example:
/* c8 ignore next */
if (process.env.DEBUG) console.log('debug');
/* c8 ignore start */
switch (process.env.DEBUG) {
case '1':
console.log('some verbosity');
break;
case '2':
console.log('a lot of verbosity');
break;
}
/* c8 ignore end */
In my case, I had coverageProvider: 'v8', and it was causing this to break. doing: coverageProvider: 'babel', fixed it and the pragma works great.
According to a babel issue thread Istambul appears to have a bug where it assumes the preceding line of code is terminated with a semi-colon...
constructor(message: string) {
// TODO: how do I get Jest code coverage for "super(message)?
// /* istanbul ignore next */ assumes the preceding line of code is terminated with a ;
DEBUG: console.log();
/* istanbul ignore next */
super(message);
}
Related
While using PMD code analyser,
I have shown several duplicates which includes framework classes and delegates and datasource methods too, I just want to suppress those findings. I tried with Suppress warnings not works. Also I can't find syntax for Objective-c. Can any one give me how to suppress duplicate findings in PMD?
I tried the below command for excluding the directory,
./run.sh cpd --files
/Users/Arun/Documents/Projects/Sample
--language objectivec --minimum-tokens 100 --format xml --exclude /Users/Arun/Documents/Projects/Sample/ExternalFrameworks.
I don't see any duplicate suppression syntax for objective-c.
There is currently no support to have CPD ignore chunks of code in any other language than Java. The only option available is to fully ignore files using the --exclude flag.
Supporting it through comments (// CPD-[OFF|ON] | /* CPD-[OFF|ON] */) should be relatively easy since Objective-C is implemented using JavaCC.
The source code is tokenized here. CPD suppression consists merely of dropping the ignored tokens from tokenEntries.
To do so, you first need to find comments, this is done by checking if currentToken.specialToken is not null.
Token st = currentToken.specialToken;
while (st != null) {
if (st.image.contains("CPD-OFF") {
suppressing = true;
break;
}
if (st.image.contains("CPD-ON") {
suppressing = false;
break;
}
st = st.specialToken;
}
and then checking if the value of suppressing before adding (or not) the token here
Final code should look something such as:
boolean suppressing = false;
while (currentToken.image.length() > 0) {
if (!suppressing) {
tokenEntries.add(new TokenEntry(currentToken.image, sourceCode.getFileName(), currentToken.beginLine));
}
currentToken = (Token) tokenManager.getNextToken();
Token st = currentToken.specialToken;
while (st != null) {
if (st.image.contains("CPD-OFF") {
suppressing = true;
break;
}
if (st.image.contains("CPD-ON") {
suppressing = false;
break;
}
st = st.specialToken;
}
}
PRs are always welcomed. This along with a couple unit tests should be enough to get this merged for the next release.
All,
Here is a unit test for checking the size of a collection
main() {
test("Resource Manager Image Load", () {
ResourceManager rm = new ResourceManager();
int WRONG_SIZE = 1000000;
rm.loadImageManifest("data/rm/test_images.yaml").then((_){
print("Length="+ rm.images.length.toString()); // PRINTS '6' - WHICH IS CORRECT
expect(rm.images, hasLength(WRONG_SIZE));
});
});
}
I am running this from a browser (client-side Dart libraries are in use) and it ALWAYS passes, no matter what the value of WRONG_SIZE.
Help appreciated.
In such simple cases you can just return the future. The unit test framework recognizes it and waits for the future to complete. This also works for setUp/tearDown.
main() {
test("Resource Manager Image Load", () {
ResourceManager rm = new ResourceManager();
int WRONG_SIZE = 1000000;
return rm.loadImageManifest("data/rm/test_images.yaml").then((_) {
//^^^^
print("Length="+ rm.images.length.toString()); // PRINTS '6' - WHICH IS CORRECT
expect(rm.images, hasLength(WRONG_SIZE));
});
});
}
The problem is that your code returns a Future, and your test completes before the code in the Future has finished, so there's nothing to cause it to fail.
Check out the Asynchronous Tests section on the Dart site. There are methods like expectAsync that allow the future to be passed to the test framework so that it can wait for them to complete and handle the result correctly.
Here's an example (note the expect call is now inside the function passed to expectAsync)
test('callback is executed once', () {
// wrap the callback of an asynchronous call with [expectAsync] if
// the callback takes 0 arguments...
var timer = Timer.run(expectAsync(() {
int x = 2 + 3;
expect(x, equals(5));
}));
});
I am using jslint. I have this tolerance setting in my comments.
/*jslint todo: true*/
The online tester passes it. I have it passing in a piece of code tested earlier. However, later on I get this failure message:
Missing property name.
Does anyone know why the property name is regarded as missing in the second place and not the first?
Update 17/072014 15:03 - included code
I was able to isolate the issue with the following code. Below you can see the parts of the code if run in lint.com and what errors they output:
/**
* #todo "Unexpected TODO comment".
*/
/*jslint todo: true*/
/**
* #todo Will be tolerated by jslint.
*/
/*jslint todo: false*/
var obj = {
/**
* #todo "Unexpected TODO comment".
*/
};
/*jslint todo: true*/
var obj = {
/**
* #todo jslint will tolerate this line.
*/
};
/*jslint todo: false*/
var obj = {
/*jslint todo: true*/
/**
* #todo jslint will never get to this line.
*/
/*jslint todo: false*/
};
What I found was that the tolerance properties could not be set inside an object literal. I can now work around the issue.
I had trouble finding documentation on the issue because my searches for 'literal' were being obscured by the literal notation errors. Does anyone know why this occurs or if there is documentation to explain it, or even just state that it happens?
JSLint does not like it when you introduce /* */ comments inside of objects.
For example, I had this error with this code:
config.output = {
/*jslint nomen:true*/
path: __dirname + '/public',
/*jslint nomen:false*/
publicPath: BUILD ? '/' : 'http://localhost:8080/',
filename: BUILD ? '[name].[hash].js' : '[name].bundle.js',
chunkFilename: BUILD ? '[name].[hash].js' : '[name].bundle.js'
};
And it was resolved by changing that to:
/*jslint nomen:true*/
config.output = {
path: __dirname + '/public',
publicPath: BUILD ? '/' : 'http://localhost:8080/',
filename: BUILD ? '[name].[hash].js' : '[name].bundle.js',
chunkFilename: BUILD ? '[name].[hash].js' : '[name].bundle.js'
};
/*jslint nomen:false*/
I know it's been a year or two since you asked but, hopefully this helps anyone else who is looking.
According to "Reference bundles on requirejs" the solution to the problem of getting requirejs to reference modules delivered in a bundle is to name them with a name exactly matching the filename so that with optimisations turned off they resolve from the filesystem and with optimisations turned on they resolve from the bundle.
This is the start of knockout-jqueryui
/*! knockout-jqueryui - v1.0.0 - 3/21/2014
* https://github.com/gvas/knockout-jqueryui
* Copyright (c) 2014 Vas Gabor <gvas.munka#gmail.com>; Licensed MIT */
/*global require, define, exports*/
/*jslint browser:true, maxlen:256*/
(function (root, factory) {
'use strict';
if (typeof exports === 'object') {
// CommonJS
factory(exports, require('jquery'), require('knockout'), require('jquery-ui'));
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['exports', 'jquery', 'knockout', 'jquery-ui'], factory);
} else {
// Browser globals
factory((root.kojqui = {}), root.jQuery, root.ko);
}
} (this, function (exports, $, ko) {
'use strict';
As you can see it's anonymous. I have two conspicuous options:
Edit the file and give it a name matching the file.
Somehow wrap it so as to name it.
Option one is straightforward:
if (typeof exports === 'object') {
// CommonJS
factory(exports, require('jquery'), require('knockout'), require('jquery-ui'));
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as a module named 'knockout-jqueryui'.
define('knockout-jqueryui',
['exports', 'jquery', 'knockout', 'jquery-ui'], factory);
} else {
// Browser globals
factory((root.kojqui = {}), root.jQuery, root.ko);
}
But it doesn't seem like a great idea. I'm pretty new to AMD so if anyone can advise me on a good way to implement an option two wrapper, that would be excellent.
I am pleased to report that I have found a nice clean solution.
Bundling causes knockout-jqueryui.js to be loaded directly by a script tag. As a result it uses the third factory option in the code extract shown in the question, producing a global kojqui.
This global is in scope for main.js and can be captured:
requirejs.config({
paths: {
'text': '../Scripts/text',
'durandal': '../Scripts/durandal',
'plugins': '../Scripts/durandal/plugins',
'transitions': '../Scripts/durandal/transitions'
}
});
define('jquery', function () { return jQuery; });
define('knockout', ko);
define('moment', function () { return moment; });
define('knockout-jqueryui', kojqui);
No change is required in knockout-jqueryui.js and I have verified that this works both with optimisations enabled and with optimisations off for debugging.
JSHint override not being respected.
[Output]: [L59:C38] W117: 'alert' is not defined.
[Output]: /*jshint -W117 */alert("failed to load review data..");/*jshint +W117 */
-- Actual line of code:
$scope.example.$get(
function(data){
$scope.data = //do something;
}, function(message){
/*jshint -W117 */alert("failed..");/*jshint +W117 */
});
I use these for other warnings, but W117 seems to be ignored.
Try have them on a seperate line
/* jshint -W117 */
alert("failed..");
/* jshint +W117 */
Another option to disable the warning is to add this at the top of the file
/* global alert */
alert("failed.."); //jshint ignore:line
You can also create a .jshintrc file with this content:
{
"globals": {
"alert": false
}
}
The globals configuration option says that this variable is a global defined elsewhere, and the false value says that it shouldn't be redefined.