Readings: Node Ecosystem Readings: Test Driven Development - mwilkin-401-advanced-javascript/bend-javascript-401d2 GitHub Wiki
What is Test Driven Development? It is a method of code development that requires one to first write tests before writing any application code. The basic steps of which are 1. Red: write a test and make sure it fails. 2. Green: write the simplest, easiest possible code to make the test pass. 3. Refactor: optimise the code making sure the test will still pass.
An example of this could be: The test describe('Arithmetic Module', () => { it('should add two numbers', () =>{ let x = randomNumber; let y = randomNumber; let num1 = x + y;
var num2 = arithmetic.add(x, y);
expect(num2).toEqual(num1);
});
the code arithmetic.add = function(a,b) { if( typeof a !== "number" || typeof b !== "number" ) { return null; } return a+b; };
While the test covers only one small aspect code written, it's a start. Some further test could be done to ensure that null is returned if improper inputs are given, such as undefined, an empty array, an empty object, a string, two strings... The list goes on. Some important takeaways are that writing good tests that offer good coverage of the code are an investment in the code's longevity, stability and maintainability. It provides a good base line of documentation for the code and allows for future improvements and integration of addition and/or different programmers and makes the code easier to debug. Most importantly, perhaps, it forces you to fully consider and understand the code you are writing and allows for better, more informative code reviews.