Professional Documents
Culture Documents
TESTING
WHAT IS TESTING?!
testing means checking that our
code meets some expectations.
"scripts": {
"test": "jest"
},
Navigate to the directory and create test
folder
cd getting-started-with-jest
mkdir __tests__
Creating Test
describe("Filter function", () => {
test("it should filter by a search term (link)", () => {
const input = [
{ id: 1, url: "https://www.url1.dev" },
{ id: 2, url: "https://www.url2.dev" },
{ id: 3, url: "https://www.link3.dev" }
];
expect(filterByTerm(input, "link")).toEqual(output);
});
});
describe
describe, a Jest method for containing one or more related tests. Every
time you start writing a new suite of tests for a functionality wrap it in
a describe block.
Creating Test
describe("Filter function", () => {
test("it should filter by a search term (link)", () => {
const input = [
{ id: 1, url: "https://www.url1.dev" },
{ id: 2, url: "https://www.url2.dev" },
{ id: 3, url: "https://www.link3.dev" }
];
expect(filterByTerm(input, "link")).toEqual(output);
});
});
test
expect(filterByTerm(input, "link")).toEqual(output);
});
});
expect
In a Jest test you should wrap the function call inside expect which
coupled with a matcher (a Jest function for checking the output)
makes the actual tests.
Creating Test
describe("Filter function", () => {
test("it should filter by a search term (link)", () => {
const input = [
{ id: 1, url: "https://www.url1.dev" },
{ id: 2, url: "https://www.url2.dev" },
{ id: 3, url: "https://www.link3.dev" }
];
expect(filterByTerm(input, "link")).toEqual(output);
});
});
Run
npm test
FAILED!!!
Fixing Test
expect(filterByTerm(input, "link")).toEqual(output);
});
});
Run Again
npm test
PASSED!!!
Additional Test
describe("Filter function", () => {
test("it should filter by a search term (link)", () => {
const input = [
{ id: 1, url: "https://www.url1.dev" },
{ id: 2, url: "https://www.url2.dev" },
{ id: 3, url: "https://www.link3.dev" }
];
expect(filterByTerm(input, "link")).toEqual(output);
expect(filterByTerm(input, "LINK")).toEqual(output);
});
});
https://dev.to/valentinogagliardi/jest-tutorial-for-beginners-getting-started-with-jest-for-javascript-testing-e6c