Skip to content
Advertisement

Unit testing a method that creates a JWT and returns Error: secretOrPrivateKey must have a value

I am trying to write a unit test for the method I wrote which generates a JWT. I am doing the following

describe('returns a token', function() {
        it('should return a token', function() {
            let req = {};
            const a = authenticatorClass.returnToken(req);
            console.log(a);
        });

Note: I am not using expect yet, and seeing if the method works in first place by logging the result of the method to the console

And I receive the following error: Error: secretOrPrivateKey must have a value

This is the method that I am trying to test:

returnToken(expressRequestObject) {
        const payload = {};

        return jwt.sign(
            payload,
            SECRET,
            { expiresIn: '30d' },
        );
    }

What am I doing wrong here? TIA

Advertisement

Answer

Might be your SECRET is not having any value, can you log it?

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement