用咖啡脚本 jsx 开玩笑? [英] Jest with coffeescript jsx?

查看:19
本文介绍了用咖啡脚本 jsx 开玩笑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Jest 测试用 CoffeeScript + React jsx 编写的 React 组件?

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

Jest 提供的唯一 CoffeeScript 示例使用普通的 CoffeeScript,并且不适用于 CoffeeScript + React JSX(到达 < 时的语法错误).

The only CoffeeScript example provided with Jest uses plain CoffeeScript, and doesn't work with CoffeeScript + React JSX (syntax error when it reaches a <).

// preprocessor.js
var execSync = require('exec-sync');

module.exports = {
  process: function (src, path) {
    return execSync('browserify -t coffee-reactify ' + path);
  }
};

这可行,但需要花费太多时间(对于虚拟测试来说,12 秒不错).

This works, but takes too much time (a good 12 seconds for a dummy test).

然后我尝试了:

// preprocessor.js
var coffee = require('coffee-script');
var transform = require('coffee-react-transform');

module.exports = {
  process: function(src, path) {
    if (path.match(/.coffee$/)) {
      return coffee.compile(transform(src), {'bare': true});
    }
    return src;
  }
};

这会引发一个奇怪的错误,例如:

This throws a strange error, like:

TypeError: function() {...} 没有方法 'getPooled'

TypeError: function() {...} has no method 'getPooled'

没有方法 'getPooled'"的唯一 Google 结果是 this gist,它准确地显示了错误 I得到,但没有提供其他见解.

The only Google result for "has no method 'getPooled'" is this gist, that shows exactly the error I get, but offers no other insights.

我想我可以使用 coffee-reactify,但它返回一个异步流,而 <preprocess.js中的code>process函数是同步使用的,目前还没找到同步读取流的方法.

I think I could use coffee-reactify, but it returns a stream, which is asynchronous, while the process function in preprocess.js is used synchronously, and have found no way, so far, to read a stream synchronously.

我能做什么?

推荐答案

我认为你的第二种方法是正确的,除了你没有(我在这里猜)在 jest package.json 的属性.根据我的经验,这通常是 getPooled 错误的结果.

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.

以下对我有用:

package.json

  // ...
  "jest": {
    "unmockedModulePathPatterns": ["<rootDir>/node_modules/react"],
    "testFileExtensions": [
      "js",
      "coffee"
    ],
    "scriptPreprocessor": "<rootDir>/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;
  }
};

整个过程很难排除故障,因为在 jsx -> 期间的任何地方都可能发生错误.咖啡->js->开个玩笑管道,然后默默地吞下去.我发现通过在单独的文件中运行转换以确保 jsx -> 来解决此问题最有帮助.咖啡咖啡->js正常发生,然后运行jest预处理器.

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.

这篇关于用咖啡脚本 jsx 开玩笑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