Slide 1

Slide 1 text

© 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. JavaScript Testing 0-7 (Every day)

Slide 2

Slide 2 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 2

Slide 3

Slide 3 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 3

Slide 4

Slide 4 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 4 JavaScript Testing 0-7 (Every day)

Slide 5

Slide 5 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 5 Start with Question

Slide 6

Slide 6 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 6 https://web.facebook.com/groups/ThaiPGAssociateSociety/posts/2487756734768914

Slide 7

Slide 7 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 7

Slide 8

Slide 8 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 8 We Love Bugs

Slide 9

Slide 9 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 9 Cost of Bugs

Slide 10

Slide 10 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 10

Slide 11

Slide 11 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 11 JavaScript Testing

Slide 12

Slide 12 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 12 Testing ?

Slide 13

Slide 13 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 13 Ice-Cream Cone https://alisterscott.github.io/TestingPyramids.html

Slide 14

Slide 14 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 14 Testing Pyramid https://alisterscott.github.io/TestingPyramids.html

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 16 Testing Trophy https://www.testingjavascript.com/

Slide 17

Slide 17 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 17 Testing === Confidence level

Slide 18

Slide 18 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 18 Testing === Activity Testing !== Phase

Slide 19

Slide 19 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 19 Testing Fast feedback

Slide 20

Slide 20 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 20 Fast Isolated Repeatable Self-validate

Slide 21

Slide 21 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 21 Test-First vs Test-Last ?

Slide 22

Slide 22 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 22 ทำ ก่ อนแ ล้ ว ค่ อย คิ ด test คิ ด test ก่ อน แ ล้ ว ค่ อย ท

Slide 23

Slide 23 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 23 https://github.com/goldbergyoni/javascript-testing-best-practices

Slide 24

Slide 24 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 24 http://www.growing-object-oriented-software.com/ What to do ?

Slide 25

Slide 25 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 25 http://www.growing-object-oriented-software.com/ How to do ?

Slide 26

Slide 26 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 26 Iterative and Incremental Process

Slide 27

Slide 27 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 27 Work break down

Slide 28

Slide 28 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 28 Iterative and incremental process Time Feature 1

Slide 29

Slide 29 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 29 Iterative and incremental process Time Feature 1 Done = coded and tested

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 34 Current state of Testing ?

Slide 35

Slide 35 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 35 https://wingman-sw.com/

Slide 36

Slide 36 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 36 https://wingman-sw.com/

Slide 37

Slide 37 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 37 We need Automation !!

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 39 Continuous Testing

Slide 40

Slide 40 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 40 JavaScript Testing

Slide 41

Slide 41 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 41 https://github.com/goldbergyoni/javascript-testing-best-practices

Slide 42

Slide 42 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 42 Frontend Backend Full stack JavaScript Developer !!

Slide 43

Slide 43 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 43 Frontend Backend HTTPs REST gRPC tRPC …

Slide 44

Slide 44 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 44 Frontend Backend HTTPs REST gRPC tRPC … DB External System

Slide 45

Slide 45 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 45 What and Why to Test ?

Slide 46

Slide 46 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 46 Frontend Backend HTTPs REST gRPC tRPC … End-to-End Testing

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 49 Flaky Test !!

Slide 50

Slide 50 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 50 Need more stable ???

Slide 51

Slide 51 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 51 Need more speed and feedback ???

Slide 52

Slide 52 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 52 Frontend Backend HTTPs REST gRPC tRPC … Isolated !!

Slide 53

Slide 53 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 53 Frontend testing Backend testing

Slide 54

Slide 54 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 54 Frontend HTTPs REST gRPC tRPC … Frontend Testing Mock Backend

Slide 55

Slide 55 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 55 Frontend Frontend Testing Mock Backend Mock Service Worker Axios mock adapter

Slide 56

Slide 56 text

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(); });

Slide 57

Slide 57 text

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 }), })); });

Slide 58

Slide 58 text

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'); });

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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', }); });

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 62 Backend Backend Testing Mock Database Mock External system

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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(); }); });

Slide 66

Slide 66 text

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(); }); });

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 68 Frontend Backend Isolated Testing

Slide 69

Slide 69 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 69 Isolated Testing High level of confidence !! Fast Isolated Repeatable Self-validate

Slide 70

Slide 70 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 70 Mock Bubble !!

Slide 71

Slide 71 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 71 Ensure new releases Don’t break the APIs

Slide 72

Slide 72 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 72 Frontend Backend HTTPs REST gRPC tRPC … Contract Testing

Slide 73

Slide 73 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 73 PACT Contract Testing https://docs.pact.io/

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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)

Slide 77

Slide 77 text

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)

Slide 78

Slide 78 text

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" } } } ]

Slide 79

Slide 79 text

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));

Slide 80

Slide 80 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 80 PACT Broker https://docs.pact.io/pact_broker

Slide 81

Slide 81 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 81 PACT Broker https://docs.pact.io/pact_broker

Slide 82

Slide 82 text

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) }); }); });

Slide 83

Slide 83 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 83 Contract Testing High level of confidence !! Fast Isolated Repeatable Self-validate

Slide 84

Slide 84 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 84 Testing === Confidence level

Slide 85

Slide 85 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 85 Flaky Test !!

Slide 86

Slide 86 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 86 Need more testing ?

Slide 87

Slide 87 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 87 Snapshot testing Unit testing Component testing Property- based testing ….

Slide 88

Slide 88 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 88 Continuous Testing === More Confidence

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 90 Learn from your mistake

Slide 91

Slide 91 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 91 Bug-Driven Development

Slide 92

Slide 92 text

JavaScript Testing © 2017 - 2024 Siam Chamnankit Company Limited. All rights reserved. 92 Q/A