karma-runner / karma

Spectacular Test Runner for JavaScript
http://karma-runner.github.io
MIT License
11.95k stars 1.71k forks source link

Proxies with path outside basePath #2703

Open anius opened 7 years ago

anius commented 7 years ago

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

ymkins commented 6 years ago

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/')
    },
};
filipesilva commented 6 years ago

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.

shard2001 commented 5 years ago

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')
    }