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
why do I need to specify all files for karma config even if they are loaded with requirejs
提问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 sampleSpec
dependencies:
这是我的sampleSpec
依赖项:
define(["app/bootstrap", "app/dots/some"], function () {}]
So as I understand it I load bootstrap
and some
files into browser using requirejs. However, depending on whether I specify dots/*
folder in my karma.conf.js
file the karma server succeeds or fails to resolve dots/some.js
file. What I mean is if I specify the following pattern: 'app/**/*.js'
in karma.conf.js
:
所以,按照我的理解我加载bootstrap
并some
使用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.js
file 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 karma
care 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 files
array 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 404
response.
Karma accepts also regexp patterns (minimatch strings) as routes - as specified in the documentation - so your app/**/*.js
will match any js files within app
at any level, while app/*.js
will only match JS files strictly inside the app
folder.
如果文件未映射,那么 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/base
maps your project root directory.
在代理的情况下,假设您有兴趣提供图像,karma 会设置一个静态服务器,用于http://localhost:8080/base
映射您的项目根目录。
For a full explanation have a look at the karma documentation.
有关完整说明,请查看karma 文档。