Grunt module does not execute - javascript

I spent a few hours last night trying to get my grunt module to execute but can't. Grunt acts like it executed the function but it actually doesn't.
Here is the grunt module: https://github.com/chrisabrams/grunt-commonjs-aws-s3
If you say grunt s3-generate it comes back and says "Done, without errors." - but if you console.log something inside the function, it never fires. I just need to know why it never fires.
Here is an example project that shows the grunt module doesn't run: https://github.com/chrisabrams/stackoverflow-grunt-example

Your s3-generate task is a multitask configured to read this.options() which means your config should look like:
's3-generate': {
options: {
dest: 's3.js',
key: 'a',
},
target: {}
}
If your task isn't going to handle files directly consider using this.data in your task to access the raw object literal passed to s3-generate.

Related

Loading hooks in CucumberJS with Protractor and Gulp

I setup CucumberJS with Protractor and Gulp. I followed the documentation available here:
https://github.com/cucumber/cucumber-js
I have my feature file and step definition file. I also created world.js file in support folder and it is loaded in my step definition file with:
this.World = require("../support/world.js").World;
So the same way as it is presented in the documentation.
Everything works till this moment.
I tried to add some cucumber hooks to my case. I created hooks.js file in the support folder as it is proposed in the documentation, so:
// features/support/hooks.js (this path is just a suggestion)
var myHooks = function () {
this.Before(function (callback) {
// Just like inside step definitions, "this" is set to a World instance.
// It's actually the same instance the current scenario step definitions
// will receive.
// Let's say we have a bunch of "maintenance" methods available on our World
// instance, we can fire some to prepare the application for the next
// scenario:
console.log("Before hook");
// Don't forget to tell Cucumber when you're done:
callback();
});
};
module.exports = myHooks;
The documentation does not say how this hook.js file should be loaded in my step definitions so I assume that it is somehow loaded with the "convention over configuration" approach. Unfortunately, the file is not loaded and the Before method is not executed.
Any ideas?
If hooks are NOT in the same folder as your step_definitions, you would need to explicitly specify where your hooks are using --require. For example,
cucumber.js test/functional/features/xyz.feature
--require test/functional/step_definitions/
--require features/support/ --format=pretty
To avoid this, I usually keep my hooks under step_definitions folder. Since you need to specify require for step_definitions anyways, you don't need to explicitly specify require for hooks. So lets say if your hooks are in test/functional/step_definitions/, with following your hooks should get invoked.
cucumber.js test/functional/features/xyz.feature
--require test/functional/step_definitions/
--format=pretty
Once you have your hooks.js file, go to your cucumberOpts inside of your protractor.conf.js file and add the path to your hooks.js file there, that's it, your hooks.js file will be loaded.
cucumberOpts: {
require: [
conf.paths.e2e + '/steps/**/*Steps.js',
conf.paths.e2e + '/utilities/hooks.js',
],
tags: ['~#wip', '~#manual'],
format: 'pretty'
}
You can also include console.log('Was my hook loaded') in your hooks.js file and search for that log text later to ensure your hook was properly loaded.

Hide 'Running X task' in grunt

I have been working on a project setup and deploy Gruntfile but would like to hide the command line output so that the following:
Running "init" task
Running "prompt:init" (prompt) task
[?] If you continue your project information will be overwritten.
Continue? (Y/n)
becomes
[?] If you continue your project information will be overwritten.
Continue? (Y/n)
when running grunt. I know it's only cosmetic but it's something I would like to do and cannot seem to find anything on Grunt's API documentation to indicate this can be done.
This is currently not supported, but possible thanks to the following workaround (from shama on GitHub):
grunt.log.header = function () {};
Basically, this overrides the log header function (which is responsible for the "running x task" message) by an empty function that does nothing, and more importantly, outputs nothing.
There's another way to do it:
First, run npm install grunt-log-headers to install grunt-log-headers.
Then add require('grunt-log-headers')(grunt); to your Gruntfile.js to enable it.
Finally, add this to any task for which you want to hide the log header:
options: {
gruntLogHeader: false
}
Example:
grunt.initConfig({
sometask: {
options: {
gruntLogHeader: false,
}
}
});
In fact, an issue has already been created for this. It's currently being worked on, and will normally be available in the version 0.5.0.

grunt and qunit - running a single test

I already have grunt-contrib-qunit set up. My Gruntfile.js includes something like this
qunit: { files: ['test/*.html'] }
Now I can run grunt qunit and all my tests run.
Question: how can I run just one single test without running all of them? Is there a way I can overload the value of files from the command line?
You definitely need to look into grunt-contrib-qunit and grunt-contrib-connect (https://github.com/gruntjs/grunt-contrib-qunit and https://github.com/gruntjs/grunt-contrib-connect) as the tandem will provide you with a headless phantom and a local webserver.
UPDATE - as for running just one specific test, you could write something like this, listing your tests as separate targets for your qunit task:
grunt.initConfig({
qunit: {
justSomething: ['test/justsomething.html'],
justSomethingElse: ['test/justsomethingelse.html'],
all: ['test/*.html']
}
});
Then you can call grunt qunit:justSomething, or grunt qunit:all - this is not specific to qunit, though - see http://gruntjs.com/configuring-tasks
Now, if you would really like to use the target to specify a test name, you would go with something like:
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.initConfig({
qunit: {
all: ['test/**/*.html']
}
});
grunt.task.registerTask('foo', 'A sample task that run one test.', function(testname) {
if(!!testname)
grunt.config('qunit.all', ['test/' + testname + '.html']);
grunt.task.run('qunit:all');
});
}
Then call grunt foo:testname.
Yet again, this is not specific to qunit - but rather grunt task writing.
Hope that (finally) helps.

