Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Test with karma and webpack: _karma_webpack_ - no such file or directory

I am trying to run a unit test with coverage (using karma-coverage) and webpack (using karma-webpack). The tests run as expected, but to generate a coverage report the actual source file (not the test) needs to be loaded and passed through the coverage and webpack preprocessors.

Unfortunately this fails with the following error:

ERROR [karma]: { [Error: no such file or directory]
  code: 'ENOENT',
  errno: 34,
  message: 'no such file or directory',
  path: '/_karma_webpack_/views/Foo/Foo.js' }
Error: no such file or directory

Foo.js is the file that contains the source. Directory structure is like this:

karma.conf.js
  - src/
      - js/
        - views/
            - Foo/
                - Foo.js
                - test/
                    - FooTest.js

karma.conf.js:

module.exports = function (config) {
  config.set({
    basePath: 'src/js/',

    frameworks: ['jasmine'],

    files: [
      '**/test/*Test.js',
      '**/Foo.js',
    ],

    exclude: [],

    preprocessors: {
      '**/test/*Test.js': ['webpack'],
      '**/Foo.js': ['webpack', 'coverage'],
    },

    coverageReporter: {
      type: 'html',
      dir: 'coverage',
    },

    webpack: {
      resolve: {
        alias: [
          { _karma_webpack_: 'src/js' },
        ],
      },

    },

    reporters: ['progress', 'coverage'],

    port: 9876,

    colors: true,

    logLevel: config.LOG_DEBUG,

    autoWatch: true,

    browsers: ['Chrome'],

    singleRun: false,

    concurrency: Infinity,
  });
};

The problem is obvious: the path /_karma_webpack_/views/Foo/Foo.js indeed doesn't exist. I guess it's some internal path, but how can I change this?

As you can see I already tried to use the webpack resolve option for this, but it's not working. Since the error message states ERROR [karma] I am also a bit concerned that this error might be something different.

Also, I had the suspicion that maybe the globbing pattern **/Foo.js is off, but trying some changes there (like **/**/Foo.js) also didn't help.

like image 675
Sven Avatar asked Apr 02 '16 16:04

Sven


1 Answers

I had this same problem. Turns out, I needed to get out of the karma_webpack folder, so instead of

preprocessors: {
  '**/test/*Test.js': ['webpack'],
  '**/Foo.js': ['webpack', 'coverage'],
},

try

preprocessors: {
  '../**/test/*Test.js': ['webpack'],
  '../**/Foo.js': ['webpack', 'coverage'],
},
like image 86
lmdc Avatar answered Oct 05 '22 23:10

lmdc