What I tried to accomplish, when I type grunt into my cmd shell, is the following:
start mongod
start elasticsearch
start nodemon
when mongod and elasticsearch runs, execute load_JSON_into_elasticSearch.js (because I need a connection to both of them)
when everything is done, open http://localhost:8080 in the system default browser
The only thing what works at the moment is, that it'll start mongod, elasticsearch and nodemon. That's it...
Gruntfile.js
module.exports = function (grunt) {
grunt.initConfig({
shell: {
mongo: {
command: 'mongod',
options: {
async: true
}
},
elasticsearch: {
command: 'elasticsearch',
options: {
async: true
}
}
},
nodemon: {
dev: {
script: 'server.js'
}
},
execute: {
target: {
src: ['load_JSON_into_elasticSearch.js']
}
},
open: {
dev: {
path: 'http://localhost:8080',
app: 'Google Chrome'
}
}
});
grunt.loadNpmTasks('grunt-shell-spawn');
grunt.loadNpmTasks('grunt-nodemon');
grunt.loadNpmTasks('grunt-execute');
grunt.loadNpmTasks('grunt-open');
grunt.registerTask('startServices', ['shell', 'nodemon']);
grunt.registerTask('indexAndBrowser', ['execute', 'open']);
grunt.registerTask('default', 'Start it...', function() {
var done = this.async();
grunt.log.writeln('\n===========================================\nStarting Mongod/ ElasticSearch/ Nodemon...\n===========================================\n');
var init = function () {
grunt.task.run('startServices');
var process = function () {
setTimeout(function () {
grunt.log.writeln('\n==================================\nStarting Index and Browser...\n==================================\n');
grunt.task.run('execute');
}, 20000);
};
process();
done();
};
init();
});
};
I personally would recommend using grunt-run (I wrote it to manage similar needs).
https://www.npmjs.org/package/grunt-run
It has a few useful features that you could take advantage of. Here is an example of what using grunt-run might look like for you:
grunt.initConfig({
run: {
elasticsearch: {
cmd: 'elasticsearch',
options: {
wait: false,
ready: /started/
}
},
mongo: {
command: 'mongod',
options: {
wait: false,
ready: /waiting for connections on port/
}
},
load_json_into_es: {
args: [
'load_JSON_into_elasticSearch.js'
]
}
},
open: {
dev: {
path: 'http://localhost:8080',
app: 'Google Chrome'
}
},
nodemon: {
dev: {
script: 'server.js'
}
}
});
grunt.loadNpmTasks('grunt-run');
grunt.loadNpmTasks('grunt-nodemon');
grunt.loadNpmTasks('grunt-open');
grunt.registerTask('default', [
'run:mongo'
'run:elasticsearch'
'run:load_json_into_es',
'nodemon',
'open'
]);
Related
I'm very new to Grunt (started this Friday).
when I run Grunt Serve, it opens a page with just the words "cannot GET/" on it. I found the ModRewrite fix and implemented it.
Yet when I try to get my grunt working, it still returns the same error.
Any help would be greatly appreciated
Here is my Gruntfile:
/* global module:true
*
* Gruntfile.js
* npm install -g grunt-cli
* npm install grunt-contrib-less grunt-contrib-watch grunt-contrib-connect --save-dev
*/
module.exports = function(grunt) {
'use strict';
var serveStatic = require('serve-static');
var phpMiddleware = require('connect-php');
var modRewrite = require('connect-modrewrite')
grunt.initConfig({
connect: {
server: {
options: {
port: 8765,
livereload: 35729,
open: true,
middleware: function(connect, options) {
var middlewares;
middlewares = [];
middlewares.push( modRewrite( ['^[^\\.]*$ /index.html [L]'] ) );
var directory = options.directory || options.base[options.base.length - 1];
if (!Array.isArray(options.base)) {
options.base = [options.base];
}
// Magic happens here
middlewares.push(phpMiddleware(directory));
options.base.forEach(function(base) {
// Serve static files.
middlewares.push(serveStatic(base));
});
// Make directory browse-able.
//middlewares.push(connect.directory(directory));
return middlewares;
}
}
}
}
,
// Less files are in app/less, output is in app/css
less: {
development: {
options: {
paths: ["./less"],
yuicompress: false
},
files: {
"./css/style.css": "./less/style.less"
}
}
},
watch: {
options: {
livereload: 35729
},
files: "./less/*.less",
tasks: ["less"]
},
uglify: {
dist: {
options: {
sourceMap: 'js/map/source-map.js'
},
files: {
'js/plugins.min.js': [
'js/source/plugins.js',
'js/vendor/**/*.js',
'js/vendor/modernizr*.js'
],
'js/main.min.js': [
'js/source/main.js'
]
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-uglify');
// Run grunt server to get going
grunt.registerTask('serve', [
'connect',
'watch'
]);
grunt.registerTask('server', function () {
grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.');
grunt.task.run(['serve']);
});
};
Changed middlewares.push( modRewrite( ['^[^\\.]*$ /index.html [L]'] ) );
to middlewares.push( modRewrite( ['^[^\\.]*$ /index.php [L]'] ) );
I've the following grunt file which runs the mocha tests OK (I get results of the test after running grunt.js)Now I want to add a code and I use the https://github.com/taichi/grunt-istanbul module. but when I run the grunt.js nothing happen,any idea?
What I want is simply after that mocha test are running it will run the code coverage with some reports? any new code coverage will be great
This is my project structure
myApp
-server.js
-app.js
-test
-test1.spec
-test2.spec
-test-reports
-grunt.js
-utils
-file1.js
-file2.js
-controller
-file1.js
-file2.js
This is the code inside the grunt for what I've tried
module.exports = function (grunt) {
var path = require('path');
process.env.RESOURCE_PATH_PREFIX = "../";
var d = new Date();
var datestring = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear() + " " +
d.getHours() + ":" + d.getMinutes();
var npmCommand = path.dirname(process.execPath).concat('/npm');
var reportDir = "./test-reports/" + datestring;
grunt.initConfig({
jasmine_nodejs: {
// task specific (default) options
options: {
specNameSuffix: ["-spec.js"],
helperNameSuffix: "helper.js",
useHelpers: false,
stopOnFailure: false,
reporters: {
console: {
colors: true,
},
junit: {
savePath: "./test-reports",
filePrefix: "testresult",
}
}
},
test: {
specs: [
"test/*",
]
},
makeReport: {
src: './test-reports/coverage.json',//should I create this file?or its created automatically ?
options: {
type: ['lcov', 'html'],
dir: reportDir,
print: 'detail'
}
},
coverage: {
APP_DIR_FOR_CODE_COVERAGE: "./utils/*.js",//HERE IS THE PATH OF THE UTILS Folder which all the js login which I want to test there
clean: ['build'],
instrument: {
files: tasks,//WHAT IS TASKS????
options: {
lazy: true,
basePath: 'build/instrument/'//I DONT KNOW WHAT IT IS???
}
},
reloadTasks: {
rootPath: 'build/instrument/tasks'//SHOULD I USE IT????
},
storeCoverage: {
options: {
dir: reportDir
}
}
}
});
grunt.loadNpmTasks('grunt-jasmine-nodejs');
grunt.loadNpmTasks('grunt-istanbul');
grunt.registerTask('default', ['jasmine_nodejs']);
grunt.registerTask('cover', ['instrument', 'test',
'storeCoverage', 'makeReport']);
};
If there is mocha code coverage which can help it will be great either, I want that after I run the test I will able to see report with all the code coverage.
I want that the coverage will be done for folder utils and controller (all the files there) how should I config that?
UPDATE
This is what I use for jasmin and I think I should change to mocha
jasmine_nodejs: {
// task specific (default) options
options: {
specNameSuffix: ["-spec.js"], // also accepts an array
helperNameSuffix: "helper.js",
useHelpers: false,
stopOnFailure: false,
reporters: {
console: {
colors: true,
cleanStack: 1, // (0|false)|(1|true)|2|3
verbosity: 4, // (0|false)|1|2|3|(4|true)
listStyle: "indent", // "flat"|"indent"
activity: false
},
junit: {
savePath: "./test-reports",
filePrefix: "testresult",
consolidate: true,
useDotNotation: true
}
}
},
test: {
// target specific options
options: {
useHelpers: false
},
// spec files
specs: [
"test/*",
]
}
},
How should I change it?
The syntax of my test are similar(jasmine/mocha) and what I want is simply to run my test and after run the code coverage
I'll give you an indirect answer. I've gotten code coverage to work before, but with a different plugin (and with mocha). I'm not sure if you're open to swapping out jasmine for mocha but I will say that I struggled with various code coverage plugins before coming across this one. I think you'll agree the configuration is both concise and obvious.
The plugin you want is grunt-mocha-istbanbul, and here is a sample configuration for your Gruntfile:
module.exports = function(grunt) {
grunt.initConfig({
clean: ['coverage'],
mocha_istanbul: {
coverage: {
src: 'test',
options: {
timeout: 20000,
'report-formats': 'html',
print: 'summary',
check: {
lines: 90,
statements: 90,
functions: 100,
branches: 80
}
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-mocha-istanbul');
grunt.registerTask('default', ['clean', 'mocha_istanbul']);
}
I've built a grunt setup for testing using qunit and generating coverage reports with istanbul but I can't get the storeCoverage task to complete without the error: No coverage information was collected.
Gruntfile.js
module.exports = function (grunt)
{
"use strict";
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
connect: {
root_server: {
options: {
port: 2424,
base: 'qunit'
},
}
},
qunit: {
all: ['qunit/test1.html']
},
instrument: {
files: "testable.js",
options: {
lazy: true,
basePath: "qunit/"
}
},
storeCoverage: {
options: {
dir: "report/"
}
},
makeReport: {
src: "report/*.json",
options: {
type: "lcov",
dir: "test",
print: "detail"
}
}
});
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks("grunt-contrib-qunit");
grunt.loadNpmTasks("grunt-istanbul");
grunt.registerTask("default", ["instrument", "connect", "qunit", "storeCoverage"/*, "makeReport"*/]);
};
testable.js
function runable ()
{
return true;
};
test1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>QUnit Example</title>
<link rel="stylesheet" href="qunit.css">
</head>
<body>
<script src="qunit.js"></script>
<script src="testable.js"></script>
<script>
QUnit.test("hello test", function (assert)
{
assert.ok(runable());
});
</script>
</body>
</html>
Everything works, qunit runs and the one assert does run on the instrumented file and succeeds. But when it hits the storeCoverage task it fails. Am I doing something wrong? Any help is appreciated of course.
I was able to get this working using the Gruntfile.js from the grunt-istanbul project.
Their documentation indicates that the reloadTasks is not required, but for me it actually was. The clean task they do also assists to tidy up. Also noted in their docs, you can set an ENV variable, rather than using the env task.
Try adding these to your Gruntfile.js:
env: {
coverage: {
APP_DIR_FOR_CODE_COVERAGE: rootPath + 'path/to/instrument'
}
},
clean: ['path/to/coverage'],
reloadTasks: {
rootPath: 'path/to/instrument'
},
My task looks like this:
grunt.registerTask('cover',[
'env:coverage',
'clean',
'instrument',
'reloadTasks',
'connect',
'qunit',
'storeCoverage',
'makeReport'
]);
The question is a little old, but it might help someone else.
To solve this issue, I needed to point my test code to the instrumented code otherwise the original one.
See the example:
Gruntfile.js:
module.exports = function (grunt) {
grunt.initConfig({
env: {
coverage: {
APP_DIR_FOR_CODE_COVERAGE: '../test/coverage/instrument/lib/'
}
},
clean: {
coverage: {
src: ['test/coverage/']
}
},
instrument: {
files: 'lib/*.js',
options: {
lazy: true,
basePath: 'test/coverage/instrument/'
}
},
mochaTest: {
test: {
options: {
reporter: 'spec'
},
src: ['test/**/*test.js']
}
},
storeCoverage: {
options: {
dir: 'test/coverage/reports'
}
},
makeReport: {
src: 'test/coverage/reports/**/*.json',
options: {
type: 'lcov',
dir: 'test/coverage/reports',
print: 'detail'
}
}
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-istanbul');
grunt.loadNpmTasks('grunt-env');
grunt.registerTask('test', 'mochaTest');
grunt.registerTask('coverage', ['env:coverage', 'clean', 'instrument', 'mochaTest', 'storeCoverage', 'makeReport']);};
In you test code, point to the instrumented ones.
As described in the grunt-istanbul site, you can create a requireHelper file.
module.exports = function (path) {
return require((process.env.APP_DIR_FOR_CODE_COVERAGE || '../lib/') + path);
};
And use it in your test file.
var requireHelper = require('./require_helper');
var fileToBeTested = requireHelper('file-to-be-tested');
describe('Test', function () {
it('should do some test', function () {
expect(1).to.equal(1);
});
});
I want to create a Gruntfile.js to run bunch of phantomjs tests, when I execute > grunt run-test from commandline, it runs a bunch of tests. I created a Gruntfile.js and package.json which works ok and it reads bunch of tests from a directory. Now my problem is that when I write a phantomjs test and run the "grunt", it gives me this error:
Error: Cannot find module 'system'
Error: Cannot find module 'phantom'
However phantomjs is installed before by using npm install phantomjs
Example of phantomtest which gives me the above error:
var system = require('system');
if (system.args.length === 1) {
console.log('Try to pass some args when invoking this script!');
} else {
system.args.forEach(function (arg, i) {
console.log(i + ': ' + arg);
});
}
phantom.exit();
When I run phantomjs test1 (name of the test file) it runs the test so I think maybe I should append "phantomjs" somewhere in the Gruntfile. Any idea?
Gruntfile.js
'use strict';
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
jshint: {
all: [
'Gruntfile.js',
'tests/*.js',
'<%= nodeunit.tests %>',
],
options: {
jshintrc: '.jshintrc',
},
},
// Before generating any new files, remove any previously-created files.
clean: {
tests: ['tmp'],
},
// Configuration to be run (and then tested).
testArgs: {
configFile:"test/testConf.js",
options: {
args: {
params: {
number: 1,
bool: true,
str: "string",
nil: null, // Null is not supported.
obj: {
array: [1, 2, 3],
undef: undefined
}
},
capabilities: {
'browserName': 'chrome'
},
rootElement:"body",
specs:["test/argsTest.js"],
verbose:true
}
}
},
testDebug: {
configFile:"test/testConf.js",
options: {
debug:true,
args: {
specs:["test/debugTest.js"],
}
}
},
// Unit tests.
nodeunit: {
tests: ['tests/*_test.js'],
},
});
// Actually load this plugin's task(s).
grunt.loadTasks('tests');
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-nodeunit');
// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.
grunt.registerTask('test', ['clean']);
// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test']);
};
I am having a go at grunt for optimizing my Durandal SPA. It seems to work great but now I would like to output a second file called libs.js which is the merged uglified version of all my required libraries but my first dist is getting ignored and still the only file I get is main-built.js
I only get one file so app/libs.js never gets created. I also have no grunt errors.
Here is my Gruntfile:
module.exports = function (grunt) {
grunt.initConfig({
durandal: {
libs: {
src: [
"../scripts/jquery-1.9.1.js",
"../scripts/typeahead.js",
"../scripts/jquery-ui-1.10.3.js",
"../scripts/knockout-3.0.0rc.js",
"../scripts/toastr.js",
"../scripts/q.js",
"../scripts/breeze.min.js",
"../scripts/bootstrap.js",
"../scripts/moment.js",
"../scripts/lodash.js",
"../scripts/respond.js",
"../scripts/knockout-sortable.js",
"../scripts/knockout-bootstrap.js",
"../scripts/knockout.validation.js",
],
dest: 'scripts/libs.js',
options: {
uglify2: {
compress: {
global_defs: {
DEBUG: false
}
}
}
}
},
dist: {
src: [
"app/**/*.*",
"scripts/durandal/**/*.*"
],
options: {
baseUrl: "app/",
mainPath: "app/main.js",
out: "app/main-built.js",
uglify2: {
compress: {
global_defs: {
DEBUG: false
}
}
}
}
}
}
});
grunt.loadTasks('tasks');
grunt.registerTask('default', ['durandal']);
};
This is JavaScript. If you create an object like { a: 'a', a: 'b' }, the first key will be overwritten by the second by the VM.
Instead of configuring it like this:
dist: {
// config goes here
},
dist: {
// config goes here
}
Try
libs: {
// config goes here
},
main: {
// config goes here
}