Joe Karlsson
progress. tech. cats. coffee. art. food. photog. mpls.
Builder of mechanical keyboards
Creator of bechdel.io
The analysis of computer software that is performed without actually executing programs
Using Flow and TypeScript could have prevented about 15% of the bugs that end up in committed code on Github
A unit test is a way of testing a unit - the smallest piece of code that can be logically isolated in a system
const multiply = (a, b) => {
return a * b;
}
it('should return the product of two numbers', () => {
const result = multiply(2, 3);
const expectedProduct = 6;
expect(result).toBe(expectedProduct);
});
Verifies that a piece of code works the same way as it did when the snapshot was created
const Button = () => {
return (
<div className="Button">
About
</div>
);
};
describe('Button', () => {
it('match the snapshot', () => {
const component = renderer.create(
<Button />,
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
});
Any Changes cause a failure immediately
is when individual software units are combined and tested as a group
import { mount } from 'enzyme';
const Button = (props) => {
const { alertNumber } = props;
return (
<button
className="Button-Bad"
onClick={alertNumber(multiply(2, 3))}
>
Multiply
</button>
);
};
describe('Button', () => {
it('should run the multiply function on click', () => {
const mockAlertNumber = jest.fn();
const component = mount(<Button alertNumber={mockAlertNumber} />);
component.find('button').simulate('click');
expect(mockAlertNumber).toHaveBeenCalledTimes(1);
});
});
Performed from start to finish under "Real World"
scenarios
describe('Button', () => {
it('should render the product of 3 * 2 on the DOM when clicked', () => {
browser.url('http://localhost:3001/');
const btn = $('.Button');
btn.click();
const newNum = $('.newNum');
expect(newNum.getText()).to.be.equal('6');
});
});
Launches Web Browser and runs tests
describe('React Meetup Developer', () => {
it('should learn how to test React applications', () => {
const dev = new Developer();
dev.attendMeetup();
dev.sitThroughBoringTalk();
dev.practiceTDDAtHome();
expect(dev.learned(TDD)).toBe(true);
});
});
Clean Code: A Handbook of Agile Software Craftsmanship
Uncle Bob
5 Common Misconceptions About TDD & Unit Tests
Eric Elliot
But these dudes are
By Joe Karlsson
A slide deck by Joe Karlsson https://github.com/JoeKarlsson/testing-react-apps