SlideShare a Scribd company logo
1 of 36
Download to read offline
Testing in JavaScript
Marian Rusnak
WebElement Bardejov
31st August 2018
...or how to sleep well at night
console.log('About me');
● Developing web apps 7+ years
● JavaScript, PHP, Java, ASP.NET
● Oath, London (AOL+Yahoo)
● Alf Software, Presov
● Masaryk University, Brno
rusnak.marian1@gmail.com
@MarianRusnak
Are you confident
to deploy to production
on Friday at 5pm?
● Types of tests
● Tests for UI projects @ Oath
● Tests for ad delivery @ Oath
● Tests for web APIs @ Oath
● CI/CD
● Tools for testing in JavaScript
Agenda
Types of tests
● Manual
● Smoke
● Unit
● Functional/end-to-end
● Integration
● Performance
● Usability
● Acceptance Testing
● …
Manual
Unit
// math.js
class Math {
static add(a, b) {
return a + b;
}
}
// mathTest.js
test('2 + 2 should equal 4', () => {
expect(Math.add(2, 2)).to.equal(4);
});
Functional/e2e
Tests for UI projects @ Oath
Unit tests
● Controllers
● Business logic
describe('controllers/mraid', () => {
it('should render view with context passed', done => {
const request = {};
const response = {
render: (view, templateContext) => {
expect(templateContext.placementyType).to.equal('inline');
done();
}
};
mraidController.handle(request, response);
});
});
Functional/end-to-end tests
● Simulate user interaction
module.exports = {
'should be able to skip video': browser => {
browser
.url(`http://localhost:8001/myapp`)
.waitForElementVisible('#videoPlayer')
.click('#videoPlayBtn')
.click('#videoSkipBtn')
.assert.elementPresent('#videoReplayBtn')
.end();
}
};
Tests for ad delivery @ Oath
Ad delivery
Ad delivery
JSON ad config
+
JavaScript libraries
Unit tests
describe('render()', () => {
it('should render the ad', () => {
var advert = new Advert(adConfig);
advert.render();
expect(advert.rendered).to.be.true;
});
});
Functional/end-to-end tests
<!-- ad.html -->
<html>
<body>
<script src="AdAPI.js"></script>
<button onclick="$AD.click()">Buy me!</button>
</body>
</html> // test.js
module.exports = {
'Test click': browser => {
browser
.url(ad.html')
.waitForElementPresent('button')
.click('button')
.url(url => {
browser.assertEquals(url, 'http://advertiser.com');
})
.end();
}
};
Testing client-side code
// test.js
module.exports = {
'Test click': browser => {
browser
.url(ad.html')
.waitForElementPresent('button')
.click('button')
.url(url => {
browser.assertEquals(url, 'http://advertiser.com');
})
.end();
}
};
describe('render()', () => {
it('should render the ad', () => {
var advert = new Advert(adConfig);
advert.render();
expect(advert.rendered).to.be.true;
});
});
Tests web APIs @ Oath
Testing web APIs
SuperTest - https://www.npmjs.com/package/supertest
describe('GET /ads/{id}/events', () => {
it('should respond with events as JSON', done => {
request(server)
.get('/ads/123/events')
.expect(200)
.end((err, response) => {
expect(response.body).to.be.an('object').with.all.keys('render', 'view');
done(err);
});
});
});
Testing web APIs
nock - https://www.npmjs.com/package/nock
describe('3rd party integration', () => {
it('should make a request to 3rd party, done => {
const requestScope = nock('http://thirdparty.com')
.get(`/ads/abc123`)
.reply(200, '3rd party request successful');
thirdPartyAdapter.fetchAd({id: 'abc123'})
.then(() => {
expect(requestScope.isDone()).to.be.true;
done();
})
.catch(done);
});
});
Testing web APIs
+
Automated testing
1. Commit
2. Push
3. Tests are triggered
4. Tests are successful
5. Sweet dreams
CI/CD
Continuous Integration
● Develop, integrate, build, test
Continuous Delivery
● Develop, integrate, build, test, release
CI/CD tools
Tools for testing in JavaScript
Unit tests - test frameworks
describe('setName()', () => {
it('should set name property',() => {
...
});
});
test('adds 1 + 2 to equal 3', () => {
...
});
Unit tests - tools
var callback = sinon.stub();
callback.withArgs(42).returns(1);
callback.withArgs(1).throws("TypeError");
var callback = sinon.stub();
callback.onCall(0).returns(1);
callback.onCall(1).returns(2);
callback.returns(3);
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.lengthOf(3);
tea.should.have.property('flavors')
.with.lengthOf(3);
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.lengthOf(3);
expect(tea).to.have.property('flavors')
.with.lengthOf(3);
Unit tests - browsers/devices
Functional/end-to-end tests
Summary
● Tests @ Oath
● Unit & functional tests in JavaScript
● Test in real browsers!
● Test!!!
rusnak.marian1@gmail.com
@MarianRusnak
References
● http://stackoverflow.com/a/680713
● https://mochajs.org/
● https://jasmine.github.io/
● https://facebook.github.io/jest/
● https://github.com/avajs/ava
● http://sinonjs.org/
● http://chaijs.com/
● https://karma-runner.github.io
● http://nightwatchjs.org/
● http://www.protractortest.org/
● http://www.seleniumhq.org/
● https://www.browserstack.com/
● https://saucelabs.com/
● http://screwdriver.cd/

More Related Content

What's hot

What's hot (20)

Enhance react app with patterns - part 1: higher order component
Enhance react app with patterns - part 1: higher order componentEnhance react app with patterns - part 1: higher order component
Enhance react app with patterns - part 1: higher order component
 
Selenide Alternative in Practice - Implementation & Lessons learned [Selenium...
Selenide Alternative in Practice - Implementation & Lessons learned [Selenium...Selenide Alternative in Practice - Implementation & Lessons learned [Selenium...
Selenide Alternative in Practice - Implementation & Lessons learned [Selenium...
 
20160905 - BrisJS - nightwatch testing
20160905 - BrisJS - nightwatch testing20160905 - BrisJS - nightwatch testing
20160905 - BrisJS - nightwatch testing
 
Use React Patterns to Build Large Scalable App
Use React Patterns to Build Large Scalable App Use React Patterns to Build Large Scalable App
Use React Patterns to Build Large Scalable App
 
Lets make a better react form
Lets make a better react formLets make a better react form
Lets make a better react form
 
Test-Driven JavaScript Development (JavaZone 2010)
Test-Driven JavaScript Development (JavaZone 2010)Test-Driven JavaScript Development (JavaZone 2010)
Test-Driven JavaScript Development (JavaZone 2010)
 
軟體測試是在測試什麼?
軟體測試是在測試什麼?軟體測試是在測試什麼?
軟體測試是在測試什麼?
 
Web ui tests examples with selenide, nselene, selene & capybara
Web ui tests examples with  selenide, nselene, selene & capybaraWeb ui tests examples with  selenide, nselene, selene & capybara
Web ui tests examples with selenide, nselene, selene & capybara
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
 
Front-end Automated Testing
Front-end Automated TestingFront-end Automated Testing
Front-end Automated Testing
 
Testing React Applications
Testing React ApplicationsTesting React Applications
Testing React Applications
 
The Road to Native Web Components
The Road to Native Web ComponentsThe Road to Native Web Components
The Road to Native Web Components
 
Automated Testing in Angular Slides
Automated Testing in Angular SlidesAutomated Testing in Angular Slides
Automated Testing in Angular Slides
 
Nightwatch JS for End to End Tests
Nightwatch JS for End to End TestsNightwatch JS for End to End Tests
Nightwatch JS for End to End Tests
 
Protractor: Tips & Tricks
Protractor: Tips & TricksProtractor: Tips & Tricks
Protractor: Tips & Tricks
 
Сергей Больщиков "Protractor Tips & Tricks"
Сергей Больщиков "Protractor Tips & Tricks"Сергей Больщиков "Protractor Tips & Tricks"
Сергей Больщиков "Protractor Tips & Tricks"
 
Testing of React JS app
Testing of React JS appTesting of React JS app
Testing of React JS app
 
UI Testing Best Practices - An Expected Journey
UI Testing Best Practices - An Expected JourneyUI Testing Best Practices - An Expected Journey
UI Testing Best Practices - An Expected Journey
 
Insights on Protractor testing
Insights on Protractor testingInsights on Protractor testing
Insights on Protractor testing
 
Игорь Фесенко "Web Apps Performance & JavaScript Compilers"
Игорь Фесенко "Web Apps Performance & JavaScript Compilers"Игорь Фесенко "Web Apps Performance & JavaScript Compilers"
Игорь Фесенко "Web Apps Performance & JavaScript Compilers"
 

Similar to Testing in JavaScript - August 2018 - WebElement Bardejov

Test strategy for web development
Test strategy for web developmentTest strategy for web development
Test strategy for web development
alice yang
 
Compatibility Detector Tool of Chrome extensions
Compatibility Detector Tool of Chrome extensionsCompatibility Detector Tool of Chrome extensions
Compatibility Detector Tool of Chrome extensions
Kai Cui
 
Top100summit 谷歌-scott-improve your automated web application testing
Top100summit  谷歌-scott-improve your automated web application testingTop100summit  谷歌-scott-improve your automated web application testing
Top100summit 谷歌-scott-improve your automated web application testing
drewz lin
 

Similar to Testing in JavaScript - August 2018 - WebElement Bardejov (20)

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
 
Test strategy for web development
Test strategy for web developmentTest strategy for web development
Test strategy for web development
 
Good practices for debugging Selenium and Appium tests
Good practices for debugging Selenium and Appium testsGood practices for debugging Selenium and Appium tests
Good practices for debugging Selenium and Appium tests
 
DrupalCon Dublin 2016 - Automated browser testing with Nightwatch.js
DrupalCon Dublin 2016 - Automated browser testing with Nightwatch.jsDrupalCon Dublin 2016 - Automated browser testing with Nightwatch.js
DrupalCon Dublin 2016 - Automated browser testing with Nightwatch.js
 
Web ui testing
Web ui testingWeb ui testing
Web ui testing
 
jQuery Bay Area Conference 2010
jQuery Bay Area Conference 2010jQuery Bay Area Conference 2010
jQuery Bay Area Conference 2010
 
Puppeteer - A web scraping & UI Testing Tool
Puppeteer - A web scraping & UI Testing ToolPuppeteer - A web scraping & UI Testing Tool
Puppeteer - A web scraping & UI Testing Tool
 
Seven Peaks Speaks - Compose Screenshot Testing Made Easy
Seven Peaks Speaks - Compose Screenshot Testing Made EasySeven Peaks Speaks - Compose Screenshot Testing Made Easy
Seven Peaks Speaks - Compose Screenshot Testing Made Easy
 
Compatibility Detector Tool of Chrome extensions
Compatibility Detector Tool of Chrome extensionsCompatibility Detector Tool of Chrome extensions
Compatibility Detector Tool of Chrome extensions
 
Testing for fun in production Into The Box 2018
Testing for fun in production Into The Box 2018Testing for fun in production Into The Box 2018
Testing for fun in production Into The Box 2018
 
ForwardJS 2017 - Fullstack end-to-end Test Automation with node.js
ForwardJS 2017 -  Fullstack end-to-end Test Automation with node.jsForwardJS 2017 -  Fullstack end-to-end Test Automation with node.js
ForwardJS 2017 - Fullstack end-to-end Test Automation with node.js
 
Full Stack Unit Testing
Full Stack Unit TestingFull Stack Unit Testing
Full Stack Unit Testing
 
Selenium RC: Automated Testing of Modern Web Applications
Selenium RC: Automated Testing of Modern Web ApplicationsSelenium RC: Automated Testing of Modern Web Applications
Selenium RC: Automated Testing of Modern Web Applications
 
Pragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScriptPragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScript
 
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
 
WebTest - Efficient Functional Web Testing with HtmlUnit and Beyond
WebTest - Efficient Functional Web Testing with HtmlUnit and BeyondWebTest - Efficient Functional Web Testing with HtmlUnit and Beyond
WebTest - Efficient Functional Web Testing with HtmlUnit and Beyond
 
Testing in JavaScript
Testing in JavaScriptTesting in JavaScript
Testing in JavaScript
 
Top100summit 谷歌-scott-improve your automated web application testing
Top100summit  谷歌-scott-improve your automated web application testingTop100summit  谷歌-scott-improve your automated web application testing
Top100summit 谷歌-scott-improve your automated web application testing
 
Protractor framework architecture with example
Protractor framework architecture with exampleProtractor framework architecture with example
Protractor framework architecture with example
 
Javascript testing should be awesome
Javascript testing should be awesomeJavascript testing should be awesome
Javascript testing should be awesome
 

Recently uploaded

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Recently uploaded (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Testing in JavaScript - August 2018 - WebElement Bardejov

  • 1. Testing in JavaScript Marian Rusnak WebElement Bardejov 31st August 2018 ...or how to sleep well at night
  • 2. console.log('About me'); ● Developing web apps 7+ years ● JavaScript, PHP, Java, ASP.NET ● Oath, London (AOL+Yahoo) ● Alf Software, Presov ● Masaryk University, Brno rusnak.marian1@gmail.com @MarianRusnak
  • 3. Are you confident to deploy to production on Friday at 5pm?
  • 4. ● Types of tests ● Tests for UI projects @ Oath ● Tests for ad delivery @ Oath ● Tests for web APIs @ Oath ● CI/CD ● Tools for testing in JavaScript Agenda
  • 5. Types of tests ● Manual ● Smoke ● Unit ● Functional/end-to-end ● Integration ● Performance ● Usability ● Acceptance Testing ● …
  • 7. Unit // math.js class Math { static add(a, b) { return a + b; } } // mathTest.js test('2 + 2 should equal 4', () => { expect(Math.add(2, 2)).to.equal(4); });
  • 9. Tests for UI projects @ Oath
  • 10. Unit tests ● Controllers ● Business logic describe('controllers/mraid', () => { it('should render view with context passed', done => { const request = {}; const response = { render: (view, templateContext) => { expect(templateContext.placementyType).to.equal('inline'); done(); } }; mraidController.handle(request, response); }); });
  • 11. Functional/end-to-end tests ● Simulate user interaction module.exports = { 'should be able to skip video': browser => { browser .url(`http://localhost:8001/myapp`) .waitForElementVisible('#videoPlayer') .click('#videoPlayBtn') .click('#videoSkipBtn') .assert.elementPresent('#videoReplayBtn') .end(); } };
  • 12. Tests for ad delivery @ Oath
  • 13.
  • 15. Ad delivery JSON ad config + JavaScript libraries
  • 16. Unit tests describe('render()', () => { it('should render the ad', () => { var advert = new Advert(adConfig); advert.render(); expect(advert.rendered).to.be.true; }); });
  • 17. Functional/end-to-end tests <!-- ad.html --> <html> <body> <script src="AdAPI.js"></script> <button onclick="$AD.click()">Buy me!</button> </body> </html> // test.js module.exports = { 'Test click': browser => { browser .url(ad.html') .waitForElementPresent('button') .click('button') .url(url => { browser.assertEquals(url, 'http://advertiser.com'); }) .end(); } };
  • 18.
  • 19.
  • 20. Testing client-side code // test.js module.exports = { 'Test click': browser => { browser .url(ad.html') .waitForElementPresent('button') .click('button') .url(url => { browser.assertEquals(url, 'http://advertiser.com'); }) .end(); } }; describe('render()', () => { it('should render the ad', () => { var advert = new Advert(adConfig); advert.render(); expect(advert.rendered).to.be.true; }); });
  • 21. Tests web APIs @ Oath
  • 22. Testing web APIs SuperTest - https://www.npmjs.com/package/supertest describe('GET /ads/{id}/events', () => { it('should respond with events as JSON', done => { request(server) .get('/ads/123/events') .expect(200) .end((err, response) => { expect(response.body).to.be.an('object').with.all.keys('render', 'view'); done(err); }); }); });
  • 23. Testing web APIs nock - https://www.npmjs.com/package/nock describe('3rd party integration', () => { it('should make a request to 3rd party, done => { const requestScope = nock('http://thirdparty.com') .get(`/ads/abc123`) .reply(200, '3rd party request successful'); thirdPartyAdapter.fetchAd({id: 'abc123'}) .then(() => { expect(requestScope.isDone()).to.be.true; done(); }) .catch(done); }); });
  • 25. Automated testing 1. Commit 2. Push 3. Tests are triggered 4. Tests are successful 5. Sweet dreams
  • 26. CI/CD
  • 27. Continuous Integration ● Develop, integrate, build, test Continuous Delivery ● Develop, integrate, build, test, release
  • 28.
  • 30. Tools for testing in JavaScript
  • 31. Unit tests - test frameworks describe('setName()', () => { it('should set name property',() => { ... }); }); test('adds 1 + 2 to equal 3', () => { ... });
  • 32. Unit tests - tools var callback = sinon.stub(); callback.withArgs(42).returns(1); callback.withArgs(1).throws("TypeError"); var callback = sinon.stub(); callback.onCall(0).returns(1); callback.onCall(1).returns(2); callback.returns(3); foo.should.be.a('string'); foo.should.equal('bar'); foo.should.have.lengthOf(3); tea.should.have.property('flavors') .with.lengthOf(3); expect(foo).to.be.a('string'); expect(foo).to.equal('bar'); expect(foo).to.have.lengthOf(3); expect(tea).to.have.property('flavors') .with.lengthOf(3);
  • 33. Unit tests - browsers/devices
  • 35. Summary ● Tests @ Oath ● Unit & functional tests in JavaScript ● Test in real browsers! ● Test!!! rusnak.marian1@gmail.com @MarianRusnak
  • 36. References ● http://stackoverflow.com/a/680713 ● https://mochajs.org/ ● https://jasmine.github.io/ ● https://facebook.github.io/jest/ ● https://github.com/avajs/ava ● http://sinonjs.org/ ● http://chaijs.com/ ● https://karma-runner.github.io ● http://nightwatchjs.org/ ● http://www.protractortest.org/ ● http://www.seleniumhq.org/ ● https://www.browserstack.com/ ● https://saucelabs.com/ ● http://screwdriver.cd/