Renaming the Istanbul coverage report xml file - istanbul

I am using coverageIstanbulReporter to generate code coverage report for my javascript project. The default output xml file is named as 'cobertura-coverage.xml'. I want to rename this file to something else. I can't find the configuration for this.
coverageIstanbulReporter: {
reports: ['html', 'lcovonly', 'cobertura'],
dir: path.join(__dirname, 'coverage'),
fixWebpackSourcePaths: true
},

It's hinted at in the main readme of the project, but I couldn't find a direct instruction either. I found it by searching all of Github in code, that's usually a good way to find out how other have used things. Which isn't always correct, or applicable, but it will point in the right direction.
You have to add report-config and the name of the report type. Like so:
coverageIstanbulReporter: {
reports: ['html', 'lcovonly', 'cobertura'],
dir: path.join(__dirname, 'coverage'),
fixWebpackSourcePaths: true,
'report-config': {
cobertura: {
file: 'custom_file_name.xml'
}
},
},

Related

Grunt-complexity on all the files in a directory

I'd like to run Grunt-Complexity on all the files in a directory?
I'd like to get this kind of output.
Is there a way?
My js files are all under a subdirectory called "js".
Here's my gruntfile:
/*global module:false*/
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
// Task configuration.
complexity: {
generic: {
src: ['grunt.js', 'js/*'],
//exclude: ['doNotTest.js'],
options: {
breakOnErrors: false,
jsLintXML: 'report.xml', // create XML JSLint-like report
checkstyleXML: 'checkstyle.xml', // create checkstyle report
pmdXML: 'pmd.xml', // create pmd report
errorsOnly: false, // show only maintainability errors
cyclomatic: [3, 7, 12], // or optionally a single value, like 3
halstead: [8, 13, 20], // or optionally a single value, like 8
maintainability: 100,
hideComplexFunctions: false, // only display maintainability
broadcast: false // broadcast data over event-bus
}
}
}
});
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-complexity');
// Default task.
grunt.registerTask('default', 'complexity');
};
I'm simply calling this by typing
grunt
from the command line.
then if I type this
grunt complexity js/*
I get
Warning: Task "js/AgencyMediaController.js" not found. Use --force to continue.
Aborted due to warnings.
And AgencyMediaController.js is the first file in my js directory. So it's having a look and listing the files, but then it crashes.
Thanx!
example:
for all js file in JS folder:
src: ['js/**/*.js']
for ass .scss files in scss folder:
src: ['scss/**/*.scss']
I suggest for you create a config for your src folder can be easy in future folder changes in future projects:
sample:
var src;
config.src = src = {
sassMain : 'scss/main.scss',
distFolder : 'public/stylesheets/lovelycss.dist.css',
devFolder : 'public/stylesheets/lovelycss.dev.css',
libFolder : 'lib/**/*.js',
sassFolder : 'scss/**/*.scss',
spriteCssFolder : 'scss/helpers/_sprite.scss',
spriteDestImg : 'public/images/sprite/spritesheet.png',
spriteSrc : 'public/images/min/*.{png,jpg,gif}',
imageminCwd : 'public/images/',
imageminDest : 'public/images/min'
};
//grunt Watch ===============================
config.watch = {
scripts: {
files: ["<%= src.libFolder %>", "<%= src.sassFolder %>"]
,tasks: ["dev", "sass:dist"]
//,tasks: ["dev",'sass:dist']
}
}
I hope that helped you.
It's been quite a long while since I asked this question. I just ran into the same issue again and found the answer so here it is:
In the end it turned out to be that one of the files I was trying to analyse was causing the crash. This particular Javascript environment allows for C-like preprocessor directives and the Javascript file had something like this:
var mySettings = {
//#ifdef FOO_CONSTANT
setting : constants.FOO_SETTING
//#endif
//#ifdef BAR_CONSTANT
setting : constants.BAR_SETTING
//#endif
};
I guess the problem is that if this is read as strictly Javascript, the preprocessor directives are just plane comments, and there's a comma missing between the two properties, so Grunt complexity is unable to read this because of a syntax error. Using --force makes no difference BTW.
The annoying part is that this is all the error shows:
$ grunt --force
Running "complexity:generic" (complexity) task
Warning: undefined: Unexpected token, expected , (17570:1) Used --force, continuing.
Done, but with warnings.
So while it does say expected , (175:1) it doesn't say in which of the several Javascript files in this project the problem was found!
Just adding exclude: ['path/to/MyFileWithPreprocessorDirectives.js'] to Gruntfile.js in order to exclude this file from the analysis gets me around the problem.

