lundi 8 août 2016

Unit testing an Angular2 service that requires Electron

I'm working on an Electron app, using Angular2 (rc4) and Webpack. Unit test are ran with Karma.

I have a service that communicates with the electron process, which includes it in this way:

import { remote } from 'electron';

@Injectable()
export class SettingsService {
    main = remote.require('./main.js');
    settings: any;
    constructor() {
        this.settings = this.main.getSettings();
    }
}

When I want to write unit tests for this file, the karma runner throws this error:

Error: Cannot find module "electron"
  at webpack:///src/app/services/settings.service.ts:9:0 <- spec-bundle.js:88524

If I add target:'electron-renderer' to the webpack.test.js config file, the error changes to:

ReferenceError: Can't find variable: require
at webpack:/external%20%22electron%22:1:0 <- spec-bundle.js:83860

This is my webpack.test.js file:

const helpers = require('./helpers');

module.exports = {
  devtool: 'inline-source-map',

  resolve: {
    extensions: ['', '.ts', '.js']
  },

  module: {
    loaders: [
      {
        test: /\.ts$/,
        loader: 'ts'
      },
      {
        test: /\.html$/,
        loader: 'html'

      },
      {
        test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
        loader: 'null'
      },
      {
        test: /\.css$/,
        loader: 'null'
      }
    ],

    postLoaders: [

      {
        test: /\.(js|ts)$/, loader: 'istanbul-instrumenter-loader',
        include: helpers.root('src'),
        exclude: [
          /\.(e2e|spec)\.ts$/,
          /node_modules/
        ]
      }
    ]
  },
  target:'electron-renderer'
}

Question is: How do I run unit tests on an Angular2 component that communicates with the Electron process.

Aucun commentaire:

Enregistrer un commentaire