Introduction to Testing Flashcards
What is regression?
In software development, you can think of regression as an event that causes previously working code to stop working after a change to your code or environment. In effect, your program regresses to an earlier development state.
install jest
npm install jest -g
run jest on a file
jest filename.test.js
the entire set of tests that accompanies your program or application. You can think of it as all of the tests for a project.
a Test Suite?
What is: is a specific situation or context that you’re attempting to test.
For instance, it may attempt to verify that you get an error message when you try to log in with the wrong password. Each can contain multiple assertions.
a Test
You may sometimes see tests referred to as specs.
What is the verification step that confirms that your program did what it should. In particular, many of these test whether the return value of a function or method matches the expected results. You can make multiples of these within a test.
Assertion
Assertions are also called expectations.
command to run all tests in a folder
jest
Write a simple test to test whether an object created by a car class has 4 wheels.
Describe how it works
In one file:
class Car {
constructor() {
this.wheels = 4;
}
}
module.exports = Car;
In the test file:
const Car = require(“./car”);
describe(“The Car class”, () => {
test(“has four wheels”, () => {
let car = new Car();
expect(car.wheels).toBe(4);
});
});
Each test function defines a new test. It takes a description string and a callback.
String describes the test. When the test runs it lets you know what is being tested.
Within each test, we need to make one or more assertions. These assertions confirm the behavior that we’re trying to verify. Before we make any assertions, however, we must set up any data that we need in the test, in this case we don’t need any.
Our assertion, or expectation, is on line 2. Each expectation in Jest begins with a call to the expect method. The argument passed to expect is the value that we want to assert; it’s often called the actual value.
The expect method, in turn, returns an object that includes a variety of matcher methods. Matchers compare the actual value passed to expect with the expected value, but don’t return a meaningful value. (In particular, they don’t return a boolean value.) Instead, they simply inform Jest of the results, and Jest takes care of treating that result as a success or failure.
There are many matchers available in Jest. toBe merely checks for equality. In this case, car.wheels evaluates to 4, and that is equal to the value passed to toBe. Thus, the assertion passes.
Have 2 assertions or expectations
describe(“The Car class”, () => {
test(“has fourwheels”, () => {
let car = new Car();
expect(car.wheels).toBe(4);
expect(true).toBe(false);
});
Have 2 groups of tests
describe(“The Car class”, () => {
test(“has fourwheels”, () => {
let car = new Car();
expect(car.wheels).toBe(5);
});
});
describe(“The value true”, () => {
test(‘is false’, () => {
expect(true).toBe(false);
});
});
make jest auto update
jest –watch filename.test.js
skip a test (without commenting it out) (2 ways)
const Car = require(“./car”);
describe(“The Car class”, () => {
test(“has four wheels”, () => {
let car = new Car();
expect(car.wheels).toBe(4);
});
test.skip(“bad wheels”, () => {
let car = new Car();
expect(car.wheels).toBe(3);
});
xtest(“bad wheels”, () => {
let car = new Car();
expect(car.wheels).toBe(3);
});
});
Where can i find all the expectations/assertions?
test.skip(“bad wheels”, () => {
let car = new Car();
expect(car.wheels).toBe(3);
});
Will this pass?
test(‘two new cars are equal objects’, () => {
let car1 = new Car();
let car2 = new Car();
expect(car1).toEqual(car2);
});
This test passes since toEqual compares the properties of one object with those of the other. If they have the same number of properties with the same property names, and same values, they’re considered equal.
The test would fail if you used toBe instead of toEqual. On the other hand, a test that passes with toBe will also pass with toEqual.
make a test that only passes when the argument throws a type error
test(‘raises a TypeError when called drive on it’, () => {
let car = new Car();
expect(() => car.drive()).toThrow(TypeError);
});