Introduction to Testing Flashcards

1
Q

What is regression?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

install jest

A

npm install jest -g

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

run jest on a file

A

jest filename.test.js

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

a Test Suite?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

a Test
You may sometimes see tests referred to as specs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

Assertion
Assertions are also called expectations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

command to run all tests in a folder

A

jest

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a simple test to test whether an object created by a car class has 4 wheels.

Describe how it works

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Have 2 assertions or expectations

A

describe(“The Car class”, () => {
test(“has fourwheels”, () => {
let car = new Car();
expect(car.wheels).toBe(4);
expect(true).toBe(false);
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Have 2 groups of tests

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

make jest auto update

A

jest –watch filename.test.js

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

skip a test (without commenting it out) (2 ways)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Where can i find all the expectations/assertions?

A

test.skip(“bad wheels”, () => {
let car = new Car();
expect(car.wheels).toBe(3);
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Will this pass?
test(‘two new cars are equal objects’, () => {
let car1 = new Car();
let car2 = new Car();

expect(car1).toEqual(car2);
});

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

make an test that only passes when the argument throws a type error

A

test(‘raises a TypeError when called drive on it’, () => {
let car = new Car();
expect(() => car.drive()).toThrow(TypeError);
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

make a test that only passes when it the argument is the same as the actual value/value that we want to assert (2 options, 1 for equality, the other for if they are the same. Like objects that are different but have the same body)

A

toBe

vs

test(‘two new cars are equal objects’, () => {
let car1 = new Car();
let car2 = new Car();
expect(car1).toEqual(car2);
});

17
Q

make an test that only passes when the argument inverse of what it usually would be

A

test(‘car has wheels’, () => {
let car = new Car();
expect(car.wheels).not.toBeUndefined();
});

18
Q

test(‘array contains car’, () => {
let car = new Car();
let arr = [1, 2, 3];
arr.push(car);

expect(arr).toContain(car);
});

A

<== true

test(‘string contains “car”’, () => {
let man = “His scars have healed”;
expect(man).toContain(“car”);
});

true

19
Q

what are the 4 steps to writing a test

A

SEAT approach

Set up the necessary objects.

Execute the code against the object we’re testing.
the section within expect(HERE) is the execution step

Assert the results of execution.
expect().toBe(4) the execution to be something is the assertion

Tear down and clean up any lingering artifacts.

20
Q

How to prevent redundant code in tests, like “let car = new car()” in every single one? (2 ways)

A

With before each or after each:

describe(‘The Car class’, () => {
let car;
beforeEach(() => {
car = new Car();
});

test(‘has four wheels’, () => {
expect(car.wheels).toBe(4);
});

You might argue that we don’t need beforeEach to instantiate a new Car object. We could, for instance, initialize the car variable at the top of the describe callback and assign it a new Car object. That would work here. However, in most cases, you need to make changes to your object and experiment with it. If you don’t use the beforeEach callback, the car object won’t get reset for each test. It’s better to create a new object for each test so that you have one with a known state.

21
Q

T or F
You can have multiple expectations/assertions within a test

A

T

22
Q

T or F
You can’t have another describe within a describe

A

F

23
Q

Will this pass the test? (purp throwing an error because not defined.)

test(‘is false’, () => {
expect(purp()).toThrow();
});

A

the test won’t be able to catch the error since the exception will occur before expect even starts to execute (the arguments need to be evaluated before a function can be called).

have to do:
test(‘is false’, () => {
expect(() => purp()).toThrow();
});

24
Q

Get a sense of how much of code functions are covered by the tests

A

jest –coverage todolist.test.js

25
Q

Quiz
https://launchschool.com/quizzes/e1dd4ea1

A

https://launchschool.com/quizzes/e1dd4ea1

26
Q

Challenges
https://launchschool.com/lessons/2b72565b/assignments/9af0c0a7

A

https://launchschool.com/lessons/2b72565b/assignments/9af0c0a7