DEV Community

Jordi Enric
Jordi Enric

Posted on

Learn to create your own mini testing library in 5 minutes

Before you start

Create a testing.js file and run it with node testing.js to try your library.

This article is aimed at beginners who just started using jest and want to have a better grasp on how it works behind the scenes.

How to create our library

If you've used jest you've probably seen the test() - expect() - toBe() methods.

I'm going to teach you how to build your own "mini" testing library, so you can have these methods and test your app without installing jest, just using Node.

First let's create a method we can test

const sum = (a, b) => a + b
Enter fullscreen mode Exit fullscreen mode

Now let's create a function to test this method

if (sum(1, 2) !== 3) {
    console.error('Test failed. Sum function is broken!')
}
Enter fullscreen mode Exit fullscreen mode

Now if we ever break our sum function our if statement will send an error to the console.

This works, but we want to test our other methods too and writing if statements would be repeating too much code. Remember, keep your code DRY! (Don't Repeat Yourself)

Let's turn that logic into a reusable function. We'll call it expect and it will return a function called toBe

function expect(actual) {
    return {
        toBe(expected) {
            if (actual !== expected) {
                throw new Error('Test failed')
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Now we can test our function like this:

expect(sum(1, 2)).toBe(3)
Enter fullscreen mode Exit fullscreen mode

This is what we call an assertion.

If our sum function works, we'll get nothing in the console. If it's broken however, we'll get an error.

Now let's create a test suite! We want to test our sum function with different numbers to make sure it always works.

For this we'll create a function called test

function test(title, callback) {
    try {
    callback()
    console.log(title + ' tests passed!')
    } catch(error) {
    console.error('Some tests failed!')
    }
}
Enter fullscreen mode Exit fullscreen mode

Now we can test our sum function or any other function like this:

test('Sum', () => {
    const result1 = sum(1, 4)
    const result2 = sum(2, -1)
    const result3 = sum(10, 10)

    const expected1 = 5
    const expected2 = 1
    const expected3 = 20

    expect(result1).toBe(expected1)
    expect(result2).toBe(expected2)
    expect(result3).toBe(expected3)
})
Enter fullscreen mode Exit fullscreen mode

Now we'll get "Sum tests passed" if our function works correctly or "Some tests failed" if we break it.

Conclusion

The idea behind this article is showing the simplicity behind some basic functionalities of testing libraries. If you understand how they're built you'll have a better time using them. If you've followed the code in this tutorial you'll probably get some ideas on how to improve it.

You could try adding more functions like notToBe or equals

I decided to not use newer JS syntax in this article to make it easier to understand for those starting out.


Find me on twitter

I'm also working on a website & newsletter with resources, tools and cool stuff for frontend developers, you can find it here: https://zerotofrontend.dev/ it's currently on "soft launch" mode and I haven't started sending the letters yet. Subscribe now to get the first one in May!

Top comments (0)