I use grunt to run my mocha test and I see the test result in the console which is OK, the problem is that this task are generating report but when you run this HTML report you just see the log of running in text...I want to see the test aggregations ,and the mocha unit test are run OK, what I am missing here?
mochaTest: {
test: {
options: {
reporter: 'spec',
colors: true,
summery: true,
captureFile: 'results.html', // Optionally capture the reporter output to a file
quiet: false, // Optionally suppress output to standard out (defaults to false)
clearRequireCache: true // Optionally clear the require cache before running tests (defaults to false)
},
src: ['test/*spec.js'],
excludes: ['plugins']
},
'travis-cov': {
options: {
reporter: 'travis-cov'
}
}
},
I use the package
grunt.loadNpmTasks('grunt-mocha-test');
https://github.com/pghalliday/grunt-mocha-test
I want Report like this or any other nice html report which I can use...
You can use Mochawesome is a custom reporter for use with the Javascript testing framework, mocha. It generates a nice HTML/CSS report that helps visualize your test suites:
First, you need to install the plugin:
npm install --save-dev mochawesome
Then you change your grunt-mocha-test reporter:
mochaTest: {
test: {
options: {
reporter: 'mochawesome', //You need to change this !
colors: true,
summery: true,
captureFile: 'results.html',
quiet: false,
clearRequireCache: true
},
src: ['test/*spec.js'],
excludes: ['plugins']
},
'travis-cov': {
options: {
reporter: 'travis-cov'
}
}
},
Related
Using
mochaTest: {
test: {
options: {
reporter: 'html-cov',
captureFile: 'coverage.html'
},
src: ['tests/*.js']
}
}
in my gruntfile is gives me
"html-cov" reporter not found
warning.
How can I get my test result in a html file and view it ?
Thx..
Also I'm using grunt.loadNpmTasks('grunt-selenium-standalone') task.
Please check the following link:
https://github.com/pghalliday/grunt-mocha-test
Here you get the complete example, may it'll help you..
You can use Mochawesome. It is a custom reporter for use with the Javascript testing framework, mocha. It generates a nice HTML/CSS report that helps visualize your test suites.
First, you need to install the plugin:
npm install --save-dev mochawesome
Then you change your grunt-mocha-test reporter:
mochaTest: {
test: {
options: {
reporter: 'mochawesome', //You need to change this !
colors: true,
summery: true,
captureFile: 'results.html',
quiet: false,
clearRequireCache: true
},
src: ['test/*spec.js'],
excludes: ['plugins']
},
'travis-cov': {
options: {
reporter: 'travis-cov'
}
}
},
I am trying to add testing to the website I'm building. I'm using Mocha as my testing framework and Chai and expect as my assertion library. I made a simple test just to make sure things work and then I created a Gruntfile to run my tests. The test is a simple test that just verifies that true === true and it worked both locally and on Travis CI. Now, even though I haven't changed anything in the test, it only works locally, but fails on Travis CI. It was passing before and it still passes locally, so I'm not sure what to change.
My simple test code looks like this:
'use strict';
var chai = require('chai');
var expect = chai.expect;
describe('Test that tests run', function(done) {
it('should run a test', function(done) {
expect(true).to.eql(true);
done();
});
});
My Gruntfile looks like this:
'use strict';
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-simple-mocha');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-jscs');
// initialize Grunt
grunt.initConfig({
// create jshint task
jshint: {
dev: {
// tell jshint what check
src: ['Gruntfile.js', 'server.js', 'js/**/*.js', 'models/**/*.js', 'routes/**/*.js', '!build/**', '!tests/client/bundle.js', '!tests/karma_tests/bundle.js', '!js/imageMapResizer.min.js', '!js/kickstart.js', '!js/form-validator.js'],
options: {
node: true,
globals: {
describe: true,
it: true,
before: true,
after: true,
beforeEach: true,
afterEach: true,
res: true
}
}
},
mocha: {
// tell mocha where test files are
src: ['tests/**/*.js', '!tests/client/bundle.js', '!tests/karma_tests/bundle.js'],
options: {
node: true,
globals: {
describe: true,
it: true,
before: true,
after: true,
beforeEach: true,
afterEach: true,
res: true,
expect: true
}
}
},
// create jscs task
jscs: {
dev: {
// tell jscs to test the same files as jshint
src: ['<%= jshint.dev.src %>', '<%= jshint.mocha.src %>']
}
}
},
// create simplemocha task
simplemocha: {
dev: {
src: ['tests/test_entry.js']
}
}
});
// register linting task
grunt.registerTask('lint', ['jshint:dev', 'jshint:mocha' /*, 'jshint:jasmine'*/ ]);
// register mocha test task
grunt.registerTask('test', ['simplemocha:dev']);
grunt.registerTask('default', ['test']);
};
And .travis.yml looks like this:
language: node_js
node_js:
- "4.1"
- "4.0"
- "0.12"
- "0.11"
- "0.10"
- "0.8"
- "0.6"
- "iojs"
before_install:
- npm install -g grunt-cli
script: grunt test
Let me know if you have questions or want to see more code. Thanks in advance for all your help!
After some more digging with Travis CI I found there were 2 problems. The first was that node.js versions 0.6 and 0.8 were incompatible with many of my node packages. The other problem was 2 node packages I had included were not compatible with linux, which is the OS Travis CI uses to run tests. The packages were node-sqlserver-unofficial and msnodesqlv8.
I don't really need to work on node.js versions 0.6 or 0.8 and the I can work without the 2 node packages, so once I removed the the older node versions and the packages and my tests passed with flying colors.
I am having issues setting up JSHint to run multiple tasks. Here is what I have:
jshint: {
app: {
files: [
'web/**/*.js',
'!web/app.js',
'!web/lib/**',
'!web/build/**'
],
options: {
// Just let the templates do whatever they want when compiled
ignores: ['web/templates/*.js'],
// options here to override JSHint defaults
loopfunc: true,
newcap: false,
reporter: require('jshint-stylish'),
globals: {
jQuery: true,
gadget: true
}
}
},
test: {
files: [
'web/**/*.js',
'!web/app.js',
'!web/lib/**',
'!web/build/**'
],
options: {
// just let the templates do whatever they want when compiled
ignores: ['web/templates/*.js'],
reporter: require('jshint-stylish'),
jshintrc: '.jshintrc'
}
}
}
When I try to run grunt jshint:app or grunt jshint:test, I get errors that there is no task for either of those.
Thank you for your help if you see anything that I have missed.
I think you need to add
grunt.loadNpmTasks('grunt-contrib-jshint');
to your grunt file
I am new to nodeJS and grunt. I have this Gruntfile in this project and I want to do live reload for all the html files in my project, so that I do not have to refresh my browser all the time to detect new changes. Somehow I encounter error with the following code:
module.exports = function (grunt)
{
// Project configuration.
grunt.initConfig(
{
// Task configuration.
jshint:
{
options:
{
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
unused: true,
boss: true,
eqnull: true,
browser: true,
globals: {}
},
gruntfile:
{
src: 'Gruntfile.js'
},
lib_test:
{
src: ['lib/**/*.js', 'test/**/*.js']
}
},
connect:
{
server:
{
options:
{
hostname: 'localhost',
port: 80,
base: 'src',
keepalive: true,
livereload: true
}
}
},
watch:
{
options:
{
livereload:true
}
}
});
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
// Default task.
grunt.registerTask('default', ['connect', 'watch']);
};
It seems that when I start 'grunt default' it would not execute task watch because during connect it is keepalive.
I will be grateful if any1 can explain to me why I have this error when JSHint check my code and suggest a solution to this.
Your watch task does not have any tasks or files. For it to work with grunt-contrib-connect, you need to include more than just the livereload option, like so:
watch: {
options: {
livereload: true
},
taskName: { // You need a task, can be any string
files: [ // Files to livereload on
"app/js/*.js",
"app/templates/*.html"
]
}
}
Or alternately:
watch: {
taskName: {
options: { // Live reload is now specific to this task
livereload: true
},
files: [ // Files to livereload on
"app/js/*.js",
"app/templates/*.html"
]
}
}
All files that match the glob patterns here should then work as you're expecting. You do not need to specify a tasks parameter here if you are just live reloading these for the browser.
Also, if you are going to be using your connect server alongside watch, you should remove the keepalive parameter as it is a blocking task and can prevent executing the watch task:
connect: {
server: {
options: {
port: 8080,
base: 'src',
livereload: true
}
}
}
You need node:true in your jshint config, take a look at this example .jshintrc.
For the watch and livereload, you need to specify which files to watch, and what tasks to execute in your Gruntfile, again, take a look at this sample Gruntfile.
For example like this:
watch: {
coffee: {
files: ['<%%= config.app %>/scripts/{,*/}*.{coffee,litcoffee,coffee.md}'],
tasks: ['coffee:dist']
},
}
In this example, you specify a glob as files option and whenever that files change the according tasks are run.
I have recently discovered grunt as it is used within an opensource project I have started to work on. Having not worked with grunt before I am struggling to see how it works, or in my case doesn't.
The grunt file is supplied by the project and I assume it works for everyone else. I have installed grunt and the necessary grunt modules have all installed in the "Node_modules" directory.
When running the grunt file the first process performs a number of concatenations and this seems to work fine. The concatenated files are created.
All of the other steps do not seem to execute. The files they are intended to create are not created. There is no error message displayed on the console when grunt is executed.
I'm stumped - anyone have any clues what might be the problem.
The grunt file is :
/*global module:false*/
module.exports = function(grunt) {
// Project configuration...
grunt.initConfig({
manifest: grunt.file.readJSON('chrome/manifest.json'),
concat: {
dist: {
src: ['chrome/js/requester/**/*.js'],
dest: 'chrome/js/requester.js'
},
requester_html: {
src: [
'chrome/html/requester/header.html',
'chrome/html/requester/sidebar.html',
'chrome/html/requester/main.html',
'chrome/html/requester/loggers/*.html',
'chrome/html/requester/modals/*.html',
'chrome/html/requester/footer.html'
],
dest: 'chrome/requester.html'
},
requester_tester: {
src: [
'chrome/html/requester/header.html',
'chrome/html/requester/sidebar.html',
'chrome/html/requester/main.html',
'chrome/html/requester/modals/*.html',
'chrome/html/requester/loggers/*.html',
'chrome/html/requester/footer.html',
'chrome/html/requester/tester.html'
],
dest: 'chrome/tester.html'
}
},
mindirect: {
dist: {
src: ['chrome/js/requester.js'],
dest: 'chrome/js/requester.min.js'
}
},
watch: {
requester_templates: {
files: ['chrome/html/requester/templates/*'],
tasks: ['handlebars'],
options: {
livereload: true
}
},
requester_js: {
files: ['chrome/js/requester/**/*.js'],
tasks: ['concat:dist'],
options: {
livereload: true
}
},
requester_html: {
files: ['chrome/html/requester/*', 'chrome/html/requester/modals/*', 'chrome/html/requester/loggers/*'],
tasks: ['concat:requester_html', 'concat:requester_tester'],
options: {
livereload: true
}
},
requester_css: {
files: ['chrome/css/**/*.scss'],
tasks: ['sass'],
options: {
livereload: true
}
}
},
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
browser: true
},
globals: {
jQuery: true
}
},
handlebars: {
compile: {
options: {
partialsUseNamespace: true,
namespace: 'Handlebars.templates',
processPartialName: function(filePath) {
var pieces = filePath.split("/");
var name = pieces[pieces.length - 1].split(".")[0];
return name;
},
processName: function(filePath) {
var pieces = filePath.split("/");
var name = pieces[pieces.length - 1].split(".")[0];
return name;
}
},
files: {
"chrome/html/requester/templates.js": "chrome/html/requester/templates/*"
}
}
},
sass: {
dist: {
files: {
'chrome/css/requester/styles.css': 'chrome/css/requester/styles.scss'
}
}
},
compress: {
main: {
options: {
archive: 'releases/v<%= manifest.version %>.zip'
},
files: [
{src: ['chrome/**', '!chrome/tests/**', '!chrome/manifest_key.json', '!chrome/tester.html'], dest: '/'}, // includes files in path and its subdirs
]
}
}
});
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-handlebars');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-mindirect');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-compress');
// Default task.
grunt.registerTask('default', ['jshint', 'concat']);
grunt.registerTask('package', ['concat', 'handlebars', 'sass', 'compress']);
};
The output from the console is as follows :
Running "jshint:globals" (jshint) task
>> 0 files lint free.
Running "concat:dist" (concat) task
File "chrome/js/requester.js" created.
Running "concat:requester_html" (concat) task
File "chrome/requester.html" created.
Running "concat:requester_tester" (concat) task
File "chrome/tester.html" created.
Done, without errors.
Given that the tasks are defined like this:
grunt.registerTask('default', ['jshint', 'concat']);
grunt.registerTask('package', ['concat', 'handlebars', 'sass', 'compress']);
the output you show is what you'd expect if you are running grunt without a task name. It runs the jshint and concat tasks.
If you want to run the tasks associated with package, then you have to run grunt package to run those tasks.
It looks like I did not understand "tasks" within grunt.
Instead of executing "grunt" which runs the "default" tasks, I had to execute "grunt package" which runs the tasks that I was interested in.
As Louis said, you have to specify the right task to run.
But you can also create or modify the tasks you have in order to make it simpler. In your example you may include package in the default task. Because concat task is already executed inside package, you may just replace it in the default task:
grunt.registerTask('default', ['jshint', 'package']);
grunt.registerTask('package', ['concat', 'handlebars', 'sass', 'compress']);
and, in order to build your site, just type
grunt
both jshint and package tasks will be executed