2019-02-28

2019-02時点でJestの設定で最後までハマったもの

Vue cli 3 はよいという話を以前書いたんだけど、Jest でテストできるようになるまで一つだけすごくハマったものがあって、それは Babel runtime の除外設定。

ちなみにバージョンは以下の通り。

  • @babel/core 7.3.3
  • jest 24.1.0

エラーはこういうもので、

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:

  • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatters" in your config.
  • If you need a custom transformation specify a "transform" option in your config.
  • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs: https://jestjs.io/docs/en/configuration.html

Details:

xxx/node_modules/@babel/runtime-corejs2/helpers/esm/classCallCheck.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest)
{export default function _classCallCheck(instance, Constructor) {
 ^^^^^^

babel-jest 経由で Babel を通したコードなのになぜか ESM版の runtime-corejs2 というコードを通るので、その中に export があって死ぬという怪奇現象。

で、今となってはどこをどう調べたか分からないんだけど、自分が1月に作った設定では以下の記述を足して回避していた。

transformIgnorePatterns: ["/node_modules/(?!@babel/runtime-corejs2)"]

恐らく Babel 7 + Jest ではこういう落とし穴がちょこちょこあるのだろう。

transformIgnore したら export を回避できるというのもよく分からん…。(そしてなぜこうしようと思ったのだ、過去の自分よ)

もしかしたら

Configuring Jest &#183; Jest

transformIgnorePatterns のデフォルト値が <rootDir> 含んでなくてプロジェクト内の node_modules を ignore し忘れてるってことだから明示的に設定したのかな。いやでも新しく足したのも <rootDir> 入ってないけど…。分かってるんならよりよいデフォルトに変えてほしい。

ちなみに最小設定は

  • rootDir
  • moduleNameMapper ( Webpack の alias 相当 )
  • transformIgnorePatterns

でイケるっぽい。

About

例によって個人のなんちゃらです

Recent Posts

Categories

Tool 日々 Web Biz Net Apple MS ことば News Unix howto Food PHP Movie Edu Community Book Security Text TV Perl Ruby Music Pdoc 生き方 RDoc ViewCVS CVS Rsync Disk Mail FreeBSD Cygwin PDF Photo Zebedee Debian OSX Comic Cron Sysadmin Font Analog iCal Sunbird DNS Linux Wiki Emacs Thunderbird Sitecopy Terminal Drawing tDiary AppleScript Life Money Omni PukiWiki Xen XREA Zsh Screen CASL Firefox Fink zsh haXe Ecmascript PATH_INFO SQLite PEAR Lighttpd FastCGI Subversion au prototype.js jsUnit Apache Trac Template Java Rhino Mochikit Feed Bloglines CSS del.icio.us SBS qwikWeb gettext Ajax JSDoc Rails HTML CHM EPWING NDTP EB IE CLI ck ThinkPad Toy WSH RFC readline rlwrap ImageMagick epeg Frenzy sysprep Ubuntu MeCab DTP ERD DBMS eclipse Eclipse Awk RD Diigo XAMPP RubyGems PHPDoc iCab DOM YAML Camino Geekmonkey w3m Scheme Gauche Lisp JSAN Google VMware DSL SLAX Safari Markdown Textile IRC Jabber Fastladder MacPorts LLSpirit CPAN Mozilla Twitter OpenFL Rswatch ITS NTP GUI Pragger Yapra XML Mobile Git Study JSON VirtualBox Samba Pear Growl Mercurial Rack Capistrano Rake Win RSS Mechanize Sitemaps Android JavaScript Python RTM OOo iPod Yahoo Unicode Github iTunes God SBM friendfeed Friendfeed HokuUn Sinatra TDD Test Project Evernote iPad Geohash Location Map Search Simplenote Image WebKit RSpec Phone CSV WiMAX USB Chrome RubyKaigi RubyKaigi2011 Space CoffeeScript Nokogiri Hpricot Rubygems jQuery Node GTD CI UX Design VCS Kanazawa.rb Kindle Amazon Agile Vagrant Chef Windows Composer Dotenv PaaS Itamae SaaS Docker Swagger Grape WebAPI Microservices OmniAuth HTTP 分析基盤 CDN Terraform IaaS HCL Webpack Vue.js BigQuery Middleman CMS AWS PNG Laravel Selenium OAuth OpenAPI GitHub UML GCP TypeScript SQL Hanami Document SVG AsciiDoc Pandoc DocBook Develop Jekyll macOS Node.js Vite Heroku Transformer AI Data Cloud Wasm