After installing Jest, you have to update your script commands to use it.
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"test": "jest"
},
It's the proccess of testing a small piece of code, such as basic functions and basic classes. We do that in order to check small errors that can be produced by these small functions and classes.
Whichever file you call <fileName>.text.js
it will be automatically interpreted as a test file by JavaScript.
The basic syntax is:
test('description', testFunction => {})
The complete syntax of a basic test is:
test("adds 1 + 2 to equal 3", () => {
expect(sum(1, 2)).toBe(3);
});
For a unknown reason I already despise, there's a couple linked syntax configs that you should know when dealing with exports and imports in vanilla JS:
- Create your function inside a
.js
file without exporting it. - Export it using
module.exports = <functionName>
- Import your function at the top of your
test.js
file usingconst <functionName> = require("<pathToFile")
Vite's linter will warn you that there's a better way to write ES module imports. Ignore it if you don't know what you're dealing with. Trust me.
.toBe()
is used to expect primitive values (e.g. numbers, strings, booleans)toEqual()
compares values of objects of arrays (i.e.: non-primitive values)toBeFalsy()
checks values expected to be falsy. Simple.toBeTruthy()
, same thing but on the contrarytoThrow()
, useful when a function is built to throw errors in given scenarios