<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Emil</title>
    <description>The latest articles on DEV Community by Emil (@cawabunga).</description>
    <link>https://dev.to/cawabunga</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F235550%2F44a2338a-7acb-4f63-bef5-b0dc204f3799.jpg</url>
      <title>DEV Community: Emil</title>
      <link>https://dev.to/cawabunga</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cawabunga"/>
    <language>en</language>
    <item>
      <title>Yarn Plug'n'Play and Karma</title>
      <dc:creator>Emil</dc:creator>
      <pubDate>Tue, 24 Sep 2019 13:19:33 +0000</pubDate>
      <link>https://dev.to/cawabunga/yarn-plug-n-play-and-karma-omi</link>
      <guid>https://dev.to/cawabunga/yarn-plug-n-play-and-karma-omi</guid>
      <description>&lt;p&gt;After turning on Plug'n'Play feature on the Yarn, I faced a problem with running our javascript tests. Strictly speaking, Karma couldn't run at all. &lt;/p&gt;

&lt;p&gt;Console was saying:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./node_modules/.bin/karma: No such file or directory
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I quickly realised that I call binary directly by the path, but &lt;code&gt;node_modules&lt;/code&gt; is empty, so I need to get new path. That wasn't a big problem, just to get a path of an executable: &lt;code&gt;yarn bin karma&lt;/code&gt;, and for its running simply: &lt;code&gt;yarn run karma&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then I got running Karma, but not tests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ERROR [preprocess]: Can not load "webpack", it is not registered!
ERROR [karma-server]: Server start failed on port 9878: Error: No provider for "framework:jasmine"!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here is a slice of configuration file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// karma.config.js:&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="p"&gt;...&lt;/span&gt;
   &lt;span class="nx"&gt;browsers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Chrome&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
   &lt;span class="nx"&gt;frameworks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jasmine&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
   &lt;span class="nx"&gt;preprocessors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app/javascript/**/*.test.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;webpack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
   &lt;span class="p"&gt;},&lt;/span&gt;
   &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After doing some research in the web I didn't find any working solution. So I've opened source code of Karma and its dependency injection library. I've found that Karma loads its plugins on the fly by scanning all available plugins (preprocessors, testing frameworks, browser launchers etc.). (If I were more attentive and &lt;a href="https://karma-runner.github.io/4.0/config/plugins.html"&gt;read the documentation&lt;/a&gt; properly there would be no problems.) But Yarn with enabled PnP has different directory structure, so Karma doesn't see any plugins. &lt;/p&gt;

&lt;p&gt;The thing we just need to do is specify plugins strictly in the configuration and add them to your &lt;code&gt;package.json&lt;/code&gt; as dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// karma.config.js:&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="nx"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;karma-jasmine&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;karma-webpack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;karma-chrome-launcher&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;By enabling Plug'n'Play be more careful with working with relative paths of libraries. And don't forget to specify all your explicit and implicit dependencies in your &lt;code&gt;package.json&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>yarn</category>
      <category>karma</category>
      <category>webpack</category>
    </item>
  </channel>
</rss>
