How to mock cookie inside JEST in ReactJS?

As we know that using a cookie we can store data as a string in the user’s browser. We may need to mock the cookie when running test cases in ReactJS using the JEST test runner.

We can mock the cookie Object’s define property. Following is the code:

import * as React from "react"
import { shallow } from 'enzyme';
import User from './User';

describe('Register app Vipps User component', () => {
    describe('Existing users', () => {

        beforeEach(() => {
            Object.defineProperty(document, 'cookie', {
                writable: true,
                value: 'status=active',
            });
        });

        it('Check login link on thank you page', () => {
            console.log(document.cookie);
            const wrapper = shallow();
            expect(wrapper.find('a').prop('href').includes('active')).toBe(true);
        });
    });
});