Karma: Proxies with path outside basePath

Created on 12 May 2017  路  3Comments  路  Source: karma-runner/karma

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/'
 },
investigation support

Most helpful comment

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.

All 3 comments

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/')
    },
};

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.

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')
    }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

joerideg picture joerideg  路  5Comments

charpour picture charpour  路  3Comments

jhildenbiddle picture jhildenbiddle  路  4Comments

wellyshen picture wellyshen  路  4Comments

schippie picture schippie  路  5Comments