Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: index_1.X is not a constructor - Karma - Jasmine - Webpack

I'm trying to run a simple unit test in angular2+webpack using jasmine and karma. Below is my test:

it('my test', () => {
    let myObject = new MyObject();
    myObject.name = 'My Name';
    expect(myObject.name).toBe('My Name');
});

When running I receive the following error:

TypeError: index_1.MyObject is not a constructor

I also tried simple javascript type with an enum type defined in my project:

it('my test', () => {
    let myObject = {
        name = 'My Name',
        hairColor = HairColor.Brown
    }
    expect(myObject.name).toBe('My Name');
});

In this case, I receive the following error:

TypeError: Cannot read property 'Brown' of undefined

This is my object and enum type:

export class MyObject {
    name: string;
    family: string;
    hairColor: HairColor;
}

export enum HairColor {
    Brown,
    Black
}

Looks like that webpack does not provide the correct output js files to karma. Below is my config files:

webpack.test.js

var webpack = require('webpack');
var helpers = require('./helpers');

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

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

  module: {
    rules: [
      {
        test: /\.ts$/,
        loaders: [
          {
            loader: 'awesome-typescript-loader',
            options: { configFileName: helpers.root('src', 'tsconfig.json') }
          } , 'angular2-template-loader', 'angular-router-loader'
        ]
      },
      {
        test: /\.html$/,
        loader: 'html-loader'

      },
      {
        test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
        loader: 'null-loader'
      },
      {
        test: /\.css$/,
        exclude: helpers.root('src', 'app'),
        loader: 'null-loader'
      },
      {
        test: /\.css$/,
        include: helpers.root('src', 'app'),
        loader: 'raw-loader'
      }
    ]
  },

  plugins: [
    new webpack.ContextReplacementPlugin(
      // The (\\|\/) piece accounts for path separators in *nix and Windows
      /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
      helpers.root('./src'), // location of your src
      {} // a map of your routes
    )
  ]
}

karma.config.js

var webpackConfig = require('./webpack.test');

module.exports = function (config) {
  var _config = {
    basePath: '',

    frameworks: ['jasmine'],

    files: [
      {pattern: './karma-test-shim.js', watched: false}
    ],

    preprocessors: {
      './karma-test-shim.js': ['webpack', 'sourcemap']
    },

    webpack: webpackConfig,

    webpackMiddleware: {
      stats: 'errors-only'
    },

    webpackServer: {
      noInfo: true
    },

    reporters: ['kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  };

  config.set(_config);
};

karma-test-shim.js

Error.stackTraceLimit = Infinity;

require('core-js/es6');
require('core-js/es7/reflect');

require('zone.js/dist/zone');
require('zone.js/dist/long-stack-trace-zone');
require('zone.js/dist/proxy');
require('zone.js/dist/sync-test');
require('zone.js/dist/jasmine-patch');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');

var appContext = require.context('../src', true, /\.spec\.ts/);

appContext.keys().forEach(appContext);

var testing = require('@angular/core/testing');
var browser = require('@angular/platform-browser-dynamic/testing');

testing.TestBed.initTestEnvironment(browser.BrowserDynamicTestingModule, browser.platformBrowserDynamicTesting());
like image 344
hosjay Avatar asked Apr 06 '17 05:04

hosjay


1 Answers

The issue was that I was importing MyObject and HairColor from the Barrel! Like the following:

import { HairColor } from '../index';

I changed it to import from its own file and the issue is now solved!

import { HairColor } from '../enums/hair-color.enum';
like image 86
hosjay Avatar answered Sep 30 '22 04:09

hosjay