samedi 5 décembre 2020

Testing 'console.log' with Jest [duplicate]

I'm fairly new to testing with jest and can't figure out how to test a function that doesn't return anything. This is how I test the function checkSpeed when it returns a value:

function checkSpeed(speed) {
    const speedLimit = 50; 

    if (speed <= speedLimit) {
        return 'Not over the speed limit'; 
    }
    return 'Speed Limit exceeded!');
}

module.exports = checkSpeed

The working test looks like this:

const {expect} = require('@jest/globals');

const checkSpeed = require('./speed');

test ('returns Message according to speed', () => {
    expect(checkSpeed(40)).toBe('Not over the Speed Limit')
    expect(checkSpeed(60)).toBe('Speed Limit exceeded!')
})

But how should I test the function when it doesn't return anything but just displays a message with console.log()? What is considered to be a good practice when testing something like this?

function checkSpeed(speed) {
    const speedLimit = 50; 

    if (speed <= speedLimit) {
        console.log('Not over the speed limit'); 
    }
    console.log('Speed Limit exceeded!');
}

module.exports = checkSpeed

Aucun commentaire:

Enregistrer un commentaire