Testing Flashcards
Why do we test?
As a beginner, you should write tests to preventregression. 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.
What is the syntax for a jest test?
test(‘two plus two is four’, () => {
expect(2 + 2).toBe(4);
});
What is the syntax for a test suit?
const Car = require(“./car”);
describe(“The Car class”, () => {
test(“has four wheels”, () => {
let car = new Car();
expect(car.wheels).toBe(4);
});
});
// To skip a test: test.skip(“has four wheels”, () => {… or xtest.(‘has….
How do you group your Jest tests?
-
describe(descriptionString, callback)
- The
describe
method groups your tests. Groups are there to help you structure your tests into logical sections. The string argument describes the group of tests. The callback contains the grouped tests.
- The
What method do you use for a single test?
-
test(descriptionString, callback)
- Each invocation of
test
defines a new test. The string argument describes the test. Within each test, we need to make one or more assertions.
- Each invocation of
What method provides our matcher methods?
-
expect()
- The argument passed to
expect
is the value that we want to assert; it’s often called theactual value. Theexpect
method, in turn, returns an object that includes a variety ofmatchermethods. Matchers compare the actual value passed toexpect
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.
- The argument passed to
What is a test suite?
The entire set of tests that accompanies your program or application. You can think of it as all of the tests for a project.
What is a test?
ATestis a specific situation or context that you’re attempting to test. For instance, a test may attempt to verify that you get an error message when you try to log in with the wrong password. Each test can contain multiple assertions. You may sometimes see tests referred to asspecs.
What is an assertion?
AnAssertionis the verification step that confirms that your program did what it should. In particular, many assertions test whether the return value of a function or method matches the expected results. You can make multiple assertions within a test. Assertions are also calledexpectations.
How do we invert a matcher?
Using .not :
test(‘car has wheels’, () => {
let car = new Car();
expect(car.wheels).not.toBeUndefined();
});
What is the SEAT approach?
- Set up the necessary objects.
- Execute the code against the object we’re testing.
- Assert the results of execution.
- Tear down and clean up any lingering artifacts.
What method do we use to reset the variables before each test?
let car;
beforeEach(() => {
car = new Car();
});
What callback is called after each test? (if provided)
afterEach()
What is code coverage?
code coverage is the percentage of functions or methods called by your tests or the percentage of lines of code that executed as a result of your tests.
How do you find code coverage in jest?
jest –coverage fileName.test.js