This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// index.js | |
function myTask(args) { | |
if (somethingBadHappened()) { | |
throw new Error('Too many cooks will spoil the broth.'); | |
} | |
} | |
// tasks/task.js | |
module.exports = function(grunt) { | |
// Sync version |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// index.js | |
function myTask(args) { | |
} | |
// tasks/task.js | |
module.exports = function(grunt) { | |
grunt.registerMultiTask('my-task', function() { | |
var myTask = require('../'); | |
var options = this.options(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
module.exports = function(grunt) { | |
grunt.initConfig({}); | |
grunt.registerTask('this-will-fail', function() { | |
var done = this.async(); | |
setTimeout(function() { | |
done('An error has occurred.'); | |
}, 500); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
module.exports = function(grunt) { | |
grunt.registerTask('my-task', function myTaskFn() { | |
// This code only gets run when your task executes, | |
// so you can do work here and not worry about | |
// slowing everyone else down. | |
var bigDep = require('big-dependency'); | |
bigDep(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app code file | |
const constants = require('./constants'); | |
module.exports = function getGreeting(name) { | |
return CONSTANTS.GREETING_PREFIX + name; | |
} | |
// test code file snippet | |
expect(getGreeting('Sam')).to.equal('Hi, Sam'); | |
// constants file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app code file | |
// app/utils/get-greeting.js | |
const constants = require('../constants'); | |
module.exports = function getGreeting(name) { | |
return CONSTANTS.GREETING_PREFIX + name; | |
} | |
// test code file snippet | |
// test/utils/get-greeting.test.js | |
const constants = require('../../app/constants'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make a bunch of stub objects like this, | |
// commingling mock behavior with test assertions. | |
const npm = { | |
config: { | |
get: function(prop) { | |
expect(prop).to.equal('registry'); | |
if (prop === 'registry') { | |
return npmRegistry; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const writtenManifest = readJson('test-dir/manifest.json'); | |
// bad | |
expect(manifest).to.deep.equal({ | |
// One giant hardcoded object, including fields we are about... | |
foo: 'bar', | |
odp: 'quux', | |
// ...and fields we don't | |
superfluousField: 'We do not really care about this field ' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// heartbeat.js | |
module.exports = function* heartbeat(next) { | |
if (this.path !== '/heartbeat') { | |
return yield next; | |
} | |
this.body = 'OK'; | |
} | |
// in the index.js, we use the middleware |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BAD APPROACH | |
const heartbeat = require('./heartbeat'), | |
coTape = require('co-tape'), | |
tape = require('tape'); | |
tape('heartbeat', function(t) { | |
t.test('serves heartbeat request', coTape(function*(t) { | |
t.plan(1); | |
// add some knowledge of implementation details of what we're testing |