SlideShare a Scribd company logo
1 of 63
Download to read offline
Holger Grosse-Plankermann
Jest: Frontend Testing done right
Who am I?
Developer/Consultant/Whatever
Taming the Web since the 2000
Compiled Mozilla for bonus features
Backend vs. Frontend dev
Podcaster http://autoweird.fm
@holgergp
http://github.com/holgergp Holger Grosse-Plankermann
Show of hands
Do you test your frontend code?
•Unit Testing
•Selenium
•Macros
4
Show of hands
What test framework are you using?
•Jest
•Mocha
•Karma
5
•Jasmine
•Qunit
•Something else?
AGENDA
What is Jest
Why another library?
Unit Tests in JavaScript
Where do I come from?
Where are we heading?
Top 3 reasons for Jest
Number 3 will amaze you :)
7
Where do I come from
(Unit-) Testing JS
Testing and JS?
•Software development used to be
dominated by the needs of the backend
•The important stuff is in the backend!
Some enterprise architect
•(Frontend- ) JavaScript has not been a
first-class citizen for a long time.
•Nah! We don’t need to test that
The same enterprise architect
Why is that?
•Backend Java testing is well
established
•And tooling is quite nice tbh
•mvn clean install ftw
•JavaScript testing used to be a
mess
•Tons of config and dependencies
needed to be orchestrated
•And that rumor is still in our heads
Testing in the frontend is important
•More and more complex code than in the
backend
•Rapid feedback
•Safety net
•Design!
•Documentation
•Testing is a task many devs don’t like
•Don’t let the tools stand in the way!
•Help is on the way!
14
A library that puts the fun in testing
Enter Jest
Delightful JavaScript Testing
https://facebook.github.io/jest/
•Testing library/framework
•Comparable to
•JUnit (Java)
•Test::Unit (Ruby)
•PHPUnit (PHP)
What is Jest?
•Developed by Facebook
•Current version 22.4
•MIT licensed
•> 16.000 Github Stars
•Has been around since 2014
Jest: Facts and figures
•Comes from Facebook but it’s not
limited to React
•Works like a charm in my projects
•React based
•Vue based
•JQuery backed
Jest: Facts and figures
Easy Setup
You can start with
very little config
Awesome CLI
A really polished
product
All in the box
Little to no need for
third party libs
TOP 3 Reasons to use Jest now
You won’t believe number 2 :)
19
Simple to install and simple to use
Easy setup
Create-React-App
Probably the easiest way to get going is
to just use create-react-app.
repl.it
Use an online repl might be even easier.
npm install jest
Come on, let’s get our hands dirty!
Let’s get started
I want to write the first test!
Be amazed
Install dependencies $ npm install -D jest
Let’s get started
package.json
"scripts": {
"test": "jest",
"test:watch": "jest --watch"
}
Write a simple test
describe('Demo should', () => {
it('be green', () => {
expect(true).toBe(true);
});
});
Run it $> npm test
PASS ./demo.spec.js
Demo should
✓ be green (2ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.606s
Ran all test suites.
Optional: Install Jest globally
$ npm install -g jest
•Use Jest without configuring scripts
•I use it for convenience
Anatomy of a test
describe("add", () => {
beforeEach(() => {})
afterEach(() => {})
beforeAll(() => {})
afterAll(() => {})
it("should compute the sum", () => {
expect(add(1, 2)).toBe(3);
});
});
test("should compute the sum", () => {
expect(add(1, 2)).toBe(3);
})
This gives a nice descriptive scoped
structure of your test.
As a bonus, Jest can print the
results nicely,
I can recommend that structure.
A more traditional way of writing
your tests. Possible, but less
expressive than the describe style.
Where are my tests?
•Out of the box, Jest looks for
__tests__/*
bar.spec.js
foo.test.js
•Configurable
•Suited my needs well thus far
Works with ES6
•Add babel
{
"presets": ["env"]
}
•Jest picks it up automatically
•No need to configure
•Compile to JS friendly
•e.g. works (now) well with TypeScript
•Add a .babelrc
$ npm install -D babel-jest babel-core babel-preset-env
Configurable
•in your package.json
https://facebook.github.io/jest/docs/en/configuration.html
"jest": {
"verbose": true
}
•via JavaScript
// jest.config.js
module.exports = {
verbose: true,
};
•Or using CLI
Sandboxed and parallel
•Runs errors tests first
•Runs tests in parallel
•Tests run in a sandbox
•No conflicting
Runs in your CI Server
I work with it in:
•Travis
•Gitlab CI
•Jenkins
28
Fun to work with
Awesome CLI
PASS client/service/skippingAndForcing.spec.js
PASS client/service/customMatchers.spec.js
PASS client/service/asymetricExpectations.spec.js
PASS client/service/serviceUser.spec.js
PASS client/service/newExpectations.spec.js
PASS client/service/httpServiceWithPromises.spec.js
PASS client/components/FirstTest.spec.js
PASS client/components/TextComponent.spec.jsx
PASS client/components/App.spec.jsx (5.539s)
PASS client/components/ListComponent.spec.jsx (5.589s)
Test Suites: 10 passed, 10 total
Tests: 5 skipped, 23 passed, 28 total
Snapshots: 5 passed, 5 total
Time: 9.83s
Ran all test suites.
Test results
•Nice output out of the box
FAIL ./add.spec.js
● add › should compute the sum
expect(received).toBe(expected) // Object.is equality
Expected value to be:
4
Received:
3
8 |
9 | it('should compute the sum', () => {
> 10 | expect(add(1, 2)).toBe(4);
11 | });
12 | });
13 |
at Object.<anonymous> (add.spec.js:10:27)
PASS ./asymetricExpectations.spec.js
..
Test Suites: 1 failed, 8 passed, 9 total
Tests: 1 failed, 5 skipped, 15 passed, 21 total
Meaningful error reports
Watch mode
•Nice for TDD
Watch mode
•Nice for TDD
Watch mode
•Nice for TDD
Watch mode
•Nice for TDD
Watch mode
•Nice for TDD
Code coverage
$ jest --coverage
PASS client/service/newExpectations.spec.js
Ran all test suites.
-----------------------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
-----------------------------|----------|----------|----------|----------|----------------|
All files | 90.91 | 100 | 86.67 | 93.33 | |
components | 100 | 100 | 100 | 100 | |
App.jsx | 100 | 100 | 100 | 100 | |
ListComponent.jsx | 100 | 100 | 100 | 100 | |
TextComponent.jsx | 100 | 100 | 100 | 100 | |
service | 81.25 | 100 | 77.78 | 84.62 | |
httpService.js | 100 | 100 | 100 | 100 | |
httpServiceWithPromises.js | 100 | 100 | 100 | 100 | |
serviceUser.js | 62.5 | 100 | 50 | 66.67 | 9,10 |
-----------------------------|----------|----------|----------|----------|----------------|
•Computes coverage out of the box
$ jest --verbose
PASS client/components/App.spec.jsx
App
✓ renders (14ms)
✓ calling Service correctly (3ms)
PASS client/service/httpServiceWithPromises.spec.js
httpService getPosts should
✓ talk to the backend using done (1ms)
✓ talk to the backend using promise returns
✓ talk to the backend using async await (1ms)
httpService getPostsRejecting should
✓ reject using done
✓ reject using expectations
✓ reject using async await (1ms)
Test Suites: 10 passed, 10 total
Tests: 5 skipped, 23 passed, 28 total
Snapshots: 5 passed, 5 total
Time: 2.187s
Ran all test suites.
Even nicer output
•verbose option
There’s more
•—onlyChanged /-o
•runs test affected by files changed
•--lastCommit
•runs test affected by files changed in the last
commit
IDE Support
It just works
•I use it mainly in IntelliJ/Webstorm and it just works
•Use a Run Config like any other test
•In EAP even coverage is shown
•Works fine in VSCode
•Some more config
•Not as polished as IntelliJ
•But fine for me
•I use the Jest extension
•I often use it on the command line
•Alongside the IDE
•The watch feature shines there
Editor integration IntelliJ
3
Less need for 3rd party libs
Batteries included
Expectations
Mocking
Snapshot Tests
What’s in the box
Async Tests
DOM Testing
Code coverage
Expectations .toBe(value)
.toHaveBeenCalled()
.toBeCloseTo(number, numDigits)
.toBeDefined()
.toBeFalsy()
.toBeGreaterThan(number)
.toBeGreaterThanOrEqual(number)
.toBeLessThan(number)
.toBeLessThanOrEqual(number)
.toBeInstanceOf(Class)
.toBeNull()
.toBeTruthy()
.toBeUndefined()
.toContain(item)
.toContainEqual(item)
.toEqual(value)
.toHaveLength(number)
.toMatch(regexpOrString)
.toMatchObject(object)
…
•No need for separate Mocha/Jasmine/expect
•All the expectations you need
https://facebook.github.io/jest/docs/en/expect.html
Expectations: Examples
Expecting Objects
1 describe('createCustomer should’, () => {
2 it('produce a valid customer',() => {
3 const customer = {name: 'Peter', premium: true};
4 expect(customer).toBeDefined();
5 expect(customer.name).toEqual('Peter');
6 expect(customer.name).toEqual(expect.stringContaining('ete'));
7 expect(customer).toEqual(expect.objectContaining({premium: true}));
8 })
9 });
Expecting Arrays
describe('createCustomers should', () => {
it('work with many customer',() => {
const customers = [
{name: 'Peter', premium: true},
{name: 'Max', premium: false},
{name: 'Tine', premium: true}
];
expect(customers).toContainEqual({name: 'Tine', premium: true});
})
});
1
2
3
4
5
6
7
8
9
1
0
Useful shortcuts
fdescribe()/describe.only()
fit()/it.only()
ftest()/test.only()
Executes only this block (in the block)
Skips the block
xdescribe()/describe.skip()
xit()/it.skip()
xtest()/test.skip()
Matchers are extendable
expect.extend({
toHaveAtLeastTimesPremium(rec, argument) {
const pass = rec.filter(r=>r.premium).length >= argument;
if (pass) {
return {
message: () => `expected ${rec} to have less premium customers`,
pass: true,
};
} else {
return {
message: () => `expected ${rec} to have more premium customers`,
pass: false,
};
}
}});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
describe('createCustomers should', () => {
it('be testable with custom matcher',() => {
const customers = [
{name: ‚Peter', premium: true},
{name: 'Max', premium: false},
{name: 'Tine', premium: true}
];
expect(customers).toHaveAtLeastTimesPremium(2);
})
});
1
2
3
4
5
6
7
8
9
10
actual expect
Mocking
In a unit test it is often unfeasible to kickstart your whole dependency graph
Mocking lets you narrow down the scope of your test
{
}{
}
Mocking
•No need for separate Sinon.js
•Works similar to Mockito in Java
Mock Functions
Lets you replace more
complex logic with logic right
for your test
Manual Mocks
Lets you replace whole
modules for your test
Mock functions
•Mock calls
describe('bulkOrder should', () => {
it('order products', () => {
const product = {
order: jest.fn()
};
bulkOrder(product);
expect(product.order).toHaveBeenCalled();
expect(product.order).toHaveBeenCalledTimes(3);
expect(product.order).lastCalledWith(3);
expect(product.order).toHaveBeenCalledWith(2);
expect(product.order.mock.calls[1][0]).toBe(2);
});
});
1
2
3
4
5
6
7
8
9
10
11
12
13
export function bulkOrder(product) {
product.order(1);
product.order(2);
product.order(3);
}
1
2
3
4
5
Mock functions
•Mock returns
export function showProductStatus(product) {
const messagePrefix = 'Das Produkt';
return product.isAvailable()
? `${messagePrefix} ist verfügbar :)`
: `${messagePrefix} ist nicht verfügbar :(`;
}
1
2
3
4
5
6
describe('showProductStatus should', () =>
it('return an appropriate answer if product is available', () => {
const product = {
isAvailable: jest.fn().mockReturnValue(true)
};
const productStatus = showProductStatus(product);
expect(productStatus).toBe('Das Produkt ist verfügbar :)');
})
);
1
2
3
4
5
6
7
8
9
import { findAllCustomers } from "./customerService";
jest.mock("./httpService", () => {
const getCustomerMock = () => {
return {
customers: [{name: 'Walter'}, {name: 'Käthe'}]
};
};
return {
getCustomers: getCustomerMock
};
});
describe("customerService should", () => {
it("work with returned customers", () => {
const data = findAllCustomers();
expect(data).toEqual(["Walter", "Käthe"]);
});
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Mock a module
Snapshot tests by example
const validationMessage = (fieldname) => {
return `Feld '${fieldname}' ist ein Pflichtfeld.`
}
expect(validationMessage('Vorname')).toMatchSnapshot();
exports[`validationMesssage should should produce a valid message 1`] = `"Feld
'Vorname' ist ein Pflichtfeld."`;
const validationMessage = (fieldname) => {
return `Das Feld '${fieldname}' ist ein Pflichtfeld.`
}
expect(value).toMatchSnapshot()
Received value does not match stored snapshot 1.
- "Feld 'Vorname' ist ein Pflichtfeld."
+ "Das Feld 'Vorname' ist ein Pflichtfeld.“
› 1 snapshot test failed.
Code to be tested
Snapshot test
Snapshot
Result
Test that would require some effort to expect
React components (that should not change)
Things you would not test otherwise
So what is this useful for?
Snapshot test may be brittle
You NEED to review your snapshot changes
No jest -u fire and forget
Prefer unit tests
That being said
Besides snapshot tests being awesome
Are supported out of the box
const response = {
customers: [{..},{..},{..}]
};
1
2
3
it("talk to the backend using done", done => {
getCustomers().then(data => {
expect(data).toEqual(response);
done();
});
});
1
2
3
4
5
6
it("talk to the backend using promise returns", () => {
return getPosts().then(data => {
expect(data).toEqual(response);
});
});
1
2
3
4
5
it("talk to the backend using expectations", () => {
expect(getPosts()).resolves.toEqual(response);
});
1
2
3
it("talk to the backend using async await", async () => {
const posts = await getPosts();
expect(posts).toEqual(response);
});
1
2
3
4
I like the 

async/await Syntax
Async tests
DOM testing
•Jest comes with JSDOM to support testing DOM.
•JSDOM simulates a DOM-enviroment.
const setupDOM = () => {
document.body.innerHTML =
'<div>' +
' <div id="openDialog" >Click</div>' +
' <div id="dialog" />' +
'' +
'</div>';
initPage();
};
1
2
3
4
5
6
7
8
9
it('open on a click at the clickable element', () => {
document.querySelector('#openDialog').click();
expect(isDialogOpen()).toBe(true);
});
1
2
3
4
DOM testing (outlook)
Test the rendering of your React application:
Enzyme (http://airbnb.io/enzyme/)
Enzyme is a JavaScript Testing utility for React that makes it
easier to assert, manipulate, and traverse your React
Components' output.
Browser testing using Jest:
Puppeteer (https://github.com/GoogleChrome/puppeteer) and
its Jest integration (https://facebook.github.io/jest/docs/en/
puppeteer.html)
Puppeteer is a Node library which provides a high-level API to
control headless Chrome or Chromium over the DevTools
Protocol.
Expectations
Mocking
Snapshot Tests
What’s in the box
Async Tests
DOM Testing
Code Coverage
And more
53
Some closing words
Coming to an end
What we had to do
{
"presets": ["env"]
}
Install dependencies $ npm install -D jest
package.json
"scripts": {
"test": "jest",
"test:watch": "jest --watch"
}
https://github.com/holgergp/jestStarter
babel
//.babelrc
{
"presets": ["env"]
}
$ npm install -D babel-jest babel-core
babel-preset-env
Easy to use and setup
Powerful features set yet familiar
Polished product (even the CLI)
Try Jest yourself
Tryout Jest
https://facebook.github.io/jest/
START
TESTING
YOUR
JS
TODAY
Questions?
Picture Links
•https://unsplash.com/photos/FQjmQgSoRyQ
•https://unsplash.com/photos/gYxVSeZazXU
•https://unsplash.com/photos/h1v8lkfDUu4
•https://unsplash.com/photos/58tOB36ZTnw
•https://unsplash.com/photos/cEUl-0DSM9s
•https://unsplash.com/photos/9HI8UJMSdZA
•https://unsplash.com/photos/xhWMi9wdQaE
•https://unsplash.com/photos/-3wygakaeQc
•https://unsplash.com/photos/R6xx6fnvPT8
•https://unsplash.com/photos/Q6jX7BbPE38
•https://unsplash.com/photos/8xAA0f9yQnE
•https://unsplash.com/photos/0YbeoQOX89k
•https://unsplash.com/photos/RpDA3uYkJWM
•https://unsplash.com/photos/6dvxVmG5nUU
•https://unsplash.com/photos/D56TpVU8zng
•https://unsplash.com/photos/TyQ-0lPp6e4
•https://unsplash.com/photos/gdBXlLO53N4
Our mission – to promote agile development, innovation
and technology – extends through everything we do.
codecentric AG
Hochstraße 11
42697 Solingen
E-Mail: info@codecentric.de
www.codecentric.de
Telefon: +49 (0) 212. 23 36 28 0

Telefax: +49 (0) 212.23 36 28 79

Address
Contact Info
Telephone
Stay connected
Thanks for listening!

More Related Content

What's hot

Introduction To Web Application Testing
Introduction To Web Application TestingIntroduction To Web Application Testing
Introduction To Web Application TestingYnon Perek
 
Jellyfish, JSCONF 2011
Jellyfish, JSCONF 2011Jellyfish, JSCONF 2011
Jellyfish, JSCONF 2011Adam Christian
 
Acceptance Test-driven Development with Cucumber-jvm
Acceptance Test-driven Development with Cucumber-jvmAcceptance Test-driven Development with Cucumber-jvm
Acceptance Test-driven Development with Cucumber-jvmChristopher Bartling
 
AngularJS and Protractor
AngularJS and ProtractorAngularJS and Protractor
AngularJS and ProtractorFilipe Falcão
 
Protractor for angularJS
Protractor for angularJSProtractor for angularJS
Protractor for angularJSKrishna Kumar
 
Automated Web Testing using JavaScript
Automated Web Testing using JavaScriptAutomated Web Testing using JavaScript
Automated Web Testing using JavaScriptSimon Guest
 
TDD super mondays-june-2014
TDD super mondays-june-2014TDD super mondays-june-2014
TDD super mondays-june-2014Alex Kavanagh
 
Carmen Popoviciu - Protractor styleguide | Codemotion Milan 2015
Carmen Popoviciu - Protractor styleguide | Codemotion Milan 2015Carmen Popoviciu - Protractor styleguide | Codemotion Milan 2015
Carmen Popoviciu - Protractor styleguide | Codemotion Milan 2015Codemotion
 
Front-end Automated Testing
Front-end Automated TestingFront-end Automated Testing
Front-end Automated TestingRuben Teijeiro
 
Ruby onrails cucumber-rspec-capybara
Ruby onrails cucumber-rspec-capybaraRuby onrails cucumber-rspec-capybara
Ruby onrails cucumber-rspec-capybaraBindesh Vijayan
 
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016Gavin Pickin
 
Continuous Integration Testing in Django
Continuous Integration Testing in DjangoContinuous Integration Testing in Django
Continuous Integration Testing in DjangoKevin Harvey
 
Node.js Development Workflow Automation with Grunt.js
Node.js Development Workflow Automation with Grunt.jsNode.js Development Workflow Automation with Grunt.js
Node.js Development Workflow Automation with Grunt.jskiyanwang
 
CommandBox & ForgeBox Package Management
CommandBox & ForgeBox Package ManagementCommandBox & ForgeBox Package Management
CommandBox & ForgeBox Package ManagementOrtus Solutions, Corp
 
How To Use Selenium Successfully
How To Use Selenium SuccessfullyHow To Use Selenium Successfully
How To Use Selenium SuccessfullyDave Haeffner
 
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017Ortus Solutions, Corp
 

What's hot (20)

Introduction To Web Application Testing
Introduction To Web Application TestingIntroduction To Web Application Testing
Introduction To Web Application Testing
 
Protractor training
Protractor trainingProtractor training
Protractor training
 
Jellyfish, JSCONF 2011
Jellyfish, JSCONF 2011Jellyfish, JSCONF 2011
Jellyfish, JSCONF 2011
 
Acceptance Test-driven Development with Cucumber-jvm
Acceptance Test-driven Development with Cucumber-jvmAcceptance Test-driven Development with Cucumber-jvm
Acceptance Test-driven Development with Cucumber-jvm
 
AngularJS and Protractor
AngularJS and ProtractorAngularJS and Protractor
AngularJS and Protractor
 
Protractor for angularJS
Protractor for angularJSProtractor for angularJS
Protractor for angularJS
 
Automated Web Testing using JavaScript
Automated Web Testing using JavaScriptAutomated Web Testing using JavaScript
Automated Web Testing using JavaScript
 
TDD super mondays-june-2014
TDD super mondays-june-2014TDD super mondays-june-2014
TDD super mondays-june-2014
 
Carmen Popoviciu - Protractor styleguide | Codemotion Milan 2015
Carmen Popoviciu - Protractor styleguide | Codemotion Milan 2015Carmen Popoviciu - Protractor styleguide | Codemotion Milan 2015
Carmen Popoviciu - Protractor styleguide | Codemotion Milan 2015
 
Front-end Automated Testing
Front-end Automated TestingFront-end Automated Testing
Front-end Automated Testing
 
Protractor overview
Protractor overviewProtractor overview
Protractor overview
 
Protractor survival guide
Protractor survival guideProtractor survival guide
Protractor survival guide
 
Ruby onrails cucumber-rspec-capybara
Ruby onrails cucumber-rspec-capybaraRuby onrails cucumber-rspec-capybara
Ruby onrails cucumber-rspec-capybara
 
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
 
Continuous Integration Testing in Django
Continuous Integration Testing in DjangoContinuous Integration Testing in Django
Continuous Integration Testing in Django
 
Automate Thyself
Automate ThyselfAutomate Thyself
Automate Thyself
 
Node.js Development Workflow Automation with Grunt.js
Node.js Development Workflow Automation with Grunt.jsNode.js Development Workflow Automation with Grunt.js
Node.js Development Workflow Automation with Grunt.js
 
CommandBox & ForgeBox Package Management
CommandBox & ForgeBox Package ManagementCommandBox & ForgeBox Package Management
CommandBox & ForgeBox Package Management
 
How To Use Selenium Successfully
How To Use Selenium SuccessfullyHow To Use Selenium Successfully
How To Use Selenium Successfully
 
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
 

Similar to Jest: Frontend Testing richtig gemacht @WebworkerNRW

Java script unit testing
Java script unit testingJava script unit testing
Java script unit testingMats Bryntse
 
The Many Ways to Test Your React App
The Many Ways to Test Your React AppThe Many Ways to Test Your React App
The Many Ways to Test Your React AppAll Things Open
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchMats Bryntse
 
Hotfixing iOS apps with Javascript
Hotfixing iOS apps with JavascriptHotfixing iOS apps with Javascript
Hotfixing iOS apps with JavascriptSergio Padrino Recio
 
Browser Automated Testing Frameworks - Nightwatch.js
Browser Automated Testing Frameworks - Nightwatch.jsBrowser Automated Testing Frameworks - Nightwatch.js
Browser Automated Testing Frameworks - Nightwatch.jsLuís Bastião Silva
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!Ortus Solutions, Corp
 
Quick start with AngularJS
Quick start with AngularJSQuick start with AngularJS
Quick start with AngularJSIuliia Baranova
 
Ansible top 10 - 2018
Ansible top 10 -  2018Ansible top 10 -  2018
Ansible top 10 - 2018Viresh Doshi
 
Unit Testing in R with Testthat - HRUG
Unit Testing in R with Testthat - HRUGUnit Testing in R with Testthat - HRUG
Unit Testing in R with Testthat - HRUGegoodwintx
 
Enterprise Strength Mobile JavaScript
Enterprise Strength Mobile JavaScriptEnterprise Strength Mobile JavaScript
Enterprise Strength Mobile JavaScriptTroy Miles
 
How to use selenium successfully
How to use selenium successfullyHow to use selenium successfully
How to use selenium successfullyTEST Huddle
 
Automated Acceptance Testing from Scratch
Automated Acceptance Testing from ScratchAutomated Acceptance Testing from Scratch
Automated Acceptance Testing from ScratchExcella
 
Quick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmineQuick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmineGil Fink
 
Developers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonDevelopers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonIneke Scheffers
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFXHendrik Ebbers
 
How To Use Selenium Successfully (Java Edition)
How To Use Selenium Successfully (Java Edition)How To Use Selenium Successfully (Java Edition)
How To Use Selenium Successfully (Java Edition)Dave Haeffner
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)Thierry Gayet
 
Selenium withnet
Selenium withnetSelenium withnet
Selenium withnetVlad Maniak
 

Similar to Jest: Frontend Testing richtig gemacht @WebworkerNRW (20)

Java script unit testing
Java script unit testingJava script unit testing
Java script unit testing
 
The Many Ways to Test Your React App
The Many Ways to Test Your React AppThe Many Ways to Test Your React App
The Many Ways to Test Your React App
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
 
Hotfixing iOS apps with Javascript
Hotfixing iOS apps with JavascriptHotfixing iOS apps with Javascript
Hotfixing iOS apps with Javascript
 
Browser Automated Testing Frameworks - Nightwatch.js
Browser Automated Testing Frameworks - Nightwatch.jsBrowser Automated Testing Frameworks - Nightwatch.js
Browser Automated Testing Frameworks - Nightwatch.js
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
 
Quick start with AngularJS
Quick start with AngularJSQuick start with AngularJS
Quick start with AngularJS
 
Ansible top 10 - 2018
Ansible top 10 -  2018Ansible top 10 -  2018
Ansible top 10 - 2018
 
Unit Testing in R with Testthat - HRUG
Unit Testing in R with Testthat - HRUGUnit Testing in R with Testthat - HRUG
Unit Testing in R with Testthat - HRUG
 
Tdd
TddTdd
Tdd
 
Enterprise Strength Mobile JavaScript
Enterprise Strength Mobile JavaScriptEnterprise Strength Mobile JavaScript
Enterprise Strength Mobile JavaScript
 
Continuous feature-development
Continuous feature-developmentContinuous feature-development
Continuous feature-development
 
How to use selenium successfully
How to use selenium successfullyHow to use selenium successfully
How to use selenium successfully
 
Automated Acceptance Testing from Scratch
Automated Acceptance Testing from ScratchAutomated Acceptance Testing from Scratch
Automated Acceptance Testing from Scratch
 
Quick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmineQuick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmine
 
Developers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonDevelopers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomon
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFX
 
How To Use Selenium Successfully (Java Edition)
How To Use Selenium Successfully (Java Edition)How To Use Selenium Successfully (Java Edition)
How To Use Selenium Successfully (Java Edition)
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)
 
Selenium withnet
Selenium withnetSelenium withnet
Selenium withnet
 

Recently uploaded

Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 

Recently uploaded (20)

Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 

Jest: Frontend Testing richtig gemacht @WebworkerNRW

  • 2. Who am I? Developer/Consultant/Whatever Taming the Web since the 2000 Compiled Mozilla for bonus features Backend vs. Frontend dev Podcaster http://autoweird.fm @holgergp http://github.com/holgergp Holger Grosse-Plankermann
  • 3. Show of hands Do you test your frontend code? •Unit Testing •Selenium •Macros 4
  • 4. Show of hands What test framework are you using? •Jest •Mocha •Karma 5 •Jasmine •Qunit •Something else?
  • 5. AGENDA What is Jest Why another library? Unit Tests in JavaScript Where do I come from? Where are we heading? Top 3 reasons for Jest Number 3 will amaze you :)
  • 6. 7 Where do I come from (Unit-) Testing JS
  • 7. Testing and JS? •Software development used to be dominated by the needs of the backend •The important stuff is in the backend! Some enterprise architect •(Frontend- ) JavaScript has not been a first-class citizen for a long time. •Nah! We don’t need to test that The same enterprise architect
  • 8. Why is that? •Backend Java testing is well established •And tooling is quite nice tbh •mvn clean install ftw •JavaScript testing used to be a mess •Tons of config and dependencies needed to be orchestrated •And that rumor is still in our heads
  • 9. Testing in the frontend is important •More and more complex code than in the backend •Rapid feedback •Safety net •Design! •Documentation •Testing is a task many devs don’t like •Don’t let the tools stand in the way! •Help is on the way!
  • 10. 14 A library that puts the fun in testing Enter Jest
  • 11. Delightful JavaScript Testing https://facebook.github.io/jest/ •Testing library/framework •Comparable to •JUnit (Java) •Test::Unit (Ruby) •PHPUnit (PHP) What is Jest?
  • 12. •Developed by Facebook •Current version 22.4 •MIT licensed •> 16.000 Github Stars •Has been around since 2014 Jest: Facts and figures
  • 13. •Comes from Facebook but it’s not limited to React •Works like a charm in my projects •React based •Vue based •JQuery backed Jest: Facts and figures
  • 14. Easy Setup You can start with very little config Awesome CLI A really polished product All in the box Little to no need for third party libs TOP 3 Reasons to use Jest now You won’t believe number 2 :)
  • 15. 19 Simple to install and simple to use Easy setup
  • 16. Create-React-App Probably the easiest way to get going is to just use create-react-app. repl.it Use an online repl might be even easier. npm install jest Come on, let’s get our hands dirty! Let’s get started I want to write the first test!
  • 17. Be amazed Install dependencies $ npm install -D jest Let’s get started package.json "scripts": { "test": "jest", "test:watch": "jest --watch" } Write a simple test describe('Demo should', () => { it('be green', () => { expect(true).toBe(true); }); }); Run it $> npm test PASS ./demo.spec.js Demo should ✓ be green (2ms) Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: 1.606s Ran all test suites.
  • 18. Optional: Install Jest globally $ npm install -g jest •Use Jest without configuring scripts •I use it for convenience
  • 19. Anatomy of a test describe("add", () => { beforeEach(() => {}) afterEach(() => {}) beforeAll(() => {}) afterAll(() => {}) it("should compute the sum", () => { expect(add(1, 2)).toBe(3); }); }); test("should compute the sum", () => { expect(add(1, 2)).toBe(3); }) This gives a nice descriptive scoped structure of your test. As a bonus, Jest can print the results nicely, I can recommend that structure. A more traditional way of writing your tests. Possible, but less expressive than the describe style.
  • 20. Where are my tests? •Out of the box, Jest looks for __tests__/* bar.spec.js foo.test.js •Configurable •Suited my needs well thus far
  • 21. Works with ES6 •Add babel { "presets": ["env"] } •Jest picks it up automatically •No need to configure •Compile to JS friendly •e.g. works (now) well with TypeScript •Add a .babelrc $ npm install -D babel-jest babel-core babel-preset-env
  • 22. Configurable •in your package.json https://facebook.github.io/jest/docs/en/configuration.html "jest": { "verbose": true } •via JavaScript // jest.config.js module.exports = { verbose: true, }; •Or using CLI
  • 23. Sandboxed and parallel •Runs errors tests first •Runs tests in parallel •Tests run in a sandbox •No conflicting
  • 24. Runs in your CI Server I work with it in: •Travis •Gitlab CI •Jenkins
  • 25. 28 Fun to work with Awesome CLI
  • 26. PASS client/service/skippingAndForcing.spec.js PASS client/service/customMatchers.spec.js PASS client/service/asymetricExpectations.spec.js PASS client/service/serviceUser.spec.js PASS client/service/newExpectations.spec.js PASS client/service/httpServiceWithPromises.spec.js PASS client/components/FirstTest.spec.js PASS client/components/TextComponent.spec.jsx PASS client/components/App.spec.jsx (5.539s) PASS client/components/ListComponent.spec.jsx (5.589s) Test Suites: 10 passed, 10 total Tests: 5 skipped, 23 passed, 28 total Snapshots: 5 passed, 5 total Time: 9.83s Ran all test suites. Test results •Nice output out of the box
  • 27. FAIL ./add.spec.js ● add › should compute the sum expect(received).toBe(expected) // Object.is equality Expected value to be: 4 Received: 3 8 | 9 | it('should compute the sum', () => { > 10 | expect(add(1, 2)).toBe(4); 11 | }); 12 | }); 13 | at Object.<anonymous> (add.spec.js:10:27) PASS ./asymetricExpectations.spec.js .. Test Suites: 1 failed, 8 passed, 9 total Tests: 1 failed, 5 skipped, 15 passed, 21 total Meaningful error reports
  • 33. Code coverage $ jest --coverage PASS client/service/newExpectations.spec.js Ran all test suites. -----------------------------|----------|----------|----------|----------|----------------| File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines | -----------------------------|----------|----------|----------|----------|----------------| All files | 90.91 | 100 | 86.67 | 93.33 | | components | 100 | 100 | 100 | 100 | | App.jsx | 100 | 100 | 100 | 100 | | ListComponent.jsx | 100 | 100 | 100 | 100 | | TextComponent.jsx | 100 | 100 | 100 | 100 | | service | 81.25 | 100 | 77.78 | 84.62 | | httpService.js | 100 | 100 | 100 | 100 | | httpServiceWithPromises.js | 100 | 100 | 100 | 100 | | serviceUser.js | 62.5 | 100 | 50 | 66.67 | 9,10 | -----------------------------|----------|----------|----------|----------|----------------| •Computes coverage out of the box
  • 34. $ jest --verbose PASS client/components/App.spec.jsx App ✓ renders (14ms) ✓ calling Service correctly (3ms) PASS client/service/httpServiceWithPromises.spec.js httpService getPosts should ✓ talk to the backend using done (1ms) ✓ talk to the backend using promise returns ✓ talk to the backend using async await (1ms) httpService getPostsRejecting should ✓ reject using done ✓ reject using expectations ✓ reject using async await (1ms) Test Suites: 10 passed, 10 total Tests: 5 skipped, 23 passed, 28 total Snapshots: 5 passed, 5 total Time: 2.187s Ran all test suites. Even nicer output •verbose option
  • 35. There’s more •—onlyChanged /-o •runs test affected by files changed •--lastCommit •runs test affected by files changed in the last commit
  • 36. IDE Support It just works •I use it mainly in IntelliJ/Webstorm and it just works •Use a Run Config like any other test •In EAP even coverage is shown •Works fine in VSCode •Some more config •Not as polished as IntelliJ •But fine for me •I use the Jest extension •I often use it on the command line •Alongside the IDE •The watch feature shines there
  • 38. 3 Less need for 3rd party libs Batteries included
  • 39. Expectations Mocking Snapshot Tests What’s in the box Async Tests DOM Testing Code coverage
  • 41. Expectations: Examples Expecting Objects 1 describe('createCustomer should’, () => { 2 it('produce a valid customer',() => { 3 const customer = {name: 'Peter', premium: true}; 4 expect(customer).toBeDefined(); 5 expect(customer.name).toEqual('Peter'); 6 expect(customer.name).toEqual(expect.stringContaining('ete')); 7 expect(customer).toEqual(expect.objectContaining({premium: true})); 8 }) 9 }); Expecting Arrays describe('createCustomers should', () => { it('work with many customer',() => { const customers = [ {name: 'Peter', premium: true}, {name: 'Max', premium: false}, {name: 'Tine', premium: true} ]; expect(customers).toContainEqual({name: 'Tine', premium: true}); }) }); 1 2 3 4 5 6 7 8 9 1 0
  • 42. Useful shortcuts fdescribe()/describe.only() fit()/it.only() ftest()/test.only() Executes only this block (in the block) Skips the block xdescribe()/describe.skip() xit()/it.skip() xtest()/test.skip()
  • 43. Matchers are extendable expect.extend({ toHaveAtLeastTimesPremium(rec, argument) { const pass = rec.filter(r=>r.premium).length >= argument; if (pass) { return { message: () => `expected ${rec} to have less premium customers`, pass: true, }; } else { return { message: () => `expected ${rec} to have more premium customers`, pass: false, }; } }}); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 describe('createCustomers should', () => { it('be testable with custom matcher',() => { const customers = [ {name: ‚Peter', premium: true}, {name: 'Max', premium: false}, {name: 'Tine', premium: true} ]; expect(customers).toHaveAtLeastTimesPremium(2); }) }); 1 2 3 4 5 6 7 8 9 10 actual expect
  • 44. Mocking In a unit test it is often unfeasible to kickstart your whole dependency graph Mocking lets you narrow down the scope of your test { }{ }
  • 45. Mocking •No need for separate Sinon.js •Works similar to Mockito in Java Mock Functions Lets you replace more complex logic with logic right for your test Manual Mocks Lets you replace whole modules for your test
  • 46. Mock functions •Mock calls describe('bulkOrder should', () => { it('order products', () => { const product = { order: jest.fn() }; bulkOrder(product); expect(product.order).toHaveBeenCalled(); expect(product.order).toHaveBeenCalledTimes(3); expect(product.order).lastCalledWith(3); expect(product.order).toHaveBeenCalledWith(2); expect(product.order.mock.calls[1][0]).toBe(2); }); }); 1 2 3 4 5 6 7 8 9 10 11 12 13 export function bulkOrder(product) { product.order(1); product.order(2); product.order(3); } 1 2 3 4 5
  • 47. Mock functions •Mock returns export function showProductStatus(product) { const messagePrefix = 'Das Produkt'; return product.isAvailable() ? `${messagePrefix} ist verfügbar :)` : `${messagePrefix} ist nicht verfügbar :(`; } 1 2 3 4 5 6 describe('showProductStatus should', () => it('return an appropriate answer if product is available', () => { const product = { isAvailable: jest.fn().mockReturnValue(true) }; const productStatus = showProductStatus(product); expect(productStatus).toBe('Das Produkt ist verfügbar :)'); }) ); 1 2 3 4 5 6 7 8 9
  • 48. import { findAllCustomers } from "./customerService"; jest.mock("./httpService", () => { const getCustomerMock = () => { return { customers: [{name: 'Walter'}, {name: 'Käthe'}] }; }; return { getCustomers: getCustomerMock }; }); describe("customerService should", () => { it("work with returned customers", () => { const data = findAllCustomers(); expect(data).toEqual(["Walter", "Käthe"]); }); }); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Mock a module
  • 49. Snapshot tests by example const validationMessage = (fieldname) => { return `Feld '${fieldname}' ist ein Pflichtfeld.` } expect(validationMessage('Vorname')).toMatchSnapshot(); exports[`validationMesssage should should produce a valid message 1`] = `"Feld 'Vorname' ist ein Pflichtfeld."`; const validationMessage = (fieldname) => { return `Das Feld '${fieldname}' ist ein Pflichtfeld.` } expect(value).toMatchSnapshot() Received value does not match stored snapshot 1. - "Feld 'Vorname' ist ein Pflichtfeld." + "Das Feld 'Vorname' ist ein Pflichtfeld.“ › 1 snapshot test failed. Code to be tested Snapshot test Snapshot Result
  • 50. Test that would require some effort to expect React components (that should not change) Things you would not test otherwise So what is this useful for?
  • 51. Snapshot test may be brittle You NEED to review your snapshot changes No jest -u fire and forget Prefer unit tests That being said Besides snapshot tests being awesome
  • 52. Are supported out of the box const response = { customers: [{..},{..},{..}] }; 1 2 3 it("talk to the backend using done", done => { getCustomers().then(data => { expect(data).toEqual(response); done(); }); }); 1 2 3 4 5 6 it("talk to the backend using promise returns", () => { return getPosts().then(data => { expect(data).toEqual(response); }); }); 1 2 3 4 5 it("talk to the backend using expectations", () => { expect(getPosts()).resolves.toEqual(response); }); 1 2 3 it("talk to the backend using async await", async () => { const posts = await getPosts(); expect(posts).toEqual(response); }); 1 2 3 4 I like the 
 async/await Syntax Async tests
  • 53. DOM testing •Jest comes with JSDOM to support testing DOM. •JSDOM simulates a DOM-enviroment. const setupDOM = () => { document.body.innerHTML = '<div>' + ' <div id="openDialog" >Click</div>' + ' <div id="dialog" />' + '' + '</div>'; initPage(); }; 1 2 3 4 5 6 7 8 9 it('open on a click at the clickable element', () => { document.querySelector('#openDialog').click(); expect(isDialogOpen()).toBe(true); }); 1 2 3 4
  • 54. DOM testing (outlook) Test the rendering of your React application: Enzyme (http://airbnb.io/enzyme/) Enzyme is a JavaScript Testing utility for React that makes it easier to assert, manipulate, and traverse your React Components' output. Browser testing using Jest: Puppeteer (https://github.com/GoogleChrome/puppeteer) and its Jest integration (https://facebook.github.io/jest/docs/en/ puppeteer.html) Puppeteer is a Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol.
  • 55. Expectations Mocking Snapshot Tests What’s in the box Async Tests DOM Testing Code Coverage And more
  • 57. What we had to do { "presets": ["env"] } Install dependencies $ npm install -D jest package.json "scripts": { "test": "jest", "test:watch": "jest --watch" } https://github.com/holgergp/jestStarter babel //.babelrc { "presets": ["env"] } $ npm install -D babel-jest babel-core babel-preset-env
  • 58. Easy to use and setup Powerful features set yet familiar Polished product (even the CLI) Try Jest yourself
  • 60.
  • 63. Our mission – to promote agile development, innovation and technology – extends through everything we do. codecentric AG Hochstraße 11 42697 Solingen E-Mail: info@codecentric.de www.codecentric.de Telefon: +49 (0) 212. 23 36 28 0
 Telefax: +49 (0) 212.23 36 28 79
 Address Contact Info Telephone Stay connected Thanks for listening!