1

As I want to migrate my AngularJs application to Angular 7, I created a hybrid app using ngUpgrade. The old AngularJs app is now embedded in the new Angular one.

When I run ng test it fires my Angular tests which is fine. But I also want to run the old tests written in AngularJs.

At the moment my Karma config looks like:

'use strict';

const webpackConfig = require('./karma.webpack.config');

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular', 'es6-shim'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma'),
      require('karma-es6-shim'),
      require('karma-junit-reporter'),
      require('karma-webpack'),
    ],
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, 'coverage'),
      reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true
    },

    mime: {
      'text/x-typescript': ['ts','tsx']
    },

    exclude: [
      '**/*.html'
    ],

    preprocessors: {
      './karma.entrypoint.ts': ['webpack']
    },

    webpack: webpackConfig,

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

    junitReporter : {
      // results will be saved as $outputDir/$browserName.xml
      outputDir : 'build/test-results/test/'
    },

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

The ./karma.webpack.config file is:

    const config = require('./webpack.config.js')();
    const webpack = require('webpack');
    const path = require('path');
    const nibStylusPlugin = require('nib');
    const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

    module.exports = Object.assign({}, config, {
       context: path.resolve(__dirname, '.'),
       entry: {
        test: './karma.entrypoint.ts'
       },
       mode: 'development',
       devtool: 'cheap-module-inline-source-map',
       optimization: {
         splitChunks: false,
         runtimeChunk: false
       },
       plugins: [
         new ForkTsCheckerWebpackPlugin(),
         new webpack.ProvidePlugin({
           "window.jQuery": "jquery",
           tv4: "tv4"
         }),
         new webpack.LoaderOptionsPlugin({
           options: {
             stylus: {
               use: [nibStylusPlugin()],
               import: ['~nib/lib/nib/index.styl'],
             }
           }
         })
       ]
     });

And last but not least the karma.entrypoint.ts:

    import 'jquery';
    import 'angular';
    import 'angular-mocks';
    import * as moment from 'moment';
    import{appConfigMock} from './karma.app-mock.config';

    window['__APP_CONFIG__'] = appConfigMock;
    (<any>moment).suppressDeprecationWarnings = true;

    const testsContext = (<any>require).context('./src/app/', true, /\.spec\.ts$/);
    testsContext.keys().forEach(testsContext);

Before, when I had only the AngularJs app, I run the tests with a very similar structure by using karma command. It utilized my webpack config to create a new karma webpack config. Now, when I run it through the ng test command, I even cannot require the './karma.webpack.config' at the beginning on karma configuration (I get Invalid config file! TypeError: require(...) is not a function).

How should I approach this case to run all the tests?

Hubert Kubiak
  • 607
  • 1
  • 7
  • 30

1 Answers1

0

I finally solved it by running the tests separately but with one npm command, npm run test:


    "test-ng1": "cross-env APP_CONFIG=testing NODE_ENV=development karma start --single-run",
    "test-ng2-apps": "ng test --watch=false --ts-config=./karma.tsconfig.json",
    "test": "yarn test-ng2-apps && yarn test-ng1",
Hubert Kubiak
  • 607
  • 1
  • 7
  • 30