Testing, testing, 1, 2, 3…

How to get started unit testing your JavaScript code

Testing your code is one of the more intimidating parts of programming for beginners, it can be difficult to even figure out what to test, let alone how to do it. Most development courses will provide tests along with the challenges you need to solve so you never get to work directly with testing until you finish the material and are out in the scary big world of code on your own.

mkdir my-first-jest && cd $_
npm init -y
npm i jest --save-dev
"scripts": {
"test": "jest"
},
mkdir __tests__
describe("test function", () => {
// test block
});
describe("function", () => {
test("purpose of the test (input)", () => {
const input = [
{ input1 },
{ input2 },
{ input3 }
];
const output = [{ input2 }];
});
});
describe("function", () => {
test("purpose of the test (input)", () => {
const input = [
{ input1 },
{ input2 },
{ input3 }
];
const output = [{ input2 }];

expect(function(input, "")).toEqual(output);
});
});
npm test

Software Engineer. Creature of Havoc.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store