visit
If the project grows it WILL become a problem! Imagine - you start getting users, making money, hiring devs, there are so many feature going on that you will not be able to keep all those dependencies in your head.
I worked in a place once which had a pretty big code project with no tests at all. The repo was massive, thousands of customers used it every day and they paid millions. But customers were paying and they wanted new features. Some were simple, like changing text and adding a button here and there. Some were more complex like providing integration with external software.One day we got our Product Owner (PO) who specified this new requirement. We needed a few new reports for customers, which were basically a couple of new tables. What started as a small task which we scoped around 4-weeks until production. Turned out to be a 4-month struggle with tens if not hundreds of bugs raised by QA.Why was that? It wasn’t the technology used, it wasn’t the developers ;) and it wasn’t the complexity of the project. It was the lack of tests!There were three of us working on those features, and due to no tests, we didn't know if our changes break someone else's code!Another problem was that those bugs were usually found after a couple of days of full manual regression testing. After a bug was raised we had to pick bug fixes as a priority and switch context. And then investigate, trying to remember why I wrote this rubbish code, fix it, create a PR, get it approved, and then release (obviously manually, because what did you expect from a no tests codebase).This massive gig made the business realize we cannot do it anymore and a decision has been made to rewrite the whole app! It took almost 18 months and obviously, no new features were added in the meantime.You get now why tests are important. You don't? Close the page, you'll never get it! Get back to adding functionality and stop wasting your precious time even reading about tests!
Our plan is the following:
"jest": {
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
}
},
npm i --save-dev husky
"husky": {
"hooks": {
"pre-commit": "npm run test:watch",
}
}
"test": "jest --coverage",
"test:watch": "npm test -- --watchAll"
Now if you run
npm run test:watch
an additional folder Coverage will appear in your root directory. It contains ‘lcov-report’ and this is where index.html sits. If you open it in the browser it won’t contain anything as we haven’t got any tests yet! So let's add some tests!const initialState = {
todos: [
{ id: 1, text: 'Do boring stuff', completed: false, color: 'purple' },
{ id: 2, text: 'Write tests!', completed: false, color: 'blue' }
],
filters: {
status: 'All',
colors: []
}
}
function nextTodoId(todos) {
const maxId = todos.reduce((maxId, todo) => Math.max(todo.id, maxId), -1)
return maxId + 1
}
export default function appReducer(state = initialState, action) {
switch (action.type) {
case 'todos/todoAdded':
{
return {
...state,
todos: [
...state.todos,
{
id: nextTodoId(state.todos),
text: action.payload,
completed: false
}
]
}
}
case 'todos/todoToggled':
{
return {
...state,
todos: state.todos.map(todo => {
if (todo.id !== action.payload) {
return todo
}
return {
...todo,
completed: !todo.completed
}
})
}
}
case 'filters/statusFilterChanged':
{
return {
// Copy the whole state
...state,
// Overwrite the filters value
filters: {
// copy the other filter fields
...state.filters,
// And replace the status field with the new value
status: action.payload
}
}
}
default:
return state
}
}
import appReducer from './reducer';
describe('appReducer', () => {
})
describe('appReducer', () => {
it('should return the initialState when no action has been used', () => {
expect(appReducer({}, {type: ''})).toEqual({});
})
})
Part two where we actually start using react testing library : How React Testing Library Can Improve Your Mental Health [Part 2] | Hacker Noon