Change format of the Source map created from closure compiler

I have this closure-compiler task defined with following options:
'closure-compiler': {
files: {
},
options: {
externs: [],
compilation_level: 'ADVANCED_OPTIMIZATIONS',
language_in: 'ECMASCRIPT5_STRICT',
create_source_map: '<%= sourceDir %>js/<%= outputName %>.min.js.map',
output_wrapper: '%output%\n//# sourceMappingURL=<%= sourceMapURL %>js/<%= outputName %>.min.js.map'
}
}
the sourcemap is created and it looks like this:
{
"version":3,
"file":"build/js/game.min.js",
"lineCount":39,
"mappings":"AAEA,...",
"sources":["/src/js/utils.js","/src/js/game/Button.js",...],
"names":[...]
}
but then the source map doesnt work, what I need is:
{
"version":3,
"file":"game.min.js",
"lineCount":39,
"mappings":"AAEA,...",
"sources":["utils.js","game/Button.js",...],
"names":[...]
}
what should I do to have the sourcemap created in that form?
For Grunt, there are many options for sourcemaps that have to be handled as a separate build step. It lacks the power of the gulp-sourcemaps plugin and so either each tool has to handle every conceivable option for generating a sourcemap or another tool must be used.
Post processing a sourcemap in this fashion isn't too difficult as sourcemaps are JSON data.
grunt-sourcemap-localize looks to do exactly what you are wanting.

How to force jshint to print source file where the the hint applies?

I am running jshint from grunt.
Here is my config:
jshint: {
files: ['Gruntfile.js', 'app/htmlplayer/js/directives/**/*.js'],
options: {
globals: {
jQuery: true
}
}
},
But after running grunt test which triggers jshint in the console are printed hints and at the end is printed:
"118 errors in 30 files"
Here is one example
How I can find a file where this problem is found?
Is there a way to configure jshint to specify source file responsible for each error?
You will need to change the reporter option, to modify the plugin's output.
By default (when reporter value is null), the output is like -
Switching to checkstyle reporter, will give an output like -
Switching to jslint reporter, will give an output like -
Other than this, you can use custom reporters as well, for example the jshint-stylish -
The grunt task configuration would be -
jshint: {
all: ['entry.js'],
options: {
reporter: require('jshint-stylish')
}
}
You can find other custom reporters at - https://www.npmjs.com/search?q=jshint%20reporter

Interpolated array in Grunt template is interpreted as a string

