Last active
January 18, 2020 09:19
-
-
Save romainl/3e88f29e98604aab88567b01bc07e287 to your computer and use it in GitHub Desktop.
What would be the simplest cross-environment JavaScript testing framework?
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
/* | |
Pros: - intuitive syntax | |
- portable | |
Cons: - always exits with 0, non-blocking | |
- legibility/alignment issues | |
*/ | |
const testedFunction = (param) => { | |
return param * 2; | |
}; | |
// One test, one assertion | |
console.group('Default export'); | |
console.log('Should multiply by two:\t', true && | |
testedFunction(3) === 6 | |
); | |
console.groupEnd(); | |
/* Output: | |
Default export | |
Should multiply by two: true | |
*/ | |
// One test, several assertions | |
console.group('Default export'); | |
console.log('Should multiply by two:\t', true && | |
testedFunction(45) === 90 | |
); | |
console.log('Should handle floats:\t', true && | |
testedFunction(0.0004) === 0.0008 | |
); | |
console.groupEnd(); | |
/* Output: | |
Default export | |
Should multiply by two: true | |
Should handle floats: true | |
*/ | |
// Several tests | |
console.group('Test suite 1'); | |
console.group('Default export'); | |
console.log('Should multiply by two:\t', true && | |
testedFunction(12) === 14 | |
); | |
console.group('Silly example'); | |
console.log('Should be true:\t', true && | |
typeof 'foo' === 'string' | |
); | |
console.groupEnd(); | |
/* Output: | |
Test suite 1 | |
Default export | |
Should multiply by two: false | |
Silly example | |
Should be true: true | |
*/ |
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
/* | |
Pros: - output is very legible | |
- exits with non-zero if a test fails | |
Cons: - convoluted syntax, may require snippets | |
*/ | |
const testedFunction = (param) => { | |
return param * 2; | |
}; | |
// One test, one assertion | |
console.group('Default export'); | |
let t = 'Should multiply by two'; console.assert( | |
testedFunction(3) === 6 | |
, t) || console.log('✓\t', t); | |
console.groupEnd(); | |
/* Output: | |
Default export | |
✓ Should multiply by two | |
*/ | |
// One test, several assertions | |
console.group('Default export'); | |
t = 'Should multiply by two'; | |
console.assert( | |
testedFunction(45) === 90 | |
, t) || console.log('✓\t', t); | |
t = 'Should handle floats'; | |
console.assert( | |
testedFunction(0.0004) === 0.0008 | |
, t) || console.log('✓\t', t); | |
console.groupEnd(); | |
/* Output: | |
Default export | |
✓ Should multiply by two | |
✓ Should handle floats | |
*/ | |
// Several tests | |
console.group('Test suite 1'); | |
console.group('Default export'); | |
t = 'Should multiply by two'; | |
console.assert( | |
testedFunction(12) === 14 | |
, t) || console.log('✓\t', t); | |
console.groupEnd(); | |
/* Output: | |
Test suite 1 | |
Default export | |
assert.js:42 | |
throw new errors.AssertionError({ | |
^ | |
AssertionError [ERR_ASSERTION]: Should multiply by two | |
[...] | |
shell returned 1 | |
*/ |
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
/* | |
Pros: - output is very legible | |
Cons: - convoluted syntax, may require snippets | |
- doesn't exit with non-zero if a test fails | |
*/ | |
const testedFunction = (param) => { | |
return param * 2; | |
}; | |
// One test, one assertion | |
console.group('Default export'); | |
console.log(( | |
testedFunction(3) === 6 | |
)?'✓':'✗','\t'+'Should multiply by two'); | |
console.groupEnd(); | |
/* Output: | |
Default export | |
✓ Should multiply by two | |
*/ | |
// One test, several assertions | |
console.group('Default export'); | |
console.log(( | |
testedFunction(45) === 90 | |
)?'✓':'✗','\t'+'Should multiply by two'); | |
console.log(( | |
testedFunction(0.0004) === 0.0008 | |
)?'✓':'✗','\t'+'Should handle floats'); | |
console.groupEnd(); | |
/* Output: | |
Default export | |
✓ Should multiply by two | |
✓ Should handle floats | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment