title: Universal JS for browser and nodeJS along with unit tests
body: <p>I want to build a codebase that's both functional and testable on nodeJS and the browser. </p>
  
  <p>I've covered the nodeJS environment here with the following code and attempted for the browser, what's missing is how to make the same files work for the browser along with unit tests (without duplicating code) and while taking care of the transpiring with babel and webpack.</p>
  
  <ol>
  <li>Run Browser + NodeJS</li>
  <li>Unit Tests browser+nodeJS</li>
  </ol>
  
  <p><strong>src/Vehicle.js</strong></p>
  
  <pre><code>class Vehicle {
      constructor() {
          this.wheels = 4;
      }
  
      getWheels() {
          return this.wheels;
      }
  }
  
  module.exports = Vehicle; 
  // if (module != undefined) module.exports = Vehicle;
  // if (window != undefined) window.Vehicle = Vehicle;
  </code></pre>
  
  <p><strong>src/Truck.js</strong></p>
  
  <pre><code>const Vehicle = require("./Vechicle");
  
  class Truck extends Vehicle { 
      constructor() {
          super();
          wheels = 16;
      }
  }
  
  module.exports = Truck;
  </code></pre>
  
  <p><strong>webpack.config.js</strong> This doesn't work. (need both minified and non-minified)</p>
  
  <pre><code>module.exports = {
      module: {
          entry: "src/**",
          output: {
              filename: [
                  "../dist/bundle.min.js" // with sources maps
                  "../dist/bundle.js" // non minified
              ]
          }
          rules: [
              {
                  test: /\.js$/,
                  exclude: [/node_modules/, "test"],
                  use: {
                      loader: "babel-loader"
                  }
              }
          ]
      }
  };
  </code></pre>
  
  <hr>
  
  <p><strong>Tests</strong> (nodeJS with mocha)</p>
  
  <p><strong>test/VehicleTest.js</strong></p>
  
  <pre><code>describe("Vehicle Test", function () {
      const assert = require("assert");
      it("should test Vehicle wheels", function () {
          const Vehicle = require("../src/Vechicle");
          let v = new Vehicle();
          assert(v.getWheels() == 4);
      });
  });
  </code></pre>
  
  <p><strong>test/TruckTest.js</strong></p>
  
  <pre><code>describe("Truck Test", function () {
      const assert = require("assert");
      it("should test Truck Wheels", function () {
          const Truck = require("../src/Truck");
          let t = new Truck();
          assert(t.getWheels() == 16);
      });
  });
  </code></pre>
  
  <hr>
  
  <p><strong>Tests</strong> (browser)</p>
  
  <p><strong>test/index.html</strong> Browser Test </p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;Title&lt;/title&gt;
      &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/6.1.19/browser.js"&gt;&lt;/script&gt;
  
      &lt;script src="../dist/bundle.min.js"&gt;&lt;/script&gt; &lt;!-- all files from the src folder --&gt;
  
      &lt;script src="VehicleTest.js"&gt;&lt;/script&gt;
      &lt;script src="TruckTest.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>