簡體   English   中英

如何在 Angular 和 AngularJs 混合應用程序中運行測試?

[英]How to run tests in Angular & AngularJs hybrid app?

由於我想將我的 AngularJs 應用程序遷移到 Angular 7,我使用 ngUpgrade 創建了一個混合應用程序。 舊的 AngularJs 應用程序現在嵌入在新的 Angular 應用程序中。

當我運行ng test它會觸發我的 Angular 測試,這很好。 但我也想運行用 AngularJs 編寫的舊測試。

目前我的 Karma 配置如下所示:

'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 配置來創建一個新的 karma webpack 配置。 現在,當我通過ng test命令運行它時,我什至不能在 karma 配置的開頭require './karma.webpack.config' (我得到Invalid config file! TypeError: require(...) is not a function )。

我應該如何處理這種情況以運行所有測試?

我最終通過單獨運行測試解決了這個問題,但使用一個 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",

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM