Angular TestBed - Karma/Jasmine | Angular TestBed导入了意外值 'BrowserDynamicTestingModule' | Angular AOT

标签 angular karma-webpack

测试台代码示例,遵循指南 https://angular.io/guide/testing -

describe("Login", () => {
  let loginService: LoginService;

  beforeEach(() => {
    TestBed.configureTestingModule(
        { providers: [LoginService] }
    );

    loginService = TestBed.get(LoginService); // ERROR
  });

  it("login testing", (done: DoneFn) => {}); // Empty test
}

抛出错误 -

        Error: Unexpected value 'BrowserDynamicTestingModule' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.
        at syntaxError (webpack-internal:///1394:723:34)
        at eval (webpack-internal:///1394:15544:48)
        at Array.forEach (<anonymous>)
        at CompileMetadataResolver.getNgModuleMetadata (webpack-internal:///1394:15519:53)
        at JitCompiler._loadModules (webpack-internal:///1394:34773:74)
        at JitCompiler._compileModuleAndAllComponents (webpack-internal:///1394:34751:40)
        at JitCompiler.compileModuleAndAllComponentsSync (webpack-internal:///1394:34639:46)
        at CompilerImpl.compileModuleAndAllComponentsSync (webpack-internal:///1409:264:58)
        at TestingCompilerImpl.compileModuleAndAllComponentsSync (webpack-internal:///1408:333:35)
        at TestBed._initIfNeeded (webpack-internal:///1393:998:36)
        at TestBed.get (webpack-internal:///1393:1069:14)
        at Function.TestBed.get (webpack-internal:///1393:854:29)
        at Object.eval (webpack-internal:///1413:10:42)
        at ZoneDelegate.invoke (webpack-internal:///781:392:26)
        at ProxyZoneSpec.onInvoke (webpack-internal:///1403:81:43)
        at ZoneDelegate.invoke (webpack-internal:///781:391:32)
        at Zone.run (webpack-internal:///781:142:43)
        at Object.eval (webpack-internal:///1405:96:38)
        at ZoneQueueRunner.jasmine.QueueRunner.ZoneQueueRunner.execute (webpack-internal:///1405:126:46)
        at ZoneQueueRunner.jasmine.QueueRunner.ZoneQueueRunner.execute (webpack-internal:///1405:126:46)
        at eval (webpack-internal:///1405:123:134)
        at ZoneDelegate.invokeTask (webpack-internal:///781:425:31)
        at Zone.runTask (webpack-internal:///781:192:47)
        at drainMicroTaskQueue (webpack-internal:///781:602:35)

Karma 配置文件 -

var webpackConfig = require('./config/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: {
        logLevel: 'warn',
        stats: 'errors-only',
        chunks: false
    },

    webpackServer: {
        noInfo: true
    },

    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: false,
    browsers: ['ChromeHeadless'],
    singleRun: false,
    concurrency: Infinity
};

config.set(_config);
};

业力希姆 -

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

// setup angular test bed
var testing = require('@angular/core/testing');
var browser = require('@angular/platform-browser-dynamic/testing');
testing.TestBed.initTestEnvironment(browser.BrowserDynamicTestingModule,                             
browser.platformBrowserDynamicTesting());

// setup app context
var appContext = require.context('./scripts', true, /\.spec\.ts/);
appContext.keys().forEach(appContext);

Webpack 开发 -

// remove common chunks which can cause an issue for unit testing - jsonP
const commonsChunkPluginIndex = commonConfig.plugins.findIndex(plugin => 
plugin.chunkNames);
commonConfig.plugins.splice(commonsChunkPluginIndex, 1);

module.exports = webpackMerge(commonConfig, {
devtool: "cheap-module-eval-source-map",

output: {
    path: helpers.root("wwwroot"),
    filename: "[name].js",
    chunkFilename: "[id].chunk.js"
},

plugins: [
  new ExtractTextPlugin("[name].css")
],

devServer: {
    historyApiFallback: true,
    stats: "minimal"
}
});

Webpack 常用配置 -

module.exports = {
entry: {
    'polyfills': './scripts/polyfills.ts',
    'vendor': './scripts/vendor.ts',
    'app': './scripts/boot.ts'
},

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

module: {
    rules: [
      {
          test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
          loader: '@ngtools/webpack'
      },
      {
          test: /\.js$/,
          loader: '@angular-devkit/build-optimizer/webpack-loader',
          options: {
            sourceMap: false
          }
      },
      {
          test: /\.html$/,
          loader: 'html-loader',
          query: {
              interpolate: 'require'
          }
      },
      {
          test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
          use: [
              {
                  loader: 'url-loader',
                  options: {
                      limit: 100000
                  }
              }
          ]
      },
      {
          test: /\.scss$/,
          loaders: ['style-loader', 'css-loader', 'resolve-url-loader', 'sass-loader?sourceMap']
      },
      {
          test: /\.css$/,
          exclude: helpers.root('scripts', 'Components'),
          loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader?sourceMap' })
      },
      {
          test: /\.css$/,
          include: helpers.root('scripts', 'Components'),
          loader: 'raw-loader'
      }
    ]
},

plugins: [
    new webpack.optimize.CommonsChunkPlugin({
        name: ['app', 'vendor', 'polyfills']
    }),

    new HtmlWebpackPlugin({
        template: './wwwroot/index.html'
    }),

    new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        "window.jQuery": "jquery"
    }),

    new PurifyPlugin(),

    new AotPlugin({
        tsConfigPath: './tsconfig.json',
        entryModule: './scripts/app.module#AppModule',
        mainPath: './scripts/boot.ts',
        sourceMap: true
    })
]
};

Boot.ts,因为它是唯一包含与 BrowserDynamicTestingModule 相关的任何内容的其他文件 -

import { enableProdMode }           from "@angular/core";
import { platformBrowserDynamic }   from "@angular/platform-browser-dynamic";
import { AppModule }                from "./app.module";
import { isProd, isCordova }        from "../globalFn";

if (isProd()) {
    enableProdMode();
}

// mobile app (TODO) must wait for the device to be ready before starting up
if (isCordova()) {
    document.addEventListener("deviceready", () => {
        platformBrowserDynamic().bootstrapModule(AppModule);
    }, false);
}
else {
    platformBrowserDynamic().bootstrapModule(AppModule);
}

我目前正在运行 Angular AOT 5.2.0。我尝试恢复到 JIT 构建,但遇到了同样的错误。现在已经被困在这个问题上一段时间了,任何有关导致此问题的原因的帮助或见解将不胜感激。

我可以正常编译并运行构建

最佳答案

我放弃了让 AOT 工作,也许有人知道这是否可能。我只是完全求助于 JIT 构建来进行测试。之前的 JIT 构建不起作用,因为 Awesome-ts-loader 已更新。

关于Angular TestBed - Karma/Jasmine | Angular TestBed导入了意外值 'BrowserDynamicTestingModule' | Angular AOT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50027969/

相关文章:

html - 未出现具有百分比高度的 ngx-infinite-scroll

Angular 2 : two way binding inside parent/child component

angular - 使用业力和 webpack 4 运行单元测试时无法读取未定义的属性 'externalModuleIndicator'

angular - 无法创建 KeyboardEvent 来测试 Internet Explorer 11 中的输入组件( Angular )

javascript - Angular 2 : How to capture when browser restore/maximize

javascript - Angular 通用 : "Project could not be found in workspace."

angular - 在 Angular 4 的 ContentChildren 内的列上使用 Kendo 网格过滤器

javascript - 使用 webpack 进行 Karma 测试

angular - Karma 在阅读 typescript 时抛出 "Module parse failed"