Test Driven Development(TDD) 0r Test First Development
Is when you start working on your code by writing automated tests before writing the code that is being tested.
Test-running systems available in JavaScript: Mocha, Jasmine, Tape and Jest to name a few. the syntax for each one is very similar.
In the end, writing tests is less about the syntax and more about the TDD philosophy. The most important issues are knowing why we write tests and what we test rather than how.
How does it work?
- Create the test and test it with the current code which is ging to fail because the new feature is not yet implemented
- Create code for that featureā¦test the code again using the created test
- After it passes the test we can refactor the code.
Why even do unit testing?
- Because complexitiy grows over time
Jest
Install jest using npm
npm install --save-dev jest
Generate a basic configuration file
jest --init
By default, the current version of Jest will not recognize ES6 import statements. To fix this, we need to install the Babel compiler and the preset-env plugin.
npm install --save-dev babel-jest @babel/core @babel/preset-env
Configure Babel to target your current version of Node by creating a babel.config.js
file in the root of your project:
babel.config.js
module.exports = {
presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
};