21
React Testing

Testing React Applications

Embed Size (px)

Citation preview

Page 1: Testing React Applications

React Testing

Page 2: Testing React Applications

ES6 = ES2015

Page 3: Testing React Applications

Agenda

1. Static code analysis

2. Jest

Page 4: Testing React Applications

Simon Bächler

Web developer at Feinheit

Page 5: Testing React Applications

Testing

What tool do you use to test your JavaScript?

Page 6: Testing React Applications

http://ashleynolan.co.uk/blog/frontend-tooling-survey-2015-results

Page 7: Testing React Applications

Static code analysis

Style checking and linting

Pluggable linting rules

Supports ES2015

Supports JSX

Autofix option ESLint

Page 8: Testing React Applications

var foo = bar;

Page 9: Testing React Applications
Page 10: Testing React Applications

eslint —init

Page 11: Testing React Applications

Test Framework

Built on top of Jasmine

Developed by Facebook

Supports React out of the box

Mock by default

fake DOM

Page 12: Testing React Applications

0.12

No support for Node.js 0.12

Jest >= 0.5.2 supports Node 4

Page 13: Testing React Applications

Behavior drivenjest.dontMock(‚../sum');

describe('sum', function() { it('adds 1 + 2 to equal 3', function() { var sum = require(‚../sum'); expect(sum(1, 2)).toBe(3); });});

Page 14: Testing React Applications

Component under test

Mock object JSON fixture

StateDependency

Page 15: Testing React Applications

Mocks

Dependency

(Default Mock)

dependency.foo(bar)

undefinedComponent under test

Jest

expect(dependency.foo).toBeCalled()

expect(dependency.foo).lastCalledWith(bar)

expect(dependency.foo).not.toBeCalledWith(baz)

doSomething(bar)

Page 16: Testing React Applications

Let’s look at some code

Page 17: Testing React Applications

Jest runs in Node.js• No window object, no LocalStorage• No debugging (there is a workaround)• 'npm test'• node-debug --nodejs --harmony

node_modules/.bin/jest --runInBand

Page 18: Testing React Applications

More Jest functionality

• Test asynchronous calls• Spy on non-mocked objects and methods• Timer mocks• Regex matcher• Partial matchers• Setup and teardown methods

Page 19: Testing React Applications

System TestsSelenium Web Driver

Remote control a Browser

Supports Chrome, Firefox, IE, Edge and PhantomJS

Also iOs and Android (with Appium)

But this is another talk.

Page 20: Testing React Applications

–Jacob Kaplan-Moss

„Code without tests is broken by design.“

Page 21: Testing React Applications

END OF PART I