Requirejs do not add “.js” for modules using karma

前端 未结 2 1743
余生分开走
余生分开走 2021-01-17 00:08

I have simple test, that must work in webstorm using karma and requirejs.

The problem is that for some reason requirejs do not add \".js\" for modules i was loading

相关标签:
2条回答
  • 2021-01-17 00:25

    Here is an interesting read on how RequireJs handles this:

    http://requirejs.org/docs/api.html#jsfiles

    Reading that makes it seem like an issue with RequireJS, but there seems to be some debate on whether or not that is true. Regardless, this gist seems to solve the issue.

    var tests = Object.keys(window.__karma__.files).filter(function (file) {
      return /\.spec\.js$/.test(file);
    }).map(function(file){
      return file.replace(/^\/base\/src\/js\/|\.js$/g,'');
    });
    
    require.config({
      baseUrl: '/base/src/js'
    });
    
    require(tests, function(){
      window.__karma__.start();
    });
    
    0 讨论(0)
  • 2021-01-17 00:30

    It looks like trouble in require.js

    The problem is in next: 1. When in deps appear absolute path - requirejs stop add ".js" for any require call 2. When in deps appear file with extension, for some reason requirejs again stop add ".js" for modules

    Other Solution here - to replace bathUrl and add it to requirejs conf do not help.

    For me solution is next:

    var tests = Object.keys(window.__karma__.files).filter(function (file) {
        return (/\-jasmine\.js$/).test(file);
    }).map(function (file) {
        return file.replace(/^\/|\.js$/g, '');
    });
    

    and

    baseUrl: '',
    

    for requirejs.conf

    and i have no idea why requirejs still add "/base" for all url that are requested, but now all works fine

    0 讨论(0)
提交回复
热议问题