Professional Documents
Culture Documents
If tests are not automated, then we end up manually inspecting every feature in every release. The manual approach is
unscalable & its easier to make mistakes
In Automated tests, we have a piece of code that does the verification for us. Automated tests interface with our application to
perform actions and compare the actual result with the expected output we have previously defined.
By making our test reproduce the steps that may cause the bug to happen, we can be assured that the specific bug does not
happen any more
If we are working on a new feature or fixing an existing bug, passing tests for the existing features, makes us more confident
that our code is not causing any regressions
The more tests we have and the closer these tests resemble what real users do, the more reliable they become.
Tests decreases the time it takes to get feedback on the code we write, and, therefore make it quicker to fix mistakes. Not
having automated tests will cause us to write too much code before checking whether the feature works. Most of the time, by
the time we do realise that the code is buggy, its too late. We have already written too much code and there are more places
for bugs to hide.
Even if we maintain a checklist of test cases for manual testing, we will have the overhead of keeping that documentation
always up to date. Humans are bad at performing repetitive tasks. Machines are good at it.
Tests are excellent for collaborative development. When a programmer adds automated tests for their features, everyone else
benefits. The other developers dont need to ask them how to test their changes. Even if our changes builds on top of theirs,
we can be assured that we are not breaking any of their work.
Initially, writing tests may be time consuming, but more we run it, the more value we extract from it. As time passes and the
number of features increases in our application, the efforts involved in manual tests grows much quicker than automated
tests. The longer the lifecycle of a project, the more critical tests become.
Type of tests
Consider a bicycle:
Integration tests: It’s also vital to ensure that they work together. for example a chain must fit properly into the chain rings. The
pedals must fit into the chain ring in an expected way. Similarly, the next step is to test larger components which are a combination
of several functions.
E2E Tests: Its equally important to test whether people can drive the bi-cycle once all parts are in place. In the same way, the final
step is to test the application itself.
Tools
Unit & Integration tests: Jest
E2E: Cypress
Unit tests
Unit tests helps us ensure that the smallest units of our software, functions, behave as we expect them to. We write unit tests to
test the functionality of our code, i.e. Whether our code is working as expected or not. A unit in a unit test can be a function, an
object or a few lines of code that is capable of either consumed or executing independently.
Documentation
https://jestjs.io/docs/getting-started
Examples
https://codesandbox.io/s/javascript-testing-class-8q0y87?file=/src/matchers.test.js:564-778
https://codesandbox.io/s/jest-mocking-0uwg8v?file=/src/localBank.test.js:2125-2148
https://stackblitz.com/edit/jest-example-kfagfy?file=playground.test.js
Format
Live examples
// matchers:
// toBe strict equality
// range
// null, undefined or NaN
// truthy or falsey
// object properties
// collections
// errors
// custom mathcers
toBe is a matcher
Matchers
toBe() vs toEqual()
toEqual()
const book1 = {
id: 5,
title: "Book 5",
price: 49
};
const book2 = {
id: 5,
title: "Book 5",
price: 99
};
const myDesiredBook = {
title: "Book 5"
};
toMatch
expect(bookTitle).toMatch("Rings");
expect(bookTitle).toMatch(/rin../i);
// expect(bookTitle).not.toMatch("Potter");
});
kind of subset
toBeLessThan()
expect(price).toBeCloseTo(5.1234, 4);
});
expect(a).toBeUndefined();
expect(b).toBeNull();
expect(c).toBeNaN();
expect(d).toBeDefined();
});
// toBeTruthy(), toBeFalsy()
// falsey values - false, 0, "", null, underfined, NaN
// more falsey values - -0, -0n, Objects are falsy if and only if they have the [[IsHTMLDDA]] internal slot.
test("understanding toBeTruthy and toBeFalsy", () => {
let a = 0;
let b = 1;
expect(a).toBeFalsy();
expect(b).toBeTruthy();
});
// toHaveLength() array/string
// toHaveProperty() can use the dot syntax (or array syntax) to drill into nested objects;
// "one.two.three" or ["one","two","three"]
// 2nd argument if we care about the value
test("understanding matchers for object properties", () => {
let str = "Vivek";
let arr1 = ["one", "two", "three"];
let arr2 = ["four", "five", "six"];
let obj = {
name: "Vivek",
arr1: ["one", "two", "three"],
obj1: {
a: "a",
b: "b"
}
};
expect(obj).toHaveProperty("obj1.a", "a");
expect(str).toHaveLength(5);
expect(arr1).toHaveLength(3);
});
fruits.set("mangoes", 700);
expect(fruits.has("bananas")).toBeTruthy();
expect(fruits.get("mangoes")).toBe(700);
letters.add("d");
letters.add("e");
letters.add("f");
expect(letters.has("e")).toBeTruthy();
letters.delete("e");
expect(letters.has("e")).not.toBeTruthy();
});
Javascript
Class - 1 Video Hinglish https://www.loom.com/share/189401b30d724e0b81aa00a9ba8ef249
Testing Course
Javascript https://codesandbox.io/s/javascript-testing-class-8q0y87?
Class - 1 Codesandbox English
Testing Course file=/src/matchers.test.js:2953-2959
Javascript
Class - 2 Video Hinglish https://www.loom.com/share/f7ddf66dab8443858cf2651edacb5a25
Testing Course
Javascript
Class - 3 Video Hinglish https://www.loom.com/share/66108fc73e8049c389f995d2042e42b4
Testing Course
Javascript
Class - 4 Video Hinglish https://www.loom.com/share/a8ff801ac55a45bfb2d89a45f89143e5
Testing Course
Javascript
Class - 5 Video Hinglish https://www.loom.com/share/bc7c793afcdb42b781051829a516192f
Testing Course
Javascript Class - 5 Codesandbox English https://codesandbox.io/s/asynchronousjavascriptclass-ng2cl4?
Testing Course file=/src/asyncAwait.test.js:617-622
Javascript
Class - 7 Video Hinglish https://www.loom.com/share/d3f8d0db695f46e19a1e46001413da8e
Testing Course
Javascript https://stackblitz.com/edit/jest-example-kfagfy?
Class - 7 StackBlitz English
Testing Course file=playground.test.js
Javascript
Class - 8 Video Hinglish https://www.loom.com/share/9dfa5817120c43fb897f4ec16a12177e
Testing Course
Javascript https://stackblitz.com/edit/jest-example-kfagfy?
Class - 8 StackBlitz English
Testing Course file=playground.test.js