【问题标题】:Trouble configuring karma proxy at website root在网站根目录配置 karma 代理时遇到问题
【发布时间】:2020-10-06 21:26:53
【问题描述】:

我无法将 karma 配置为代理网站根目录中的文件。我的 Angular 组件之一包含一个嵌入式 Ace 编辑器,它在 http://localhost:9876/worker-html.js 中查找文件。我已经使用 files 属性成功地在 Karma 中提供了文件

// Makes ace files available in tests
files: [
  { pattern: "../node_modules/ace-builds/src-min-noconflict/worker-html.js", watched: false, included:false, nocache:false, served:true }
],

并验证它是否已提供服务(通过检查 window.__karma__,它并不总是在 karma 浏览器窗口中定义,我不知道为什么,但这是一个单独的问题)。我还验证了我可以在这个路径访问所需的文件:

http://localhost:9876/absoluteD:/Evan/programming%20stuff/Projects/Gneus/node_modules/ace-builds/src-min-noconflict/worker-html.js

但是,我终其一生都无法将其代理到所需的 URL (http://localhost:9876/worker-html.js)。我已经尝试了所有我能想到的合理代理配置,所以我认为我缺少一些基本的东西。有人可以帮我吗?这是整个 karma 配置文件供参考,包括我最近的代理尝试:

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    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')
    ],
    client: {
      clearContext: false, // leave Jasmine Spec Runner output visible in browser

      // Don't run tests in random order
      // If you update this, you have to restart ng test batch
      jasmine: {
        random: false
      }
    },

    // Makes ace files available in tests
    files: [
      { pattern: "../node_modules/ace-builds/src-min-noconflict/worker-html.js", watched: false, included:false, nocache:false, served:true }
    ],
    proxies: {
      '/worker-html.js': "http://localhost:9876/absoluteD:/Evan/programming stuff/Gneus/node_modules/ace-builds/src-min-noconflict/worker-html.js"
    },

    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, '../coverage'),
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,

    
  });
};

【问题讨论】:

标签: angular proxy jasmine karma-jasmine karma-runner


【解决方案1】:

约翰在上面评论中的链接 (dev.to/jwp/angular-karma-proxy-configuration-1kbb) 引导我找到正确的代理配置。我的问题有两个方面:我不了解代理对象的各个部分,并且我复制的链接有误(经典)。以下是工作配置文件的相关部分,供任何可能有帮助的人使用:

basePath: '',
...

// Makes ace files available in tests
files: [
  { pattern: "../node_modules/ace-builds/src-min-noconflict/worker-html.js", watched: false, included:false, nocache:false, served:true }
],
proxies: {
  "/worker-html.js": { 
    "target": "http://localhost:9876/absoluteD:/Evan/programming stuff/Projects/Gneus/node_modules/ace-builds/src-min-noconflict/worker-html.js"
  }
},

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2017-04-12
    • 2020-04-09
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多