karma-runner / karma

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

Inherit working directory from calling process #2842

Open bdwain opened 7 years ago

bdwain commented 7 years ago

Expected behaviour

the working directory for my test runner is my actual working directory

Actual behaviour

the working directory is /

Environment Details

    files: [
      testIndexPath
    ],

    plugins: [
      'karma-coverage',
      'karma-chrome-launcher',
      'karma-jasmine',
      'karma-jasmine-expect-jsx',
      'karma-webpack'
    ],

    preprocessors: {
      [testIndexPath]: ['webpack']
    },

//testIndex file
let context = require.context(path.resolve(process.cwd(), 'src/js/'), true, /\.js$/);
context.keys().forEach(context);

This is how karma-webpack is supposed to be used. However, however, when the file gets loaded by karma, process.cwd() is "/". I would expect it to inherit from the calling process, and also be settable by a configuration option.

Process.cwd() is necessary if you are trying to have a default test index file like my example, and to have it in node_modules. Otherwise I wouldn't know where to look for the code.

Is it possible to add a cwd option to allow setting of the cwd from the node api and the command line? Also, generally it makes more sense for cwd to default to inherit the calling process's cwd.

bdwain commented 7 years ago

any update on this? it seems like it'd be a simple fix, and it would be very helpful.

EzraBrooks commented 6 years ago

Seems like a good feature to have - my only concern is that it may require retooling whole portions of the code that does things like hosting the web resources. I'd happily review a PR on this, if submitted.