Workshop
Silvia Mur
Elena García
Why do we need tests?
Types of tests
What is TDD?
TDD life cycle
The anatomy of a test
Unit testing in JavaScript
Let's start coding!
How do I know that with my code I didn't break anything?
Oh, there is a bug, something is not working properly
Test driven development (TDD) is a software development approach in which a test is written before writing the code. Once the new code passes the test, it is refactored to an acceptable standard.
but... in the mid term is really beneficial
We do not have time to do TDD
We do not have time to do TDD
I have awesome tests because I do TDD
I have awesome tests because I do TDD
My test coverage is 100% because I do TDD
My test coverage is 100% because I do TDD
The architecture of my application is a result of doing TDD
The architecture of my application is a result of doing TDD
Write enough code to pass the test
describe('sum function', () => {
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2).toBe(3));
});
});
function sum () {
return 3;
}
describe('sum function', () => {
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2).toBe(3));
});
test('adds 2 + 2 to equal 4', () => {
expect(sum(2, 2).toBe(4));
});
});
function sum (a, b) {
return a + b;
}
1. Setup
2. Execution
3. Assert
0. Description
1. Setup
2. Execution
3. Assert
0. Description
1. Assert
2. Execution
3. Setup
It's used to create a sentence for the test
describe('Hermione casts the Alohomora spell', () => {
it('opens the door in front of her', () => {
...
})
})
It checks that the action was performed correctly
(check that either the return value of a function or the state of an object are as expected)
describe('Hermione casts the Alohomora spell', () => {
it('unlocks the door in front of her', () => {
// assert
expect(door.status).toBe('unlocked')
})
})
(Act)
It performs the action that we are testing on the unit
describe('Hermione casts the Alohomora spell', () => {
it('unlocks the door in front of her', () => {
// execute
Hermione.cast('Alohomora', door)
// assert
expect(door.status).toBe('unlocks')
})
})
(Arrange)
It's used to get everything ready to perform the test
(declaring variables, building required objects or setting the state based
on the circumstances we want to test)
describe('Hermione casts the Alohomora spell', () => {
it('unlocks the door in front of her', () => {
// setup
const door = { status: 'locked' }
// execute
Hermione.cast('Alohomora', door)
// assert
expect(door.status).toBe('unlocked')
})
})
Sinon.JS
Jasmine
Libraries
Test runners
Testing frameworks
Karma
Jest
QUnit
yarn add jest --dev
npm install jest --save-dev
jest --init
yarn test
npm test
function sum (a, b) {
return a + b;
}
describe('sum function', () => {
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2).toBe(3));
});
});
toBe
toBeNull
toBeUndefined
toBeTruthy
toBeFalsy
toEqual
toBeGreaterThan
toBeLessThan
toMatch
toMatchObject
toContain
not
We have two katas
We will do
ping-pong pair programming
Write failing test
Write code that makes it pass
Write failing test
Write code that makes it pass
REFACTOR