1

AngularJs アプリケーションを Angular 7 に移行したいので、ngUpgrade を使用してハイブリッド アプリを作成しました。古い AngularJs アプリは新しい Angular アプリに組み込まれました。

実行するng testと、Angular テストが開始されますが、これは問題ありません。しかし、AngularJs で書かれた古いテストも実行したいと考えています。

現時点で、カルマの設定は次のようになります。

'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
  });
}; 

./karma.webpack.configファイルは次のとおりです。

    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'],
             }
           }
         })
       ]
     });

最後になりましたが、重要なことは次の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);

以前、AngularJs アプリしかなかったときは、karma コマンドを使用して、非常によく似た構造でテストを実行しました。私のwebpack構成を利用して、新しいカルマwebpack構成を作成しました。今、コマンドで実行すると、カルマ構成の最初でさえできません(取得しますng test)。require'./karma.webpack.config'Invalid config file! TypeError: require(...) is not a function

すべてのテストを実行するには、このケースにどのようにアプローチすればよいですか?

4

1 に答える 1

0

テストを個別に実行することで最終的に解決しましたが、1つのnpmコマンドを使用し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",
于 2019-11-05T10:28:49.483 に答える