Skip to content

Instantly share code, notes, and snippets.

View NickHeiner's full-sized avatar
💭
Wubba lubba dub dub!!

Nick Heiner NickHeiner

💭
Wubba lubba dub dub!!
View GitHub Profile
// Good approach
'use strict';
const coRequest = require('co-request'),
tape = require('tape'),
coTape = require('co-tape'),
makeServer = require('../..');
tape('heartbeat', function(t) {
const app = makeServer();
// 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
// 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
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 '
// 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;
}
}
// 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');
// 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
@NickHeiner
NickHeiner / task-slow-good.js
Created February 23, 2015 19:36
A better way to load deps
'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();
});
@NickHeiner
NickHeiner / Gruntfile.js
Created February 19, 2015 23:37
Bad error handling
'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);
});
@NickHeiner
NickHeiner / grunt-pass-config.js
Created February 19, 2015 23:17
Passing config
// index.js
function myTask(args) {
}
// tasks/task.js
module.exports = function(grunt) {
grunt.registerMultiTask('my-task', function() {
var myTask = require('../');
var options = this.options();