How to disable a task in Grunt?

When grunt.loadNpmTasks is used, a grunt task is automatically available to the command line. It can be useful, but sometimes, I would like this task to be private, so it can be used whithin the Grunt file but not available to the command line.
Here is a contrived example. If I do :
module.exports = function(grunt) {
grunt.initConfig({
clean: {
test: ['test'],
release: ['release']
},
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.registerTask('build', 'Build the project.', function() {
console.log("building project");
});
grunt.registerTask('release', ['clean:release', 'build']);
};
... I can use the following command :
$ grunt release
However, this one is also available, and both clean:release and clean:test will be executed:
$ grunt clean
I do not want that. I want to control what can be called from the command line, since I may not have foreseen some undesirable effects if the user directly calls some tasks or subtasks.
I thought about registering a new clean task to supersedes the main one, and then choose what to call when clean is invoked (or to call nothing at all), but it does not work well since it cannot call the original clean task:
grunt.registerTask('clean', ['clean:release']);
Use grunt.task.renameTask
var ticks = +new Date();
var clean = 'clean-' + ticks;
grunt.task.renameTask('clean', clean);
grunt.registerTask('release', [clean + ':release', 'build']);
grunt.config.set(clean, grunt.config.get('clean'));
Copying the configuration over is important if you want to preserve the targets configuration

I'm trying out Grunt and need a simple way to concatenate my modules

This is my first time using Grunt and I'd like to have it combine all my js modules, each of which is wrapped in an immediately executing function, containing a 'use strict' declaration and put them into one file, wrapped up in only one immediately executing function, with only one 'use strict' declaration.
How is this normally done?
I figured this would be a common use case? Perhaps I'm going about things the wrong way? Should I be using one of the module loading formats (i.e. commonjs, amd) All these files will always be loaded together in the browser, so I actually wouldn't mind removing all the immediately executing functions if that's how people normally go about it. The important part is that the end result is somehow wrapped, passes lint and unit tests and contains the 'use strict' declaration.
(I should clarify, I do have it working, linting, unit-testing, concatenating, and minifying, it just feels like I'm doing something wrong when I see a bunch of unnecessary immediately executing functions in the final concatenated file.)
As of pull request 10, grunt-contrib-concat now has a footer option. Instead of an intro and an outro file, I would use a banner and a footer.
Gruntfile.js
concat: {
dist: {
src: ['src/my-lib.js'],
dest: 'dist/<%= pkg.name %>.js',
options: {
banner: ";(function( window, undefined ){ \n 'use strict';",
footer: "}( window ));"
}
}
}
In my opinion, this is more maintainable and allows for templating using other properties defined in your Gruntfile.
I usually do it like the jQuery team does it. You create an intro.js and outro.js and put everything else in between:
intro.js
;(function( window, undefined ){
'use strict';
outro.js
}( window ));
grunt.js:
concat: {
dist: {
src: [
'js/src/intro.js',
...
'js/src/outro.js'
],
dest: 'js/out/app.js'
}
}
Just want to add to #elclanrs answer that if you want to be able to keep your modules in separate files for easier debugging during development, you would obviously have to wrap them with intro.js and outro.js as well. Using the built-in concat task you'd have to write something like:
concat: {
events_debug: { // wrap the 'events' module in IIFE
src: [
'js/src/intro.js',
'js/src/events.js',
'js/src/outro.js'
],
dest: 'js/out/events.js'
},
callbacks_debug: { // wrap the 'callbacks' module in IIFE
src: [
'js/src/intro.js',
'js/src/callbacks.js',
'js/src/outro.js'
],
dest: 'js/out/callbacks.js'
}
// zzZZZ...
}
Which is very tedious and self-repeating. Perhaps you may want to create a custom task for mass-wrapping files, e.g.
wrap: {
html: {
intro: 'partials/intro.js',
outro: 'partials/outro.js',
src: 'js/*.js',
dest: 'out' // output directory
}
}
There was a question about this recently, see this thread:
Using grunt concat, how would I automate the concatenation of the same file to many other files?
I would recommend you to use my grunt plugin grunt-concat-deps since it automatically resolves your modules independent on your architecture.
PLUS: You don't need any explicit module configuration for the plugin as it relies on declarative and decentralized module definition in a YUIDoc style.
See here for further information: https://github.com/leoselig/grunt-concat-deps

Categories

Resources