Skip to content

Commit

Permalink
Learn more methods in jest
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedBHameed committed Jul 1, 2020
1 parent 8fe87a6 commit ba7e306
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/002/__test__/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const sum = require("../sum");
*/

describe("Test sum function", () => {
describe("Check if no number passed or one number only", () => {
describe.only("Check if no number passed or one number only", () => {
it("Should return 0 if no argument passed.", () => {
expect(sum()).toBe(0);
});

it("Should return number", () => {
it.skip("Should return number", () => {
expect(sum(10)).toBe(10);
});
});
Expand Down
28 changes: 28 additions & 0 deletions docs/004/__test_/run.testt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* beforeAll(fn, timeout)
* beforeEach(fn, timeout)
*/

function check() {
return 1;
}

beforeAll(() => {
/**
* Used with
* 1- Connect to database.
* 2- Empty testing table
* 3- Add dummy data for testing
* 4- Prepare everything before testing
*/
expect(check()).toBe(1);
});

afterAll(() => {
/**
* Used with
* 1- Clean database
* 2- Clean cache
*/
expect(check()).toBe(1);
});
64 changes: 64 additions & 0 deletions docs/005/__test__/all.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const allData = require("../all");

test("Check if array contain 6 elements", () => {
expect(allData.length).toBe(5);
});

test("Check if array contain 6 elements but with different method", () => {
expect(allData).toHaveLength(5);
});

test("Check if array contain number 4", () => {
expect(allData).toContain(4);
});

test("Check if array contain only number [1st method]", () => {
for (let i = 0; i < allData.length; i++) {
expect(isNaN(allData[i])).toBe(false);
}
});

test("Check if array contain only number [2nd method]", () => {
for (let i = 0; i < allData.length; i++) {
expect(isNaN(allData[i])).toBeFalsy();
}
});

test("Check if array contain only number [3rd method]", () => {
for (let i = 0; i < allData.length; i++) {
expect(isNaN(allData[i])).not.toBeTruthy();
}
});

test("Check if array first element is larger than 0", () => {
expect(allData[0]).toBeGreaterThanOrEqual(1);
});

test("Check if array first element is less than 10", () => {
expect(allData[0]).toBeLessThanOrEqual(10);
});

test("Check for undefined", () => {
expect(allData[10]).toBeUndefined();
});

test("Check for substring inside string by RegExp", () => {
let myString = "I love Jest";
expect(myString).toMatch(/Jest/);
});

test("Check for property age", () => {
let myObject = {
name: "Ahmed",
age: 30,
};
expect(myObject).toHaveProperty("age");
});

test("Check for property age that have value of 30", () => {
let myObject = {
name: "Ahmed",
age: 30,
};
expect(myObject).toHaveProperty("age", 30);
});
3 changes: 3 additions & 0 deletions docs/005/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const allData = [1, 2, 3, 4, 5];

module.exports = allData;

0 comments on commit ba7e306

Please sign in to comment.