A general testing scenario

Let's explore some of the most basic ES6 testing scenarios, shown in the following table.. You can find the working example in the GitHub repository, inside of the CH02 folder:

Designation

JEST test methods

Matchers

it("Matchers", () => {
expect(24 + 20).toBe(44);
expect({ one: 1, two: 3 }).toEqual({ one: 1, two: 3 });
for (let i = 1; i < 10; i++) {
for (let j = 1; j < 10; j++) {
expect(i + j).not.toBe(0);
}
}
});

Truetiness

it("Truetiness", () => {
const isActive = null;
expect(isActive).toBeNull();
expect(isActive).toBeDefined();
expect(isActive).not.toBeUndefined();
expect(isActive).not.toBeTruthy();
expect(isActive).toBeFalsy();
});

Zeros

it("zeros", () => {
const z = 0;
expect(z).not.toBeNull();
expect(z).toBeDefined();
expect(z).not.toBeUndefined();
expect(z).not.toBeTruthy();
expect(z).toBeFalsy();
});

Numbers

it("Numbers", () => {
const value = 100 + 200;
expect(value).toBeGreaterThan(200);
expect(value).toBeGreaterThanOrEqual(200);
expect(value).toBeLessThan(500);
expect(value).toBeLessThanOrEqual(300);
expect(value).toBe(300);
expect(value).toEqual(300);
});

Strings

expect('team').not.toMatch(/I/);
expect('Christoph').toMatch(/stop/);

Arrays

it("Arrays", () => {
const resources = [
"Patients",
"Practitioners",
"Accountants",
"Employer",
"Appointments"
];
expect(resources).toContain("Accountants");
});

Exceptions

it("Exceptions", () => {
const t = () => {
throw new TypeError();
};
expect(t).toThrow(TypeError);
});
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset