7

I'm trying to access a property that is located under window.sunpietro.config property. When running tests with Jest I'm getting TypeError: Cannot read property 'config' of undefined.

My test code looks like the following:

import React from 'react';
import { render, cleanup } from 'react-testing-library';
import MyModule from '../my.module';
import { myConfigMock } from '../../../../jest.window.mock';

afterEach(cleanup);
beforeEach(() => {
    window.sunpietro = { ...myConfigMock };

    // window.sunpietro.config = {};
});

describe('MyModule', () => {
    test('The module renders correctly', () => {
        const { getByTestId } = render(<MyModule />);

        getByTestId('my-tabs').toBeDefined();
        getByTestId('my-panels').toBeDefined();
    });
});

I'm using the latest version of Jest: 24.7.1 and the react-testing-library version: 6.1.2.

How can I mock the window properties so they're accessible by MyModule instance?

1 Answer 1

6

You could set global variables in your setup. I have this setupFiles declared in my package.json

"jest": {
"setupFiles": [
  "<rootDir>/tests/shim.js",
  ...
],

Then in my shim.js. I am declaring window properties as global like

global.sunpietro : {
 ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.