karma icon indicating copy to clipboard operation
karma copied to clipboard

Proxies with path outside basePath

Open anius opened this issue 8 years ago • 3 comments

Expected behaviour

Proxied files found.

Actual behaviour

WARN [web-server]: 404 when running tests with proxy path that is one level above basePath:

WARN [web-server]: 404: /base/../node_modules/some-package/dist/img/t10062725.png

Pattern that is not outside basePath works perfectly.

Environment Details

  • Karma version: 1.7.0
  • Relevant part of your karma.config.js file:
basePath: '../src',
files: [
  {pattern: '../node_modules/some-package/dist/img/*.png', watched: false, included: false, served: true, nocache: false}
],
proxies: {
  '/test/img/': '/base/../node_modules/some-package/dist/img/'
 },

anius avatar May 12 '17 13:05 anius

There is the solution based on path node module:

const path = require('path');
module.exports = {
    basePath: '../src',
    files: [
        {pattern: '../node_modules/some-package/dist/img/*.png', watched: false, included: false, served: true, nocache: false}
    ],
    proxies: {
        '/img/': path.resolve('../node_modules/some-package/dist/img/')
    },
};

ymkins avatar Jan 16 '18 17:01 ymkins

I couldn't make it work with @ymkins suggestion, but debugging how Karma loads scripts I saw it uses the /absolute path in the debug.html when loading scripts above it's current path in the files array. The code below works:

    proxies: {
        '/img/': '/absolute' + path.resolve('../node_modules/some-package/dist/img/')
    },

I don't see this documented anywhere though, so I have to guess it's some magic internal API.

filipesilva avatar Sep 17 '18 12:09 filipesilva

And this is what I had to do to get a javascript file from one project in another project

The url:

http://localhost:9876/OurPath/Utils.js

And the karma.conf.js. Note the difference in the number of '..'s in files compared to proxy, I don't know why

	files: [
		{ pattern: "../../../OtherWebProduct/Scripts/OurPath/Utils.js", included: false, served: true, watched: false, nocache: true  }
	],
	proxies: {
		'/OurPath/': '/absolute' + path.resolve('../../OtherWebProduct/Scripts/OurPath')
	}

shard2001 avatar Sep 24 '19 13:09 shard2001