最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JestReact - How to use global object in unit tests? - Stack Overflow

programmeradmin0浏览0评论

I use CommonJS modules with require() except 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 a unit test on MyComponent, Jest can't find React. Is there a way to tell Jest to insert a global React object? (I use npm and gulp-browserify.)

I use CommonJS modules with require() except 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 a unit test on MyComponent, Jest can't find React. Is there a way to tell Jest to insert a global React object? (I use npm and gulp-browserify.)

Share Improve this question edited May 14, 2015 at 9:13 Cihan Keser 3,2615 gold badges31 silver badges43 bronze badges asked May 14, 2015 at 9:06 CsatiCsati 1,2711 gold badge15 silver badges29 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Works for 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
发布评论

评论列表(0)

  1. 暂无评论