Previous title: "Why is Grunt's concat task not using dynamic configuration values?"
I am trying to dynamically configure the files that are concatenated by Grunt, and in doing so I came across this issue where the grunt-contrib-concat plugin does not seem to pick up the dynamically set values. At first I thought I was doing something wrong, but after creating my own task and using the same dynamic values everything came out just as intended. So that leaves the question of why is the grunt concat task not doing picking up and using the same values?
A gruntfile that reproduces the behaviour is seen below (gist: fatso83/73875acd1fa3662ef360).
// Grunt file that shows how dynamic config (and option!) values
// are not used in the grunt-contrib-concat task. Run using 'grunt'
module.exports = function(grunt){
grunt.initConfig({
concat : {
foo : {
nonull : true,
src: '<%= grunt.config.get("myfiles") %>',
dest : 'outfile.txt'
}
},
myTask : {
bar : '<%= grunt.config.get("myfiles") %>'
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.registerMultiTask('myTask', function() {
grunt.log.writeln('myTask:' + this.target + ' data=' + this.data);
});
grunt.registerTask('default', ['myTask','concat']);
grunt.config.set('myfiles',['file1.txt', 'file2.txt'])
}
EDIT: A new lead:
After literally hours of going nowhere I came across this sentence on Grunt's homepage:
nonull If set to true then the operation will include non-matching
patterns. Combined with grunt's --verbose flag, this option can help
debug file path issues.
Adding that to the config (edited above to reflect this) I got this error message which at least show that something is doing something with the dynamic values:
Running "concat:foo" (concat) task
>> Source file "file1.txt,file2.txt" not found.
Warning: Unable to write "outfile.txt/file1.txt,file2.txt" file
(Error code: ENOTDIR). Use --force to continue.
After some more debugging in the other task, myTask, I have found out that the data sent in to the task as this.data is a string value, not an array. This is perhaps not very surprising, given that we do string interpolation, but this is not consistent with other interpolation features. For instance will <%= otherTask.fooTarget.src %> get the other task's src property as an array value.
Now the question is really how can I avoid passing the interpolated value as an array, rather than a string, to the concat task?
Updated after reading up on the Grunt source code
After I found out that the problem was our array was interpreted as a string I quickly found a related question with a solution that seemed promising. Simply by enclosing the interpolated array string with curly brackets Grunt was able to find the files!
Unfortunately, the globbing pattern we are effectively creating does not preserve the specified file order. In the related question above I posted a thorough explanation of what was going on and how you can work around it in the general case.
For my specific case, where I reference a field in the configuration object there is actually no need for a function call to retreive it as it is directly available in the templates own scope! Therefore, instead of calling grunt.config.get('myfiles'), I can simply do <%= myfiles %>.
For the example above:
grunt.initConfig({
concat : {
foo : {
nonull : true,
src: '<%= myfiles %>',
dest : 'outfile.txt'
}
},
myTask : {
bar : '<%= myfiles %>'
}
});

browserify: concatenated browserified files => error _prelude.js vs. loading separate files work

Running into a problem when concatenating two browserified files (vendor.js and app.js into combined.js)
Loading combined.js in the browser throws the following in _prelude.js :
Uncaught Error: Cannot find module 'function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}'
while loading the browserified files individually works just fine.
What am I missing?
(let me know if you need more config, happy to provide)
Thanks for your help!
In Gruntfile.js:
browserify: {
vendor: {
src: ['client/requires/**/*.js'],
dest: 'build/vendor.js',
options: {
shim: {
jquery: {
path: 'client/requires/jquery/js/jquery.js',
exports: '$'
},
underscore: {
path: 'client/requires/underscore/js/underscore.js',
exports: '_'
},
backbone: {
path: 'client/requires/backbone/js/backbone.js',
exports: 'Backbone',
depends: {
underscore: 'underscore'
}
},
'backbone.marionette': {
path: 'client/requires/backbone.marionette/js/backbone.marionette.js',
exports: 'Marionette',
depends: {
jquery: '$',
backbone: 'Backbone',
underscore: '_'
}
},
eventsource: {
path: 'client/requires/eventsource/eventsource.js',
exports: 'EventSource'
},
moment: {
path: 'client/requires/moment/moment.js',
exports: 'moment'
},
bootstrap: {
path: 'client/requires/bootstrap/js/bootstrap.js',
exports: null
}
}
}
},
app: {
files: {
'build/app.js': ['client/src/main.js']
},
options: {
transform: ['node-underscorify'],
debug: true,
external: ['jquery', 'underscore', 'backbone', 'backbone.marionette', 'eventsource', 'moment', 'bootstrap']
}
},
},
concat: {
'build/<%= pkg.name %>.js': ['build/vendor.js', 'build/app.js']
},
I have done some initial investigation and there appears to be a definite problem with the preamble. I've raised an issue with the grunt-browserify maintainers so lets see what comes of that.
For now I am concatenating a file between vendor.js and app.js as a way to fix the preamble as follows:
Gruntfile.js
concat: {
'build/<%= pkg.name %>.js': ['build/vendor.js', 'client/src/fix_browserify', 'build/app.js']
},
fix_browserify
require=
Note there is no carriage return or linefeed in the above file
I am unsure if there will be any unintended consequences of defining require twice but it appears to be working in my limited use. If you have more than just one app bundle then you would similarly need to interleave concatenation of the fix_browserify file before each of the bundles.
If I become aware of any better solutions I'll update this answer.
UPDATE
I ended up ditching grunt/browserify after looking at how browerify worked and just went with brunch which was far easier to setup and also does much faster rebuilds when things change. Whilst not a grunt replacement it builds everything I need.
Quick solution
We fixed this error by ensuring that the JS bundle in each file ended with a semi-colon, like Keven Wang mentioned, before being concatenated.
More information
It seems like as of writing, Browserify omits the semi-colon (possibly due to this issue) if you have enabled generating source maps (controlled by debug option). If we don't provide this option, Browserify adds the semi-colon and there are no errors after concatenating.
There seem to be issues caused by whatever the default behaviour is—omitting or appending the semi-colon (see this issue—sometimes you want to wrap the output in an expression, so don't want the semi-colon). It depends heavily on your build pipeline too, as we had no errors in another project with a slightly different build process that was running the output through Grunt's uglify task, stripping the source maps, and adding the semi-colon at the end.
I think Andrew's solution of adding a spacer file is unnecessarily hacky, and there are better solutions available to ensure the output is as you expect (i.e. with semi-colons at the end) before concatenating the bundles.

Resources