简体   繁体   English

未捕获的错误:模块解析失败:您可能需要合适的加载程序来处理此文件类型

[英]Uncaught Error: Module parse failed:You may need an appropriate loader to handle this file type

I caught this error when run karma test.我在运行业力测试时发现了这个错误。

Uncaught Error: Module parse failed: G:\\demo\\my-ng2-admin\\my-ng2-admin\\my-ng2-admin\\node_modules\\awesome-typescript-loader\\dist\\entry.js?{"configFileName":"G:\\demo\\my-ng2-admin\\my-ng2-admin\\my-ng2-admin\\src\\tsconfig.json"}!G:\\demo\\my-ng2-admin\\my-ng2-admin\\my-ng2-admin\\node_modules\\angular2-template-loader\\index.js!G:\\demo\\my-ng2-admin\\my-ng2-admin\\my-ng2-admin\\src\\app\\pages\\test\\components\\testBanner.component.spec.ts Unexpected token (18:27)未捕获的错误:模块解析失败:G:\\demo\\my-ng2-admin\\my-ng2-admin\\my-ng2-admin\\node_modules\\awesome-typescript-loader\\dist\\entry.js?{"configFileName":" G:\\demo\\my-ng2-admin\\my-ng2-admin\\my-ng2-admin\\src\\tsconfig.json"}!G:\\demo\\my-ng2-admin\\my-ng2-admin\\my- ng2-admin\\node_modules\\angular2-template-loader\\index.js!G:\\demo\\my-ng2-admin\\my-ng2-admin\\my-ng2-admin\\src\\app\\pages\\test\\components\\testBanner .component.spec.ts 意外令牌 (18:27)


 You may need an appropriate loader to handle this file type.
  |     var de;
  |     var el;
  |     beforeEach(function () __awaiter(this, void 0, void 0, function* () {
  |         testing_1.TestBed.configureTestingModule({
  |             declarations: [testBanner_component_1.testBanner]

and more details以及更多细节

//webpack.test.js
var webpack = require('webpack');
var helpers = require('./config/helpers');

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

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

  entry: {
  },
  // entry: {
  //
  //   'polyfills': './src/polyfills.browser.ts',
  //   'vendor': './src/vendor.browser.ts',
  //   'main': './src/main.browser.ts'
  //
  // },

  module: {
    rules: [
      {
        test: /\.ts$/,
        loaders: [
          {
            loader: 'awesome-typescript-loader',
            options: { configFileName: helpers.root('src', 'tsconfig.json') }
          }, 'angular2-template-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.conf.js
var webpackConfig = require('./webpack.test');
module.exports = function(config) {
  config.set({
    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',
    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],
    // list of files / patterns to load in the browser
    files: [
      {pattern: './karma-shim.js', watched: false}
      ],

    // list of files to exclude
    exclude: [
    ],
    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
      './karma-shim.js': ['webpack']
    },
    webpack: webpackConfig,
    webpackMiddleware: {
      stats: 'errors-only'
    },

    webpackServer: {
      noInfo: true
    },
    plugins:[
      'karma-jasmine',
      'karma-chrome-launcher',
      //'karma-sourcemap-loader',
      require("karma-webpack")
    ],
    proxies:{
      //http://localhost:3000/#/pages/strategys/strategytables
      "/app/": "base/src/app"
    },
    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],
    // web server port
    port: 9876,

    // enable / disable colors in the output (reporters and logs)
    colors: true,
    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,

    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,

    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],

    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
   // how many browser should be started simultaneous
    concurrency: Infinity
  })
}

//karma-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());

//testBanner.component.spec.ts
import {} from "@types/jasmine";
import {ComponentFixture, TestBed, ComponentFixtureAutoDetect,async} from"@angular/core/testing";
import { By }              from "@angular/platform-browser";
import { DebugElement }    from "@angular/core";

import { testBanner } from "./testBanner.component";

describe('bannerComponent test',()=> {
  var comp:testBanner;
  var fixture:ComponentFixture<testBanner>;
  var de: DebugElement;
  var el: HTMLElement;

  beforeEach(async()=>{
    TestBed.configureTestingModule({
      declarations:[testBanner],
    }).compileComponents();

    beforeEach(()=>{
      fixture = TestBed.createComponent(testBanner);

      comp = fixture.componentInstance;

      de = fixture.debugElement.query(By.css('h1'));

      el = de.nativeElement;
    });

  });

  it('should display original title', () => {
    fixture.detectChanges();
    expect(el.textContent).toContain(comp.title);
  });

  it('should display a different test title', () => {
    comp.title = 'Test Title';
    fixture.detectChanges();
    expect(el.textContent).toContain('Test Title');
  });

});

This can be helpful for someone who came across this question.这对遇到此问题的人可能会有所帮助。

You need to add following change in your webpack.test.js (webpack config file):您需要在webpack.test.js (webpack 配置文件)中添加以下更改:

try updating loader 'awesome-typescript-loader' like this:尝试像这样更新 loader 'awesome-typescript-loader':

{
   loader: 'awesome-typescript-loader?presets[]=es2017',
   options: { configFileName: helpers.root('src', 'tsconfig.json') }
}

It should solve your problem.它应该可以解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获(承诺):错误:模块解析失败:意外令牌(91:0)您可能需要适当的加载程序来处理此文件类型 - Uncaught (in promise): Error: Module parse failed: Unexpected token (91:0) You may need an appropriate loader to handle this file type Angular 2 - Karma 错误 - 模块解析失败“您可能需要合适的加载程序来处理此文件类型” - Angular 2 - Karma Error - Module Parse Failed 'You may need an appropriate loader to handle this file type' 模块解析失败:您可能需要适当的加载程序来处理此文件类型 - Module parse failed:You may need an appropriate loader to handle this file type 模块分析失败:括号模式您可能需要适当的加载程序来处理此文件类型 - Module parse failed: Parenthesized pattern You may need an appropriate loader to handle this file type 您可能需要适当的加载器来处理此文件类型-带有React的Angular 4 - You may need an appropriate loader to handle this file type - Angular 4 with React 您可能需要适当的加载程序来处理此文件类型。 Angular 5应用程序中的错误 - You may need an appropriate loader to handle this file type. error in Angular 5 app Angular 12 升级错误:您可能需要合适的加载器来处理此文件类型,目前没有配置加载器来处理此文件 - Angular 12 upgrade error: You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file 您可能需要适当的加载程序来处理此文件-.net core和Angular - You may need an appropriate loader to handle this file - .net core & Angular “你可能需要一个适合这种文件类型的加载器”,webpack 无法解析 angular2 文件 - “You may need an appropriate loader for this file type”, webpack can't parse angular2 file index.ngfactory.ts意外的令牌,您可能需要适当的加载程序来处理此文件类型 - index.ngfactory.ts Unexpected token, You may need an appropriate loader to handle this file type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM