Jest how to cover all branches of a global variable

I am using Jest with React-Native and I came across a problem.

A tiny piece of code in the App.js component that causes a 50:50 fork to span:

const storeMiddleware = __DEV__ ?
    applyMiddleware(
        thunkMiddleware,
        loggerMiddleware
    ) :
    applyMiddleware(
        thunkMiddleware
    );

Test case itself:

import 'react-native';
import React from 'react';
import App from '../App.js';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

describe('App', () => {
    it('should render correctly', () => {
        const tree = renderer.create(
            <App />
        ).toJSON();
        expect(tree).toMatchSnapshot();
    });
    // TODO: test if app renders correctly when __DEV__ is false
});

How do I change my test to get 100% coverage

+4
source share
1 answer

I assume that __DEV__this is a global variable, so you can easily change this value with

global['__DEV__'] = false;

Btw. there is no need to get 100% coverage, and it also makes no sense to test the settings for your devirop.

0
source

Source: https://habr.com/ru/post/1658720/


All Articles