Kent C. Dodds
Utah
1 wife, 4 kids
PayPal, Inc.
@kentcdodds
if you are able
(more than famous)
Enhanced Workflows
It dependsโข
๐ก
if your test does something that the consumer of your code doesn't then it's testing implementation details
๐ก
๐ก
if a refactor breaks your tests, then it's testing implementation details
๐ก
function sum(a, b) {
return a + b
}
test('sum adds numbers', () => {
expect(sum(1, 3)).toBe(4)
})
let api, server
beforeAll(async () => {
server = await startServer()
const {port} = server.address()
api = axios.create({
baseURL: `http://localhost:${port}/api`
})
})
afterAll(() => server.close())
beforeEach(() => resetDb())
test('can register a user', async () => {
const registerData = {username: 'bob', password: 'wiley'}
const testUser = await api
.post('auth/register', registerData)
.then(response => response.data.user)
expect(testUser.username).toBe(registerData.username)
const readUserUnauthenticated = await api
.get(`users/${testUser.id}`)
.then(response => response.data.user)
expect(readUserUnauthenticated).toEqual(testUser)
})
describe('authentication', () => {
it('should allow users to register', () => {
const user = {username: 'bob', password: 'wiley'}
cy
.visitApp()
.getByTestId('register-link')
.click()
.getByTestId('username-input')
.type(user.username)
.getByTestId('password-input')
.type(user.password)
.getByTestId('login-submit')
.click()
cy
.url()
.should('equal', 'http://localhost:3000/')
cy
.getByTestId('username-display')
.should('contain', user.username)
})
})
๏ฟ heap
๐ฐ๐ค๐ฐ
๐๐จ
๐ข
Simple problems ๐
Big problems ๐
๐ kcd.im/write-tests-feedbackย ๐
resources on the next slide ๐