Presentation at: Grunt-tdd presentation on YouTube
It is difficult to get going with TDD. There are a lot of presentations on how to write tests, but really, it is not understanding how to write tests that prevents you from doing TDD, it is the process. First of all it has to be easy to set up your test environment and even more important, you need automatic feedback when writing tests. It should not take you out of the process of implementing code, but naturally be a part of it.
Grunt-tdd ideally runs on your second monitor, in a browser window. When you write your tests it will automatically refresh. It will also refresh when you start implementing your code. This automatic refresh will let you easily jump between your test-file and implemention-file, keeping you inside your IDE.
Important! Due to watching a lot of files for changes when writing tests it is needed to increase MacOSX default open file limit, which is very low by default. Create/Edit your ~/.bash_profile file and add the following line: ulimit -S -n 2048 (or higher).
In your Gruntfile.js:
module.exports = function (grunt) {
grunt.initConfig({
tdd: {
// NODE CONFIG
node: {
files: {
sources: ['server/src/**/*.js'], // Where your application files are located
libs: [], // Any general libs needed to be loaded, will be loaded from your node_modules folder
tests: ['server/tests/**/*-test.js'] // Where your tests are located
},
options: {
node: true, // Set to true if testing node code
runner: 'jasmine', // jasmine, mocha or buster
expect: true // Use the expect.js library for assertions
}
},
// BROWSER CONFIG
browser: {
files: {
sources: ['client/src/**/*.js'], // Where your application files are located
libs: [ // Libs loaded in order
'client/libs/jquery.js',
'client/libs/underscore.js',
'client/libs/backbone.js'],
tests: ['client/tests/**/*-test.js'] // Where your tests are located
},
options: {
runner: 'buster', // jasmine, mocha or buster
expect: true, // Use the expect.js library for assertions
sinon: true // For spies, stubs and fake XHR
}
},
// REQUIREJS CONFIG
browser2: {
files: {
sources: ['client/src/**/*.js'], // Where your application files are located
libs: [ // Libs loaded in order
'client/libs/jquery.js',
'client/libs/underscore.js',
'client/libs/backbone.js'],
tests: ['client/tests/**/*-test.js'] // Where your tests are located
},
options: {
runner: 'mocha', // jasmine, mocha or buster
expect: true // Use the expect.js library for assertions
requirejs: {
baseUrl: 'client/'
}
}
}
}
});
grunt.loadNpmTasks('grunt-tdd');
}