Share
Embed
Transcript
92 slides
-
© 2017 - 2024 Siam Chamnankit Company Limited. All rights
reserved. JavaScript Testing 0-7 (Every day) -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 2 -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 3 -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 4 JavaScript Testing 0-7 (Every day) -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 5 Start with Question -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 6 https://web.facebook.com/groups/ThaiPGAssociateSociety/posts/2487756734768914 -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 7 -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 8 We Love Bugs -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 9 Cost of Bugs -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 10 -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 11 JavaScript Testing -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 12 Testing ? -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 13 Ice-Cream Cone https://alisterscott.github.io/TestingPyramids.html -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 14 Testing Pyramid https://alisterscott.github.io/TestingPyramids.html -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 15 Cupcake https://www.thoughtworks.com/insights/blog/introducing-software-testing-cupcake-anti-pattern -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 16 Testing Trophy https://www.testingjavascript.com/ -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 17 Testing === Confidence level -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 18 Testing === Activity Testing !== Phase -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 19 Testing Fast feedback -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 20 Fast Isolated Repeatable Self-validate -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 21 Test-First vs Test-Last ? -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 22 ทำ ก่ อนแ ล้ ว ค่ อย คิ ด test คิ ด test ก่ อน แ ล้ ว ค่ อย ท -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 23 https://github.com/goldbergyoni/javascript-testing-best-practices -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 24 http://www.growing-object-oriented-software.com/ What to do ? -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 25 http://www.growing-object-oriented-software.com/ How to do ? -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 26 Iterative and Incremental Process -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 27 Work break down -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 28 Iterative and incremental process Time Feature 1 -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 29 Iterative and incremental process Time Feature 1 Done = coded and tested -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 30 Iterative and incremental process Time Feature 1 Feature 1 Feature 2 Done = coded and tested -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 31 Iterative and incremental process Time Feature 1 Feature 1 Feature 2 Feature 1 Feature 2 Feature 3 Done = coded and tested -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 32 Iterative and incremental process Time Feature 1 Feature 1 Feature 2 Feature 1 Feature 2 Feature 3 Feature 1 Feature 2 Feature 3 Feature 4 Done = coded and tested -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 33 Iterative and incremental process Time Feature 1 Feature 1 Feature 2 Feature 1 Feature 2 Feature 3 Feature 1 Feature 2 Feature 3 Feature 4 Done = coded and tested -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 34 Current state of Testing ? -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 35 https://wingman-sw.com/ -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 36 https://wingman-sw.com/ -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 37 We need Automation !! -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 38 Iterative and incremental process Time Feature 1 Feature 1 Feature 2 Feature 1 Feature 2 Feature 3 Feature 1 Feature 2 Feature 3 Feature 4 Done = coded and tested Automation done Automation done Automation done Automation done -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 39 Continuous Testing -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 40 JavaScript Testing -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 41 https://github.com/goldbergyoni/javascript-testing-best-practices -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 42 Frontend Backend Full stack JavaScript Developer !! -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 43 Frontend Backend HTTPs REST gRPC tRPC … -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 44 Frontend Backend HTTPs REST gRPC tRPC … DB External System -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 45 What and Why to Test ? -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 46 Frontend Backend HTTPs REST gRPC tRPC … End-to-End Testing -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 47 How to test ? 1. Setup external system 2. Setup database 3. Deploy Backend 4. Deploy Frontend 5. Testing Process -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 48 End-to-End Testing High level of confidence Fast Isolated Repeatable Self-validate -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 49 Flaky Test !! -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 50 Need more stable ??? -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 51 Need more speed and feedback ??? -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 52 Frontend Backend HTTPs REST gRPC tRPC … Isolated !! -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 53 Frontend testing Backend testing -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 54 Frontend HTTPs REST gRPC tRPC … Frontend Testing Mock Backend -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 55 Frontend Frontend Testing Mock Backend Mock Service Worker Axios mock adapter -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 56 Example Web Browser testing with Playwright const { chromium } = require('playwright'); let browser; let page; beforeAll(async () => { browser = await chromium.launch(); }); afterAll(async () => { await browser.close(); }); afterEach(async () => { await page.close(); }); -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 57 Example Mocking API with Playwright beforeEach(async () => { page = await browser.newPage(); // Mock the API response await page.route('**/api/login', route => route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ success: true }), })); }); -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 58 Example Hard code !! it('should login successfully', async () => { await page.goto('http://localhost:3000/login'); await page.fill('#username', 'testuser'); await page.fill('#password', 'testpass'); await page.click('#login-button'); // Check for success message await page.waitForSelector('#success-message'); }); -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 59 Separate data test from test case module.exports = { url: 'http://localhost:3000/login', username: 'testuser', password: 'testpass', usernameSelector: '#username', passwordSelector: '#password', loginButtonSelector: '#login-button', successMessageSelector: '#success-message' }; const testData = require('./testData'); it('should login successfully', async () => { await page.goto(testData.url); await page.fill(testData.usernameSelector, testData.username); await page.fill(testData.passwordSelector, testData.password); await page.click(testData.loginButtonSelector); // Check for success message await page.waitForSelector(testData.successMessageSelector); }); Data test Test case -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 60 Snapshot testing with Playwright it('should login successfully', async () => { … // Take a screenshot await page.screenshot({ path: 'screenshot.png' }); // Check for success message expect(screenshot).toMatchImageSnapshot({ // allow for up to 1% difference failureThreshold: 0.01, // use percent-based comparison failureThresholdType: 'percent', // set a custom threshold for pixel matching customDiffConfig: { threshold: 0.1 }, // use a top-to-bottom comparison instead of left-to-right diffDirection: 'vertical', }); }); -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 61 Frontend Testing with Mock High level of confidence !! Fast Isolated Repeatable Self-validate -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 62 Backend Backend Testing Mock Database Mock External system -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 63 Backend Backend Testing Mock Database Mock External system newman SuperTest Mock Service Worker Axios mock adapter -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 64 Example SuperTest and Nock const request = require('supertest'); const nock = require('nock'); const expect = require('chai').expect; const app = require('../app'); // your express app -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 65 Example Mocking external system with Nock describe('API Tests', function() { beforeEach(function() { nock('https://api.yourdomain.com') .get('/endpoint') .reply(200, { message: 'success' }); }); it('tests our API', function(done) { request(app) .get('/api/endpoint') .end(function(err, res) { expect(res.body.message).to.be.equal('success'); done(); }); }); -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 66 Example API testing with SuperTest describe('API Tests', function() { beforeEach(function() { nock('https://api.yourdomain.com') .get('/endpoint') .reply(200, { message: 'success' }); }); it('tests our API', function(done) { request(app) .get('/api/endpoint') .end(function(err, res) { expect(res.body.message).to.be.equal('success'); done(); }); }); -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 67 Backend Testing with Mock High level of confidence !! Fast Isolated Repeatable Self-validate -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 68 Frontend Backend Isolated Testing -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 69 Isolated Testing High level of confidence !! Fast Isolated Repeatable Self-validate -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 70 Mock Bubble !! -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 71 Ensure new releases Don’t break the APIs -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 72 Frontend Backend HTTPs REST gRPC tRPC … Contract Testing -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 73 PACT Contract Testing https://docs.pact.io/ -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 74 Frontend HTTPs Example Backend POST /login { "user": "admin", "pass": "admin" } Response = 200 (Success) { "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" } Response = 404 (Data not found) { "message": "Data not found" } API specification -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 75 Create Contract with PACT const { Pact, Matchers } = require('@pact-foundation/pact'); const { expect } = require('chai'); const axios = require('axios'); const { like } = Matchers; const provider = new Pact({ consumer: 'Frontend', provider: 'Backend', port: 1234, }); describe('Pact with Provider', () => { before(() => provider.setup()); after(() => provider.finalize()); }); Setup contract test -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 76 Create Contract with PACT describe('when a request to login is made', () => { before(() => { return provider.addInteraction({ uponReceiving: 'a request to login', withRequest: { method: 'POST', path: '/login', body: { user: 'somkiat', pass: '1234', }, }, willRespondWith: { status: 200, body: { message: like('success'), token: like('jwt token'), }, }, Define contract by case (success) -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 77 Create Contract with PACT it('will receive the correct response', async () => { const response = await axios.post('http://localhost:1234/login', { user: 'somkiat', pass: '1234', }); expect(response.status).to.eql(200); expect(response.data).to.have.property('message', 'success'); expect(response.data).to.have.property('token'); }); Write test case + mock api Output :: contract file (YAML) -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 78 Contract file { "consumer": { "name": "Consumer" }, "provider": { "name": "Provider" }, "metadata": { "pactSpecification": { "version": "3.0.0" } } } "interactions": [ { "description": "a request to login", "request": { "method": "POST", "path": "/login", "body": { "user": "somkiat", "pass": "1234" }, "headers": { "Content-Type": "application/json" } }, "response": { "status": 200, "body": { "message": "success", "token": "jwt token" }, "headers": { "Content-Type": "application/json" } } } ] -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 79 Upload contract file to Broker const { Publisher } = require('@pact-foundation/pact-node'); const path = require('path'); const opts = { pactFilesOrDirs: [path.resolve(process.cwd(), 'pacts')], pactBroker: 'http://localhost:8000', // replace with the Pact Broker URL consumerVersion: '1.0.0', // replace with the consumer version }; new Publisher(opts).publishPacts() .then(() => console.log('Pact contract publishing complete!')) .catch(e => console.error('Pact contract publishing failed: ', e)); -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 80 PACT Broker https://docs.pact.io/pact_broker -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 81 PACT Broker https://docs.pact.io/pact_broker -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 82 Verify all contract from provider const { Verifier } = require('@pact-foundation/pact'); const path = require('path'); const opts = { provider: 'Provider', providerBaseUrl: 'http://localhost:8080', // replace with the provider base URL pactBrokerUrl: 'http://localhost:8000', // replace with the Pact Broker URL publishVerificationResult: true, providerVersion: '1.0.0', // replace with the provider version }; describe('Pact Verification', () => { it('should validate the expectations of Matching Service', () => { return new Verifier(opts).verifyProvider().then(output => { console.log('Pact Verification Complete!') console.log(output) }); }); }); -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 83 Contract Testing High level of confidence !! Fast Isolated Repeatable Self-validate -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 84 Testing === Confidence level -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 85 Flaky Test !! -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 86 Need more testing ? -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 87 Snapshot testing Unit testing Component testing Property- based testing …. -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 88 Continuous Testing === More Confidence -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 89 Back to Basic Why to test What to test When/How to test -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 90 Learn from your mistake -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 91 Bug-Driven Development -
JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited.
All rights reserved. 92 Q/A