最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin6浏览0评论

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:

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:

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?

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:

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:

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?

Share Improve this question edited Jan 13, 2015 at 16:15 Max Koretskyi asked Jan 12, 2015 at 17:16 Max KoretskyiMax Koretskyi 106k67 gold badges353 silver badges515 bronze badges 2
  • 1 Because it has to create the routing to serve them when required. Karma creates a web server that serves the files you want to test: if you don't load them at startup you still want to reach them later, that's why it wants to know where they are. – MarcoL Commented Jan 12, 2015 at 17:26
  • thanks for the explanation, but still it's unclear. Is it because how routing is working inside nodejs (I assume karma is using NodeJs)? Because otherwise karma could easily extend the path from the root. And does webserver serve files under my project's root? It doesn't copy it them anywhere? – Max Koretskyi Commented Jan 12, 2015 at 17:41
Add a ment  | 

1 Answer 1

Reset to default 15

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

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:

...
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.

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).

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.

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.

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论