Tag Archives: jest

jest (basics)

ref – https://medium.com/piecesofcode/testing-javascript-code-with-jest-18a398888838

then in package.json:

make sure the “scripts” key, and its values are there.

Test Custom function

Let’s define a custom function.
concat.js

Then we test that custom function

Make sure you name it yourclass.test.js

This lets Jest know which files to test.

So if in our case, if we’re gunna test for concat.js, we’ll name it concat.test.js

concat.test.js

Open a terminal, and in your folder directory, type: npm test

You’ll then see the result of your tests.

Basically, we use expect(…).toBe(…)

to make sure the returned result from our custom function matches up with what’s in toBe(…).

.toBe() basically just checks that value is what you expect it to be.

Testing custom objects

We can also declare a custom object, and then expect(…).toBe(…) an object.

matchers.test.js

Is the object in question equals given object?

Declare a null. Test if its a null. Test if its defined.

doing addition/subtraction with numerics

Check for elements in an array

Match a character in a string

callback

First we define the function getMessage, where we take a callback object parameter.
In the function getMessage, we pass in a string “go Javascript!”.

Then in our test, we define the callback function. In turn we pass in this callback function into the getMessage.

The way we test for the callback is to expect the value inside the callback function to be the string “go Javascript!”