javascript 为什么我需要为 karma 配置指定所有文件,即使它们加载了 requirejs

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27907347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:09:22  来源:igfitidea点击:

why do I need to specify all files for karma config even if they are loaded with requirejs

javascriptrequirejskarma-runner

提问by Max Koretskyi

I have the following directory structure:

我有以下目录结构:

--app
  |---dots
  |    |---some.js
  |
  |---entry.js
  |---bootstrap.js
  |---karma.conf.js
  |---test-main.js
  |---test
      |---sampleSpec.js

Here is my sampleSpecdependencies:

这是我的sampleSpec依赖项:

define(["app/bootstrap", "app/dots/some"], function () {}]

So as I understand it I load bootstrapand somefiles into browser using requirejs. However, depending on whether I specify dots/*folder in my karma.conf.jsfile the karma server succeeds or fails to resolve dots/some.jsfile. What I mean is if I specify the following pattern: 'app/**/*.js'in karma.conf.js:

所以,按照我的理解我加载bootstrapsome使用requirejs文件到浏览器中。但是,根据我是否dots/*karma.conf.js文件中指定文件夹,业力服务器成功或无法解析dots/some.js文件。如果我指定以下我的意思是pattern: 'app/**/*.js'karma.conf.js

files: [
  'test-main.js',
  {pattern: 'app/**/*.js', included: false},
  {pattern: 'test/*Spec.js', included: false}
],

The dots/some.jsfile is loaded into browser, if I specify like this pattern: 'app/*.js'karma server returns 404- file not found. Why is it so? Why should karmacare about the path if I load it using requirejs?

dots/some.js文件已加载到浏览器中,如果我指定此pattern: 'app/*.js'karma 服务器返回404- 找不到文件。为什么会这样?karma如果我使用加载它为什么要关心路径requirejs

回答by MarcoL

When you fire karma up, what karma does is:

当你点燃业力时,业力所做的是:

  • It does some pre-process job
  • It creates a webpage where your web assets are loaded (css, js, etc...)
  • It creates a webserver to serve your assets
  • 它做了一些预处理工作
  • 它会创建一个网页,在其中加载您的网络资产(css、js 等...)
  • 它创建了一个网络服务器来为您的资产提供服务

The webserver needs to know where you have your own assets and if you want to serve them straight from the page or load them later.

网络服务器需要知道您在何处拥有自己的资产,以及您是想直接从页面提供它们还是稍后加载它们。

In your karma config file you have several options to configure how you want to load them:

在你的 karma 配置文件中,你有几个选项来配置你想要如何加载它们:

...
files: [
   'test-main.js',
   {pattern: 'app/**/*.js', included: true, watched: false, served: true},
   ...
],

proxies: {
  '/img/': 'http://localhost:8080/base/test/images/'
}

In the filesarray you can put all the resources that you want to be included, watched and served.

在该files数组中,您可以放置​​所有您想要包含、观看和服务的资源。

If you want instead to use a custom URL (say you have a specific route in your app) you can tell karma how to reflect that custom URL to a static URL or simply to to map it (say you're using a third party service).

如果您想改用自定义 URL(假设您的应用程序中有特定路由),您可以告诉 karma 如何将该自定义 URL 反映到静态 URL 或简单地将其映射(假设您正在使用第三方服务) )。

If a file is not mapped there karma won't serve it, so when you're requiring it your request will have an HTTP 404response.
Karma accepts also regexp patterns (minimatch strings) as routes - as specified in the documentation - so your app/**/*.jswill match any js files within appat any level, while app/*.jswill only match JS files strictly inside the appfolder.

如果文件未映射,那么 karma 将不会提供它,因此当您需要它时,您的请求将有HTTP 404响应。
Karma 还接受正则表达式模式(minimatch 字符串)作为路由 - 如文档中所述 - 因此您app/**/*.js将匹配app任何级别内的任何 js 文件,而app/*.js只会匹配文件app夹内的 JS 文件。

In case of a proxy, say you're interested to serve images, karma sets up a static server where http://localhost:8080/basemaps your project root directory.

在代理的情况下,假设您有兴趣提供图像,karma 会设置一个静态服务器,用于http://localhost:8080/base映射您的项目根目录。

For a full explanation have a look at the karma documentation.

有关完整说明,请查看karma 文档