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

对着背影说爱祢 提交于 2019-12-01 13:04:33

问题


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 for tests. So it crashed trying to load "../ts/mamats/mama", while "../ts/mamats/mama.js" exists

Test (main.jasmine.js):

define(["require", "exports", "../ts/mamats/mama"], function(require, exports, mama) {
    describe("first test", function () {
        it("should be true", function () {
            var object = new mama.block();
            expect(object instanceof mama.block).toBe(true);
        });
    });
});
//# sourceMappingURL=main.jasmine.js.map

every thing works correctly when i replace "../ts/mamats/mama" with "../ts/mamats/mama.js"

sourceMappingURL here because javaScript file generated from typeScript source file, and because of that i cannot add ".js" for modules manually

Test starts with this entry point (main-test.js):

var tests = Object.keys(window.__karma__.files).filter(function (file) {
    return (/\.jasmine\.js$/).test(file);
});
requirejs.config({
    baseUrl: '/base',
    deps: tests,
    callback: window.__karma__.start
});

Why requirejs don't add ".js" for modules here?

Karma conf file:

module.exports = function(config) {
  config.set({
    basePath: '../',
    frameworks: ['jasmine', 'requirejs'],
    files: [
      'static-tests/main-test.js',
      { pattern: 'static/**/*', included: false },
      { pattern: 'static-tests/**/*', included: false }
    ],
    exclude: [],
    preprocessors: {},
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: false,
    browsers: ['Chrome'],
    singleRun: false
  });
};

回答1:


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();
});



回答2:


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



来源:https://stackoverflow.com/questions/26656102/requirejs-do-not-add-js-for-modules-using-karma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!