It's hard to write unit tests that are worth the costs our employers pay for them. I'll share two mostly-made-up diagrams then use them to justify writing code as data transformation.
There are four types of code:
**Diagram 1: Types of Code**
// Test for Architecture 2 | |
assert.deepEqual( | |
buildRequest(x, y, z), | |
expectedRequest | |
) | |
assert.deepEqual( | |
parseResponse(response), | |
expectedParsedResponse |
// Test for Architecture 1 | |
fetch = spy().andMockWith(function (args) { | |
// pattern matching and pretend games | |
}) | |
assertSpyStuff | |
assertMoreSpyStuff | |
assertMoreSpyStuff |
**Diagram 2: Should I unit test?**
high
^ ||
^ ||
^ don't test || maybe test
^ ||
cost of testing ^ ================================
^ ||
**Diagram 1: Types of Code**
high
^ ||
^ ||
^ boring I/O || interesting I/O
^ ||
cost of testing ^ ================================
^ ||
// Architecture 2 | |
function callApi(x, y, z) { | |
const request = buildRequest(x, y, z) | |
return fetch(request) | |
.then(assertStatus('callApi', x, y, z)) | |
.then(parseResponse) |
// Architecture 1 | |
function callApi(x, y, z) { | |
manipulate params | |
manipulate params some more ... | |
return fetch(...) | |
.then(result => { | |
if (result.status === ...) { | |
handle status stuff | |
} |
#!/bin/bash | |
# written with @felkerch | |
REPO=$1 | |
VERSION=$2 | |
cd ../$REPO | |
git fetch | |
git checkout v$VERSION | |
npm link | |
cd - | |
npm link $REPO |
#!/bin/bash | |
REPO=$1 | |
VERSION=$2 | |
cd ../$REPO | |
git fetch | |
git checkout v$VERSION | |
npm link | |
cd - | |
npm link $REPO |
<style lang="sass"> | |
</style> | |
<template> | |
<input | |
class="input-text" | |
@input="update" | |
:placeholder="args.placeholder" | |
:value="value" |