Skip to content Skip to sidebar Skip to footer

How Can I Set Window To Undefined In Order To Test The Ssr Rendering In An Isomorphic Application?

I have been trying to test an else block for when window is undefined. As I'm using Next.js the window will be undefined during server side rendering (SSR). At present I can not fi

Solution 1:

"By adding a @jest-environment docblock at the top of the file, you can specify another environment to be used for all tests in that file":

/**
 * @jest-environment node
 */import getLanguage from'./getLanguage';

describe('Get Language SSR', () => {

  it('should return "en"', () => {
    expect(getLanguage()).toEqual('en');  // SUCCESS
  })

});

The @jest-environment docblock will set the test environment for this test file to node causing window to be undefined and resulting in the test passing.

Post a Comment for "How Can I Set Window To Undefined In Order To Test The Ssr Rendering In An Isomorphic Application?"