mindparse
mindparse

Reputation: 7225

How to include vendor bundles from webpack for use in karma test runner

I have an AngularJS 1.x project written in TypeScript and am using Webpack. I am now trying to setup the karma-typescript module with an example test I have written for one of my services.

I followed the sample karma config file from here, so have this right now:

module.exports = function (config) {
  config.set({
    basePath: '../',
    frameworks: ['jasmine', 'karma-typescript'],
    files: [
            //Add library includes here...
      'app/**/*.ts'
    ],
    exclude: [
    ],
    preprocessors: {
      'app/**/*.ts': ['karma-typescript']
    },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['PhantomJS'],
    singleRun: false,
    concurrency: Infinity
  })
}

When I run my test task, I get an error saying angular cannot be found. I realise why, since in my karma config file I have not yet added in any of the libraries needed for my application. Rather than adding these in individually, is there a way I can get karma to load in a vendor bundle file that my webpack config file is setup to build?:

const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');
const copy = require('copy-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
  entry: {
    main: './app/app.module.ts',
    vendor: [
      'jquery',
      'angular',
      'angular-animate',
      'angular-aria',
      'angular-cookies',
      'angular-material',
      'angular-messages',
      'angular-mocks',
      'angular-resource',
      'angular-sanitize',
      'angular-ui-bootstrap',
      'angular-ui-router',
      'angular-local-storage',
      'bootstrap',
      'less',
      'lodash',
      'moment',
      'ui-select',
      'leaflet',
      'iso-currency',
      'angular-leaflet-directive'
    ],
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].bundle.js',
  },
  module: {
    rules: [
      {
        test: require.resolve('jquery'),
        use: [{
          loader: 'expose-loader',
          options: '$'
        }]
      }, {
        test: /\.less$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader', 'less-loader']
        })
      }, {
        test: require.resolve('moment'),
        use: [{
          loader: 'expose-loader',
          options: 'moment'
        }]
      },
      {
        test: /\.(png|woff|woff2|eot|ttf|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: 'url-loader'
      },
      {
        enforce: 'pre',
        test: /\.tsx?$/,
        use: 'source-map-loader'
      },
      {
        test: /\.ts?$/,
        exclude: /node_modules/,
        use: 'ts-loader'
      }
    ]
  },
  resolve: {
    extensions: ['.ts', '.js']
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      minChunks: Infinity
    }),
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: 'jquery',
      'window.jQuery': 'jquery'
    }),
    new ExtractTextPlugin('./app.css'),
    new CleanWebpackPlugin(['dist']),
    new copy([
      { from: 'app' },
      { from: 'index.html' },
      { from: 'app/assets/fonts', to: 'assets/fonts' },
      { from: 'app/assets/iln18Support', to: 'assets/iln18Support' },
      { from: 'app/assets/images', to: 'assets/images' },
      { from: 'app/partials', to: 'partials' }
    ])
  ]
};

So is there a way I can tell karma to include my vendor.bundle.js file output by webpack?

I am struggling to find good examples\documentation so if anyone can refer me to any that will help me here, I would be most grateful!

Thanks

Upvotes: 4

Views: 1341

Answers (1)

ofhouse
ofhouse

Reputation: 3256

Solved it by creating a new file called app/vendor.bundle.ts.

Instead of importing all external vendors through the webpack config I now have them in the bundle file.

The vendor.bundle.ts file itself looks something like this:
(I also think the approach to seperate the vendors from the webpack config keeps the webpack config more clean here)

import 'jquery';
import 'angular';
import 'angular-animate';
import 'angular-aria';
...

So in my webpack config I only have one entry point now, like this:

module.exports = {
  entry: {
    vendor: './app/vendor.bundle.ts',
    main: './app/app.module.ts',
  }
}
...

And in my karma.conf.js I can now import the vendor as a single entry point like this (Note that my app is also an single entry point here):

module.exports = function (config) {
  config.set({
    files: [
      // Vendors
      'app/vendor.bundle.ts',
      // App
      'app/app.module.ts',

      // Load tests
      'app/**/*spec.ts'
    ],
    preprocessors: {
      'app/**/*.ts': ['karma-typescript']
    },
    ...
  })
}

Upvotes: 2

Related Questions