#Day95 of #100daysofcode
Today I learned about Testing in JavaScript. Also, I learned how to implement my own testing framework by following this JavaScript testing guide by Kent C Dodds:
Here’s my implementation of my very basic not-at-all-production-ready testing framework:
const {sum, subtract} = require('./math')
test('sum adds numbers', () => {
const result = sum(3, 7)
const expected = 10
expect(result).toBe(expected)
})
test('subtract subtracts numbers', () => {
const result = subtract(7, 3)
const expected = 4
expect(result).toBe(expected)
})
function test(title, callback) {
try {
callback()
console.log(`✓ ${title}`)
} catch (error) {
console.error(`✕ ${title}`)
console.error(error)
}
}
function expect(actual) {
return {
toBe(expected) {
if (actual !== expected) {
throw new Error(`${actual} is not equal to ${expected}`)
}
},
}
}
Testing tools like Jest provide expect & test functions out of the box along with some really productive CLI tooling.