Check your JavaScript code on every save

Published: 2012-12-25 by Lars  KPIs

Being able to check the quality of your code and run all unit tests in your project on every commit is useful. If you could do it on every save you would get even faster feedback.

Grunt includes a watch facility that will run a task every time some file is changed. You only need to tell Grunt which files to watch and what tasks to run, so change your Gruntfile.js to include:

    grunt.loadNpmTasks('grunt-contrib-watch');
    gruntConfig.watch = {
        scripts: {
            files: ['src/**/*.*'],
            tasks: ['lint', 'test']
        }
    };

You can now open a new terminal window and run

$ grunt watch

Then use your editor and edit index.html to intentionally break the test. Save the file, and watch the immediate feedback in the adjacent terminal window. Fix the test, save the file, and see that everything is back in order. You can't get feedback much faster than that. Enjoy!

This post is part of a series on Continuous Integration for front-end JavaScript, read the other posts in the series:

Discuss on Twitter