Jest with coffeescript jsx?

前端 未结 3 1459
野趣味
野趣味 2021-01-11 18:27

How can I use Jest to test React components written in CoffeeScript + React jsx?

The only CoffeeScript example provided with Jest uses plain CoffeeScript, and doesn\

3条回答
  •  天涯浪人
    2021-01-11 18:59

    I think your second approach was correct, except you did not (I'm guessing here) add react to "unmockedModulePathPatterns" in the jest property of package.json. That is typically the result of the getPooled error in my experience.

    The following works for me:

    package.json

      // ...
      "jest": {
        "unmockedModulePathPatterns": ["/node_modules/react"],
        "testFileExtensions": [
          "js",
          "coffee"
        ],
        "scriptPreprocessor": "/preprocessor.js"
      }
    

    preprocessor.js

    // I found it simpler to use coffee-react,
    // since it does the jsx transform and coffeescript compilation 
    var coffee = require('coffee-react');
    
    module.exports = {
      process: function(src, path) {
        if (path.match(/\.coffee$/)) {
          return coffee.compile(src, {bare: true});
        }
        return src;
      }
    };
    

    This whole process is difficult troubleshoot because errors can happen anywhere during the jsx -> coffee -> js -> jest pipeline and get silently swallowed. I found it most helpful to troubleshoot this by running the transform in a separate file to make sure the jsx -> coffee and coffee -> js happened properly, and then run the jest preprocessor.

提交回复
热议问题