42
loading...
This website collects cookies to deliver better user experience
multiply
, seguindo o TDD, que foi explicado em detalhes no primeiro artigo. Ela deve receber dois valores e retornar a multiplicação deles.checkout
na branch exercise-03
. Em seguida, crie a pasta utils
dentro de src
e dentro dela crie o arquivo math.test.js
, com as seguintes linhas:test.todo('multiply: returns the multiplication of two numbers')
test.todo('multiply: throws an error when some of the arguments is NaN')
npm test -- --watch
para que o Jest fique observando os arquivos que forem modificados.todo
permite anotar os testes que queremos escrever no futuro, nesse caso, vamos criar dois testes.todo
do primeiro teste e adicione o seguinte código:test('multiply: returns the multiplication of two numbers', () => {
expect(multiply(1000, 5.26)).toBe(5260)
})
math.js
dentro de utils
, apenas com o necessário para que o teste passe.export function multiply() {
return 5260
}
expect
.import { multiply } from './math'
test('multiply: returns the multiplication of two numbers', () => {
expect(multiply(1000, 5.26)).toBe(5260)
expect(multiply(2, 6.24)).toBe(12.48)
})
export function multiply(a, b) {
return a * b
}
string
.test('multiply: returns the multiplication of two numbers', () => {
expect(multiply(1000, 5.26)).toBe(5260)
expect(multiply(2, 6.24)).toBe(12.48)
expect(multiply(15, '7.29')).toBe(109.35)
expect(multiply('3', 5)).toBe(15)
expect(multiply('5', '5')).toBe(25)
})
multiply
com funciona com diferentes valores.math.test.js
.test('multiply: throws an error when some of the arguments is NaN', () => {
expect(() => multiply('some invalid value', 'another invalid value')).toThrowError('Arguments must be numbers')
})
multiply
está dentro de outra função, isso é necessário porque ela vai disparar um erro, nesses casos, se não fizermos dessa forma, o teste não passa.math.js
:export function multiply(a, b) {
if (isNaN(a) || isNaN(b)) {
throw new Error('Arguments must be numbers')
}
return a * b
}
✓ multiply: returns the multiplication of two numbers (5 ms)
✓ multiply: throws an error when some of the arguments is NaN (8 ms)
describe
, ela recebe uma descrição e uma função onde ficam os testes. Faça as seguintes alterações no arquivo math.test.js
:describe('multiply: returns the multiplication', () => {
test('of two numbers', () => {
expect(multiply(1000, 5.26)).toBe(5260)
expect(multiply(2, 6.24)).toBe(12.48)
expect(multiply(15, '7.29')).toBe(109.35)
expect(multiply('3', 5)).toBe(15)
expect(multiply('5', '5')).toBe(25)
})
})
multiply: returns the multiplication
✓ of two numbers (4 ms)
returns the multiplication of two numbers
, a diferença é que podemos criar um teste para cada caso e saber exatamente como a função está sendo testada.describe('multiply: returns the multiplication', () => {
test('of two numbers', () => {
expect(multiply(1000, 5.26)).toBe(5260)
})
test('of others two numbers', () => {
expect(multiply(2, 6.24)).toBe(12.48)
})
test('of a number and a string', () => {
expect(multiply(15, '7.29')).toBe(109.35)
})
...
})
describe('multiply: throws an error when', () => {
test('arguments are texts', () => {
expect(() => multiply('some invalid value', 'another invalid value')).toThrowError('Arguments must be numbers')
})
})
each
que permite repetir o mesmo teste com valores diferentes. O código deve ficar assim:describe('multiply: returns the multiplication', () => {
const cases = [
['of two numbers', 1000, 5.26, 5260],
['of others two numbers', 2, 6.24, 12.48],
['of a number and a string', 15, '7.29', 109.35],
['of a string and a number', '3', 5, 15],
['of two strings', '5', '5', 25]
]
test.each(cases)('%s', (_, a, b, expected) => {
expect(multiply(a, b)).toBe(expected)
})
})
describe('multiply: throws an error when', () => {
const cases = [
[
'arguments are texts',
'some invalid value',
'another invalid value',
'Arguments must be numbers'
]
]
test.each(cases)('%s', (_, a, b, expected) => {
expect(() => multiply(a, b)).toThrowError(expected)
})
})
Array
de Arrays
com os argumentos que são passados para o teste. Para mais detalhes sobre o método each
, você pode consultar a documentação.