Skip to content

Instantly share code, notes, and snippets.

@nicosantangelo
Created February 17, 2014 18:05
Show Gist options
  • Save nicosantangelo/9055834 to your computer and use it in GitHub Desktop.
Save nicosantangelo/9055834 to your computer and use it in GitHub Desktop.
grunt-watch configuration to run with Rails and Jasmine (grunt-contrib-jasmine)
// This file expects the specs to have the same name ending with '.spec.js' and to be in the same folder structure than the source
// Example:
// /app/assets/javascripts/models/todo.js
// /spec/javascripts/models/todo.spec.js
// It runs only the spec of the changed file or, if it's not found, runs the entire suite
module.exports = function(grunt) {
grunt.initConfig({
jasmine: {
all: {
src: [
"http://localhost:3000/assets/application.js"
],
options: {
version: "2.0.0",
specs: [
"spec/javascripts/**/*.spec.js"
]
}
}
},
watch: {
scripts: {
files: ["app/assets/javascripts/**/*.js", "spec/javascripts/**/*.spec.js"],
options: {
spawn: false,
}
}
}
});
grunt.event.on('watch', function(action, changedFilePath) {
var specsPath = "spec/javascripts/";
// If the file is a spec run it
if(changedFilePath.search(".spec.js") > -1) {
specsPath = changedFilePath;
} else {
// Find and run the spec for this particular file, if it exists
// If it doesn't run all files
var filepath = specsPath + changedFilePath.split("/javascripts")[1].replace(/\.js$/, ".spec.js");
if (grunt.file.exists(filepath)) {
specsPath = filepath;
} else {
specsPath += "**/*.spec.js";
}
}
grunt.config("jasmine.all.options.specs", [specsPath]);
grunt.task.run("jasmine:all");
});
grunt.loadNpmTasks("grunt-contrib-jasmine");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.registerTask("default", ["watch"]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment