简体   繁体   English

使用rails / webpacker进行反应测试设置

[英]React testing setup with rails/webpacker

I've added webpacker to my existing rails app, everything is working like a charm. 我已经将webpacker添加到我现有的rails应用程序中,一切都像魅力一样。

Webpack config is found under Webpack配置位于

config/webpack/shared.js
config/webpack/development.js
config/webpack/production.js

node_modules are installed in node_modules安装在

vendor/node_modules

js pack files are in js包文件在

app/javascript/packs/application.js

I've installed react and wrote a little component: 我安装了react并写了一个小组件:

app/javascript/discover/example.jsx

Now I struggle with how to setup a working test environment. 现在我很难设置如何设置一个有效的测试环境。 Normally I'd say the usual test setup should include: karma , jasmine or mocha , webpack . 通常我会说通常的测试设置应该包括: karmajasminemochawebpack

Where should the config files live? 配置文件应该在哪里? Where will the test files be stored and to build a karma.config.js to bundle everything together. 将测试文件存储在何处并构建karma.config.js以将所有内容捆绑在一起。

It would be great to have a sample application that shows how to do all that correctly, but I obviously lack the necessary skills to plug everything together correctly. 如果有一个示例应用程序可以显示如何正确地执行所有操作,那将会很棒,但我显然缺乏将所有内容正确插入所需的必要技能。

This is not an easy to answer question, but having such an example application would be extremely helpful to a lot of people that'd want to use webpacker in the future. 这不是一个容易回答的问题,但拥有这样一个示例应用程序对于很多想要在将来使用webpacker的人来说非常有帮助。

Thanks for any thoughts on that topic, 感谢您对该主题的任何想法,
Jo

Some helpful resources: 一些有用的资源:

  1. https://medium.com/@scbarrus/how-to-get-test-coverage-on-react-with-karma-babel-and-webpack-c9273d805063#.g6p5go9gd https://medium.com/@scbarrus/how-to-get-test-coverage-on-react-with-karma-babel-and-webpack-c9273d805063#.g6p5go9gd
  2. http://qiita.com/kimagure/items/f2d8d53504e922fe3c5c http://qiita.com/kimagure/items/f2d8d53504e922fe3c5c
  3. http://nicolasgallagher.com/how-to-test-react-components-karma-webpack/ http://nicolasgallagher.com/how-to-test-react-components-karma-webpack/
  4. https://www.codementor.io/reactjs/tutorial/test-reactjs-components-karma-webpack https://www.codementor.io/reactjs/tutorial/test-reactjs-components-karma-webpack

NOTE: The process I went through to answer this question is not longer necessary, as the Rails team has moved JavaScript dependencies back into the root of the project by default. 注意:由于Rails团队默认情况下已将JavaScript依赖项移回项目的根目录 ,因此不再需要我回答此问题的过程。


This answer is a two-parter. 这个答案是两个部分。 First, I'll explain how, after much blood, sweat and tears, I arrived at my current Webpacker + React + Jest test environment. 首先,我将解释在经过多少血,汗水和眼泪之后,我到达了目前的Webpacker + React + Jest测试环境。 Second, I'll dive into why all the expenditure of the bodily fluids. 其次,我将深入研究为什么所有的体液消耗。

Part 1: Implementation 第1部分:实施

  1. Rails. 轨。 I generated a new Rails app, skipping all of the things that I'm not likely to need. 我生成了一个新的Rails应用程序,跳过了我不太可能需要的所有内容。

     rails new rails-react --skip-action-mailer --skip-active-record --skip-action-cable --skip-javascript --skip-turbolinks 
  2. Until Rails 5.1 ships, we need Webpacker. 在Rails 5.1发布之前,我们需要Webpacker。

     # Gemfile gem 'webpacker', git: 'https://github.com/rails/webpacker.git' 

    Then in your shell 然后在你的shell中

     $ bundle install 
  3. Set up Webpack and React using Webpacker 使用Webpacker设置Webpack和React

     $ rails webpacker:install && rails webpacker:install:react 
  4. Install Jest, and Jest related packages 安装Jest和Jest相关的包

     $ bin/yarn add jest babel-jest babel-polyfill react-test-renderer --dev 
  5. Configure Jest to play nice with Webpacker. 配置Jest以与Webpacker一起玩得很好。 This article on configuring Jest for Webpack is very helpful. 这篇关于为Webpack配置Jest的文章非常有用。 According to the docs " <rootDir> is a special token that gets replaced by Jest with the root of your project. Most of the time this will be the folder where your package.json is located". 根据文档“ <rootDir>是一个特殊的令牌,被Jest替换为项目的根目录。大部分时间这将是你的package.json所在的文件夹”。 Crucially for us this is /vendor and not / as it would be in a conventional JavaScript project. 对我们来说至关重要的是这是/vendor而不是/在传统的JavaScript项目中。

     // vendor/package.json "jest": { // The name of this directive will soon change to "roots" // https://github.com/facebook/jest/issues/2600 // This points Jest at Webpacker's default JS source directory "testPathDirs": [ "<rootDir>/../app/javascript/packs" ], // This ensures that node_modules are resolved properly // By default, Jest looks in "/" for this, not "vendor/" "moduleDirectories": [ "<rootDir>/node_modules" ], "moduleFileExtensions": [ "js", "jsx" ] } 
  6. Babel. 巴贝尔。 This one caused me the most headache. 这一次让我头疼。 Babel doesn't support dynamic configuration (although it is currently being considered ). Babel不支持动态配置(尽管目前正在考虑中 )。 Additionally, the way Babel looks for configuration does not help us. 此外,Babel寻找配置的方式对我们没有帮助。 "Babel will look for a .babelrc in the current directory of the file being transpiled. If one does not exist, it will travel up the directory tree until it finds either a .babelrc, or a package.json with a "babel": {} hash within." “Babel将在正在编译的文件的当前目录中查找.babelrc。如果不存在,它将沿目录树向上移动,直到找到.babelrc或带有”babel“的package.json: {}哈希。“

    The way Babel looks for presets is also brittle. Babel寻找预设的方式也很脆弱。 You'll note that the babel-handbook documentation for creating presets include the step "simply publish this to npm and you can use it like you would any preset". 您会注意到, 用于创建预设的babel-handbook文档包括“简单地将其发布到npm并且您可以像使用任何预设一样使用它”的步骤。 If you want Babel to actually find a preset in the conventional way, there is no alternative to using an npm package inside of the node_modules directory with a name starting with babel-preset . 如果你想让Babel以传统方式实际找到预设,除了在node_modules目录中使用名称以babel-preset开头的npm包node_modules

    This means that we need to create a .babelrc file in the Rails root, and then use the full path to each plugin that we need, relative to that .babelrc file, rather than the short-hand that you'll be familiar with if you've used Babel in the past (eg "presets": ["react"] refers to node_modules/babel-preset-react ). 这意味着我们需要在Rails根目录中创建一个.babelrc文件,然后使用我们需要的每个插件的完整路径,相对于.babelrc文件,而不是你将熟悉的简写。你过去曾经使用过Babel(例如"presets": ["react"] node_modules/babel-preset-react "presets": ["react"]指的是node_modules/babel-preset-react )。 My .babelrc , following the Jest docs on using Webpack 2 and using babel looks like this: 我的.babelrc ,关于使用Webpack 2使用babel的Jest文档看起来像这样:

     // .babelrc { "presets": [ "./vendor/node_modules/babel-preset-react", "./vendor/node_modules/babel-preset-es2015" ], "env": { "test": { "plugins": [ "./vendor/node_modules/babel-plugin-transform-es2015-modules-commonjs" ] } } } 

    Remove the options key under the babel-loader configuration in config/webpack/shared.js so that Webpack uses this configuration file, too. 删除config/webpack/shared.js babel-loader配置下的options键,以便Webpack也使用此配置文件。

That brings us up to the present moment. 这让我们到了现在。 I implemented the basic sum (vanilla JS) and Link (React) tests in the Jest docs and run them by executing npm run test in the /vendor directory. 我在Jest文档中实现了基本求和(vanilla JS)和Link (React)测试,并通过在/vendor目录中执行npm run test来运行它们。 You can see the fruits of my labors here: https://github.com/pstjean/webpacker-jest 你可以在这里看到我工作的成果: https//github.com/pstjean/webpacker-jest

Part 2. Why? 第2部分。为什么

This is not an easy thing to do right now. 现在这不是一件容易的事。 It should be. 它应该是。 The root of our problems is the unconventional directory structure imposed by Webpacker. 我们问题的根源是Webpacker强加的非常规目录结构。

The original reasoning for moving all of our Yarn and Webpack files under /vendor is that, according to DHH, having these things in the project root as is convention is not "a beautiful way to cohabit app-focused JS within a Rails app". 根据DHH的说法,将所有Yarn和Webpack文件移动到/vendor下的原始推理是,在项目根目录中具有这些东西并不是“在Rails应用程序中共享以应用为中心的JS的一种美妙方式”。 You can see the discussion on the commit implementing this here . 您可以在此处查看有关实现此提交的提交的讨论。 User lemonmade's comment on this commit foretells our current frustrations: "It will make actually configuring any JavaScript tooling extremely difficult, and causes it to behave differently than it would in any other project that uses npm packages." 用户lemonmade对此提交的评论预示着我们当前的挫折:“它将使实际配置任何JavaScript工具变得非常困难,并使其行为与使用npm包的任何其他项目中的行为不同。”

Downthread, DHH says "Appreciate the input and will keep it under advisement as we go forward. This isn't set in stone, but for now we're going to try to make this work." DH先生表示,“我们向前推进,并将继续提供建议。这不是一成不变的,但现在我们将努力做到这一点。” In my opinion, this isn't tenable, and hopefully the Rails team will realize that before 5.1 launches with this in core. 在我看来,这是不可行的,并且希望Rails团队能够在5.1启动之前意识到这一点在核心。 There is currently a very promising pull request to remedy this issue in the webpacker project. 目前有一个非常有希望的拉取请求来解决webpacker项目中的这个问题。 Let's hope it gets some traction! 让我们希望它有所吸引力!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM