Jest is a JavaScript testing framework built by Facebook and primarily designed for React-based applications, but is also used with:
A test is a prove of work about some code I want to check its predictable functionality.
git init
to create our local repository
npm init
to preparing our ork environment
npm install jest --save-dev
to install Jest.js like a development dependencies
Inside src folder, we create a test folder call it test by dev community standards.
Inside test folder, the testing files have the next syntax name: prove-name.test.js
Next we have to go to package.json file and add this test script:
{
"scripts": {
"test": "jest"
}
}
We have two way to run our testing:
npm run test
npm test
To create a test, we use a function call test() that receives two parameters:
Check the syntax in a simple example testing strings:
let str = 'Hello sexy';
test("Input have the word: Hello", ()=>{
expect(str).toMatch(/Hello/);
expect(str).toMatch(/sexy/);
})
When the test is success we can see a image like this:
// =========== Str let str = 'Hello sexy'
test("Input have the word: Hello", ()=>{
expect(str).toMatch(/Hello/);
expect(str).toMatch(/sexy/);
})
// =========== Str Arrayslet fruits = ["Orange", "Apple", "Grapes"];
test("Is there fruits inside", ()=>{
expect(fruits).toContain("Orange");
expect(fruits).toContain("Apple");
expect(fruits).toContain("Grapes");
})
// =========== Number
test("Input expect is greater than reference", ()=>{
expect(10).toBeGreaterThan(9);
expect(10).toBeGreaterThan(1);
})
// =========== Booleans
test("Is true",()=>{
expect(true).toBeTruthy();
})
test("Is false",()=>{
expect(false).toBeFalsy();
})
It’s common in JavaScript for code to run asynchronously. When you have code that runs asynchronously, Jest needs to know when the code it is testing has completed, before it can move on to another test. Jest has several ways to handle this.
// =========== CallBackslet reverseStr = (str, callBack)=>{
callBack(str.split("").reverse().join(""))
}
test("reverse string",()=>{
reverseStr('Jorge', (str)=>{
expect(str).toBe("egroJ")
})
})
// =========== Promiseconst reverseStr1 = (str)=>{
returnnewPromise((resolve, reject)=>{
if(!str){
reject(Error("Error"))
}
resolve(str.split("").reverse().join(""))
});
};
test("reverse str by Promise",()=>{
return reverseStr1("ana")
.then((str)=>{
expect(str).toBe("ana")
});
});
// =========== Async and Await
test("Reverse str by Async & Await", async ()=>{
const string = await reverseStr1("ana");
expect(string).toBe("ana")
})
Useful tip testing a DataBase:
// =========== Functions runs after of before to run test// After: This Function let me run test with functions ran before the test
afterEach(()=> console.log("After each test"));
afterAll(()=> console.log("After all test"));
// Before:
beforeEach(()=> console.log("Before each test"));
beforeAll(()=> console.log("Before all test"));
describe("testing randomCity() functionality", ()=>{
test("Return a random city from a Array", ()=>{
expect(typeof(randomCity())).toBe("string")
});
test("Verify is Cali not exist", ()=>{
expect(typeof(randomCity())).not.toMatch(/Cali/);
});
})
In our package.json file we can create a script to run a watcher focusing in our tests, the script look like this:
"scripts": {
"test": "jest",
"test:watch": "jest --watch"
},
When we run npm run test:watch
the watcher run continuously and in the console we can see the next options:
The line command is: test --coverage
and generate a result like this…
and a folder call coverage, if we want to see deeply, is only run this file in our browser: coverage/Icov-report/index.html