Jest / React - How to use a global object in unit tests?

I use CommonJS modules with require (), except for React, which is global:

// I don't want require React in every module:
// var React = require("react");

var MyComponent = React.createClass({ // React is global here
});

When running unit test in MyComponent, Jest cannot find React. Is there a way to tell Jest about nesting a global React object? (I use npm and gulp -browserify.)

+4
source share
1 answer

Works with me with the following settings.

// package.json

"jest": {
  "scriptPreprocessor": "preprocessor.js",
  "unmockedModulePathPatterns": [
    "react"
  ],
  "setupEnvScriptFile": "before_test.js"
}

// preprocessor.js

var ReactTools = require('react-tools');

module.exports = {
    process: function(src) {
        return ReactTools.transform(src);
    }
};

// before_test.js

React = require("react"); // Global React object
+8
source

All Articles